var cmd = require('commander');
var amok = require('./');
var temp = require('temp');

var pkg = require('./package.json');

cmd.usage('[options] <script>');

cmd.option('-h, --host <HOST>', '', 'localhost');
cmd.option('-p, --port <PORT>', '', 9966);

cmd.version(pkg.version);
cmd.parse(process.argv);

cmd.cwd = process.cwd();
cmd.browser = process.env['BROWSER'];
cmd.bundler = process.env['BUNDLER'];

cmd.scripts = cmd.args.reduce(function(object, value, key) {
  object[key] = value;
  return object;
}, {});

if (cmd.bundler) {
  cmd.scripts = { 'bundle.js': temp.path({suffix: '.js'}) };

  cmd.args.push('-o');
  cmd.args.push(cmd.scripts['bundle.js']);

  amok.bundle(cmd, function(error) {
  });
}

var watcher = amok.watch(cmd, function() {
  for (var script in cmd.scripts) {
    var filename = cmd.scripts[script];
    watcher.add(filename);
  }
});

setTimeout(function() {
  var bugger = amok.debug(cmd, function() {
    watcher.on('change', function(filename) {
      var script = Object.keys(cmd.scripts).filter(function(key) { return cmd.scripts[key] === filename})[0];
      if (script) {
        console.log('re-compiling', script);

        bugger.source(script, null, function(error) {
          if (error) {
            return console.error(error);
          }
          
          console.log('recompilation succesful');
        });
      }
    });
  });
}, 500);

// serve scripts and resources
var server = amok.serve(cmd, function() {
  var address = server.address();
  
  console.log('http server listening on http://%s:%d', address.address, address.port);
  
  if (cmd.browser) {
    var browser = amok.browse(cmd, function() {
    });
  }
});

@@ -657,16 +657,18 @@
 .js'%5D);%0A
+  
 %0A  amok.
@@ -693,19 +693,101 @@
 on(error
-) %7B
+, stdout, stderr) %7B%0A    process.stdout.pipe(stdout);%0A    process.stderr.pipe(stderr);
 %0A  %7D);%0A%7D
@@ -1007,22 +1007,525 @@
 unction(
-) %7B%0A  
+target) %7B%0A  %7D);%0A   %0A  bugger.on('attach', function(target) %7B%0A    console.info('debugger attatched to target %25s', target.title);%0A  %7D);%0A%0A  bugger.on('detatch', function() %7B%0A    var id = setInterval(function() %7B%0A      client.targets(function(targets) %7B%0A        var target = targets.filter(function(target) %7B%0A          return target.url.indexOf(cmd.host) %3E -1;%0A        %7D)%5B0%5D;%0A%0A        client.attach(target);%0A      %7D);%0A    %7D, 500);%0A%0A    bugger.once('attach', function() %7B%0A      clearInterval(id);%0A    %7D);%0A  %7D);%0A  %0A
   watche
@@ -1556,26 +1556,24 @@
 filename) %7B%0A
-  
     var scri
@@ -1668,26 +1668,24 @@
 e%7D)%5B0%5D;%0A    
-  
 if (script) 
@@ -1690,56 +1690,8 @@
 ) %7B%0A
-        console.log('re-compiling', script);%0A%0A  
     
@@ -1746,18 +1746,16 @@
         
-  
 if (erro
@@ -1755,26 +1755,24 @@
 f (error) %7B%0A
-  
           re
@@ -1810,25 +1810,11 @@
     
-  %7D%0A          %0A  
+%7D%0A%0A
     
@@ -1829,15 +1829,17 @@
 ole.
-log
+info
 ('re
+-
 comp
@@ -1865,26 +1865,24 @@
 ;%0A      
-  
 %7D);%0A    
   %7D%0A    
@@ -1877,19 +1877,9 @@
     
-  %7D%0A    %7D);
+%7D
 %0A  %7D
@@ -1895,39 +1895,8 @@
 );%0A%0A
-// serve scripts and resources%0A
 var 

Improve log output