#!/usr/bin/env node

var common = require('../src/cli')

// Setup cli.
common.basic();

// Command-line arguments
var argv = require("nomnom")
  .script('tessel-push')
  .option('script', {
    position: 0,
    // required: true,
    full: 'script.js',
    help: 'Run this script on Tessel.',
  })
  .option('args', {
    abbr: 'a',
    list: true,
    help: 'Arguments to pass in as process.argv.'
  })
  .option('quiet', {
    abbr: 'q',
    flag: true,
    help: '[Tessel] Hide tessel deployment messages.'
  })
  .option('messages', {
    abbr: 'm',
    flag: true,
    help: '[Tessel] Forward stdin as child process messages.'
  })
  .option('single', {
    abbr: 's',
    flag: true,
    help: '[Tessel] Push a single script file to Tessel.'
  })
  .option('flash', {
    abbr: 'f',
    flag: true,
    help: 'Write program to flash'
  })

  .parse();

argv.verbose = !argv.quiet;

function usage () {
  console.error(require('nomnom').getUsage());
  process.exit(1);
}

common.controller(function (err, client) {
  client.listen(true, [10, 11, 12, 13, 20, 21, 22])
  client.on('error', function (err) {
    if (err.code == 'ENOENT') {
      console.error('Error: Cannot connect to Tessel locally.')
    } else {
      console.error(err);
    }
  })

  // Forward stdin as messages with "-m" option
  if (argv.messages) {
    process.stdin.resume();
    require('readline').createInterface(process.stdin, {}, null).on('line', function (std) {
      client.send(JSON.stringify(std));
    })
  }

  // Check pushing path.
  if (!argv.script) {
    usage();
  } else {
    var pushpath = argv.script;
  }

  // Command command.
  var updating = false;
  client.on('command', function (command, data) {
    if (command == 'u') {
      verbose && console.error(data.grey)
    } else if (command == 'U') {
      if (updating) {
        // Interrupted by other deploy
        process.exit(0);
      }
      updating = true;
    }
  });

  client.run(pushpath, ['tessel', pushpath].concat(argv.arguments || []), {
    flash: argv.flash
  }, function (err) {
    // Stop on Ctrl+C.
    process.on('SIGINT', function() {
      client.once('script-stop', function (code) {
        process.exit(code);
      });
      setTimeout(function () {
        // timeout :|
        process.exit(code);
      }, 2000);
      client.stop();
    });

    client.once('script-stop', function (code) {
      client.close(function () {
        process.exit(code);
      });
    });
  });
})

@@ -622,32 +622,140 @@
 messages.'%0A  %7D)%0A
+  .option('logs', %7B%0A    abbr: 'l',%0A    flag: true,%0A    help: '%5BTessel%5D Stay connected and print logs.'%0A  %7D)%0A
   .option('singl
@@ -858,101 +858,8 @@
  %7D)%0A
-  .option('flash', %7B%0A    abbr: 'f',%0A    flag: true,%0A    help: 'Write program to flash'%0A  %7D)%0A%0A
   .p
@@ -2035,18 +2035,13 @@
 sh: 
-argv.flash
+true,
 %0A  %7D
@@ -2063,102 +2063,54 @@
 ) %7B%0A
-    // Stop on Ctrl+C.%0A    process.on('SIGINT', function() %7B%0A      client.once('script-stop', 
+%0A    function exit(code) %7B%0A      client.close(
 func
@@ -2107,36 +2107,32 @@
 close(function (
-code
 ) %7B%0A        proc
@@ -2136,32 +2136,37 @@
 rocess.exit(code
+ %7C%7C 0
 );%0A      %7D);%0A   
@@ -2170,210 +2170,116 @@
     
-  setTimeout(function () %7B%0A        // timeout :%7C%0A        process.exit(code);%0A      %7D, 2000);%0A      client.stop();%0A    %7D);%0A%0A    client.once('script-stop', function (code) %7B%0A      client.close(function ()
+%7D%0A%0A    if (argv.logs) %7B%0A      process.on('SIGINT', exit);%0A      client.once('script-stop', exit);%0A    %7D else
  %7B%0A 
@@ -2287,47 +2287,22 @@
     
- 
- process.
 exit(
-code
 );%0A    
-  %7D);%0A    %7D);
+%7D%0A
 %0A  %7D

Make `tessel push` write to flash and detach by default.