var system = require('system');
var fs = require('fs');

var compiler = require('./bootstrap/compiler');

function compileFile (path) {
    fs.write((fs.directory(path) + '/' +
	      fs.base(path, '.ralph') + '.js'),
	     compiler.compile(fs.read(path)) + '\n');
}

var commands = {
    clean: function () {
	commands.cleanRuntime();
	commands.cleanTests();
    },
    cleanRuntime: function () {
	system.print('Cleaning runtime ...');
	fs.listTree('./runtime/').forEach(function (path) {
	    var path = './runtime/' + path;
	    if (fs.extension(path) == '.js') {
		system.print(' - ' + path);
		fs.removeTree(path);
	    }
	});
    },
    cleanTests: function () {
	system.print('Cleaning tests ...');
	try {
	    fs.removeTree('./tests.js');
	} catch (e) {};
    },
    bootstrapRuntime: function () {
	system.print('Bootstraping runtime ...');
	fs.listTree('./runtime/').forEach(function (path) {
	    var path = './runtime/' + path;
	    if (fs.extension(path) == '.ralph' &&
		fs.base(path, '.ralph')[0] != '.')
	    {
		system.print(' - ' + path);
		compileFile(path);
	    }
	});
    },
    compileTests: function () {
	system.print('Compiling tests ...');
	compileFile('./tests.ralph');
    }
}

system.args.slice(1).forEach(function (command) {
    if (commands.hasOwnProperty(command))
	commands[command]();
});

@@ -49,218 +49,751 @@
 ('fs
-');%0A%0Avar compiler = require('./bootstrap/compiler');%0A%0Afunction compileFile (path) %7B%0A    fs.write((fs.directory(path) + '/' +%0A%09      fs.base(path, '.ralph') + '.js'),%0A%09     compiler.compile(fs.read(path)) + '%5Cn'
+-base');%0A%0Afunction extension (path) %7B%0A    var dot = path.lastIndexOf('.');%0A    if (dot != -1)%0A%09return path.substring(dot);%0A    else%0A%09return %22%22;%0A%7D%0A%0Afunction basename (path) %7B%0A    var base = path.substring(path.lastIndexOf('/') + 1);%0A    if (base.lastIndexOf(%22.%22) != -1)%0A%09base = base.substring(0, base.lastIndexOf(%22.%22));%0A    return base;%0A%7D%0A%0Afunction dirname (path) %7B%0A    return path.replace(/%5C/%5B%5E%5C/%5D*$/, '');%0A%7D%0A%0Avar compiler = require('./bootstrap/compiler');%0A%0Afunction compileFile (path) %7B%0A    var source = fs.openRaw(path, 'r');%0A    var target = fs.openRaw((dirname(path) + '/'%0A%09%09%09   + basename(path) + '.js'),%0A%09%09%09  'w');%0A    var code = source.readWhole();%0A    target.write(compiler.compile(code) + '%5Cn');%0A    source.close();%0A    target.close(
 );%0A%7D
@@ -918,39 +918,32 @@
  function () %7B%0A%09
-system.
 print('Cleaning 
@@ -957,36 +957,32 @@
  ...');%0A%09fs.list
-Tree
 ('./runtime/').f
@@ -1003,36 +1003,32 @@
 n (path) %7B%0A%09    
-var 
 path = './runtim
@@ -1040,35 +1040,32 @@
  path;%0A%09    if (
-fs.
 extension(path) 
@@ -1074,31 +1074,24 @@
  '.js') %7B%0A%09%09
-system.
 print(' - ' 
@@ -1110,20 +1110,16 @@
 s.remove
-Tree
 (path);%0A
@@ -1160,39 +1160,32 @@
  function () %7B%0A%09
-system.
 print('Cleaning 
@@ -1218,20 +1218,16 @@
 s.remove
-Tree
 ('./test
@@ -1291,31 +1291,24 @@
 ction () %7B%0A%09
-system.
 print('Boots
@@ -1342,12 +1342,8 @@
 list
-Tree
 ('./
@@ -1384,20 +1384,16 @@
  %7B%0A%09    
-var 
 path = '
@@ -1421,19 +1421,16 @@
     if (
-fs.
 extensio
@@ -1458,30 +1458,21 @@
 &%0A%09%09
-fs.
 base
+name
 (path
-, '.ralph'
 )%5B0%5D
@@ -1489,23 +1489,16 @@
     %7B%0A%09%09
-system.
 print(' 
@@ -1583,23 +1583,16 @@
 n () %7B%0A%09
-system.
 print('C

make build script compatible with CommonJS fs-base and flusspferd