#!/usr/bin/env flusspferd
// -*- mode:javascript; -*- vim:filetype=javascript:enc=utf-8:

const fs = require('fs-base'),
      sys = require('system'),
      io = require('io'),
      installer = require('juice/installer');

// Available as a function so we can run it as part of the install script
exports.makeScript = function () {

var path = installer.dirName(module.id);

io.File.create( path + '../bin/juice', parseInt( "0777", 8 ) );
var fh = fs.rawOpen( path + '../bin/juice', 'w');

fh.write(<><![CDATA[#!/usr/bin/env flusspferd
// -*- mode:javascript; -*- vim:filetype=javascript:
// Autogenerated - do not edit

var io = require( "io" ),
    system = require( "system" ),
    fs = require( "fs-base" ),
    installer = require( "juice/installer");

var JuiceCLI = {
  init : function ( name ) {
    // if name wasn't provided, exit
    if ( name === undefined ) throw "Usage: juice init my-new-project";

    if ( ! name.match( /^[-a-z0-9_]+$/i ) ) throw "Project name must consist of letters, numbers, hyphens and underscores";

    // if directory already exists, exit
    if ( fs.exists( name ) ) throw "Directory " + name + " already exists";

    var dir = fs.canonical(installer.makeTree(name));
    // create the directory structure
    var tree = [
      dir + "/db",
      dir + "/lib",
      dir + "/script",
      dir + "/static/scripts",
      dir + "/static/styles",
      dir + "/templates"
    ].map(installer.makeTree);

]]></> + "");

var files = [
  'lib/app.js', 
  'templates/index.tt', 
  'static/styles/app.css', 
  'script/server',
  'script/console'
];

for each (f in files) {
  if (f.match("^script/")) {
    // since our fs-base doesn't yet support permissions, we need to use io.File to
    // make it executable first
    fh.write('    io.File.create( dir + "' + f + '", parseInt( "0777", 8 ) );\n')
    fh.write('    if (installer.batchFilesNeeded)\n' + 
             '      fs.rawOpen(dir + "' + f + '.bat", "w").print(installer.batchFile);\n\n');

  }

  fh.write('\n    // ' + f + '\n'+ 
           '    fs.rawOpen( dir + "' + f + '", "w").print(<><![CDATA[' +
            fs.rawOpen(path + f, 'r')
              .read()
              .replace(/skeleton/g, ']]></> + name + <><![CDATA[') +
            ']]></> + "");\n'
          );
}

fh.print(<><![CDATA[
    print("Created Juice app", '"' + name + '"', "in", dir);
  } // end of init
}

var args = system.args.slice( 1 );
var action = args.shift();

if ( action in JuiceCLI )
  JuiceCLI[ action ].apply( undefined, args );
else
  throw "Valid actions: init";
]]></> + "");

}

if (require.main === module)
  exports.makeScript();

@@ -943,16 +943,17 @@
 -a-z0-9_
+.
 %5D+$/i ) 
@@ -949,24 +949,32 @@
 9_.%5D+$/i ) )
+ %7B%0A     
  throw %22Proj
@@ -1015,16 +1015,22 @@
 numbers,
+ dots,
  hyphens
@@ -1047,16 +1047,22 @@
 scores%22;
+%0A    %7D
 %0A%0A    //

Allow dots in project names.