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>', 'specify http host', 'localhost');
cmd.option('-p, --port <PORT>', 'specify http port', 9966);

cmd.option('-H, --debugger-host <HOST>', 'specify debugger host', 'localhost');
cmd.option('-P, --debugger-port <PORT>', 'specify debugger port', 9222);

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, stdout, stderr) {
//    process.stdout.pipe(stdout);
//    process.stderr.pipe(stderr);
  });
}

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(target) {
  });
   
  bugger.on('attach', function(target) {
    console.info('debugger attatched to target %s', target.title);
  });

  bugger.on('detatch', function() {
    var id = setInterval(function() {
      client.targets(function(targets) {
        var target = targets.filter(function(target) {
          return target.url.indexOf(cmd.host) > -1;
        })[0];

        client.attach(target);
      });
    }, 500);

    bugger.once('attach', function() {
      clearInterval(id);
    });
  });
  
  watcher.on('change', function(filename) {
    var script = Object.keys(cmd.scripts).filter(function(key) { return cmd.scripts[key] === filename})[0];
    if (script) {
      bugger.source(script, null, function(error) {
        if (error) {
          return console.error(error);
        }

        console.info('re-compilation succesful');
      });
    }
  });
}, 500);

var server = amok.serve(cmd, function() {
  var address = server.address();
  console.info('http server listening on http://%s:%d', address.address, address.port);
  
  if (cmd.browser) {
    var browser = amok.browse(cmd, function(error, stdout, stderr) {
//      process.stdout.pipe(stdout);
//      process.stderr.pipe(stderr);
    });
  }
});

@@ -430,24 +430,81 @@
 t', 9222);%0A%0A
+cmd.option('--no-debugger', 'disable remote debugger');%0A%0A
 cmd.version(
@@ -1212,24 +1212,48 @@
 unction() %7B%0A
+  if (cmd.debugger) %7B%0A  
   var bugger
@@ -1290,16 +1290,18 @@
 rget) %7B%0A
+  
   %7D);%0A  
@@ -1301,17 +1301,21 @@
  %7D);%0A   
+  
 %0A
+  
   bugger
@@ -1343,24 +1343,26 @@
 n(target) %7B%0A
+  
     console.
@@ -1418,21 +1418,25 @@
 tle);%0A  
+ 
+ 
 %7D);%0A%0A
+  
   bugger
@@ -1463,24 +1463,26 @@
 ion() %7B%0A    
+  
 var id = set
@@ -1505,24 +1505,26 @@
 n() %7B%0A      
+ 
+ 
 client.targe
@@ -1542,24 +1542,26 @@
 (targets) %7B%0A
+  
         var 
@@ -1599,24 +1599,26 @@
 n(target) %7B%0A
+  
           re
@@ -1669,16 +1669,18 @@
     
+  
 %7D)%5B0%5D;%0A%0A
     
@@ -1675,16 +1675,18 @@
 %7D)%5B0%5D;%0A%0A
+  
         
@@ -1708,16 +1708,18 @@
 arget);%0A
+  
       %7D)
@@ -1724,16 +1724,18 @@
 %7D);%0A    
+  
 %7D, 500);
@@ -1732,24 +1732,26 @@
   %7D, 500);%0A%0A
+  
     bugger.o
@@ -1783,16 +1783,18 @@
 %7B%0A      
+ 
+ 
 clearInt
@@ -1812,21 +1812,29 @@
     
+  
 %7D);%0A  
+ 
+ 
 %7D);%0A  
+ 
-%0A
+ %0A  
   wa
@@ -1869,24 +1869,26 @@
 filename) %7B%0A
+  
     var scri
@@ -1983,24 +1983,26 @@
 e%7D)%5B0%5D;%0A    
+  
 if (script) 
@@ -1999,24 +1999,26 @@
  (script) %7B%0A
+  
       bugger
@@ -2049,32 +2049,34 @@
 nction(error) %7B%0A
+  
         if (erro
@@ -2086,24 +2086,26 @@
 %7B%0A          
+  
 return conso
@@ -2129,16 +2129,18 @@
         
+  
 %7D%0A%0A     
@@ -2138,24 +2138,26 @@
  %7D%0A%0A        
+ 
+ 
 console.info
@@ -2192,24 +2192,26 @@
 ;%0A      
+  
 %7D);%0A    
 %7D%0A  %7D);%0A
@@ -2206,16 +2206,24 @@
     
+  
 %7D%0A  
+ 
+ 
 %7D);%0A
+  %7D%0A
 %7D, 5

Add no-debugger flag to allow disbling the debugger