var Client = require('ssh2').Client
  , fs = require('fs')
  , tessel = require('tessel')
  , envfile = require('envfile')
  , config = envfile.parseFileSync(__dirname + '/../config.env')


function sftpDeploy(opts) {

  tessel.logs.info('Connecting to remote Tessel...');
  // Create a new SSH client connection object
  var conn = new Client()

  // Open up an SSH Connection
  conn.on('ready', function() {
    // Log that it was successful
    tessel.logs.info('Connected.');

    // Start an SFTP Transfer
    conn.sftp(function transferStarted (err, sftp) {
      // Throw any unfortunate errors
      if (err) throw err;
      tessel.logs.info('Bundling up code...');
      // Command V2 to extract a tarball it receives on stdin to the remote deploy dir
      conn.exec('rm -rf /root/remote-script/*; tar -x -C /root/remote-script', function(err, rstdin) {
        // Gather details about the file structure of the script being sent
        var ret = tessel.analyzeScript(process.cwd() + "/" + opts.entryPoint, {verbose: opts.verbose});
        // Tar up the code to improve transfer rates
        tessel.tarCode(ret.pushdir, {node: true}, function(err, bundle) {
          tessel.logs.info('Bundled.')
          // Throw any unfortunate errors
          if (err) throw err;

          tessel.logs.info('Deploying code of size', ret.size, 'bytes ...');
          // Write the zipped code to stdin on v2
          rstdin.end(bundle);

          
          // Wait for the transfer to finish...
          rstdin.once('finish', function() {
            tessel.logs.info('Deployed.');
            tessel.logs.info('Running script...');
            // Once it has been written, run the script with Node
            conn.exec('node remote-script/' + opts.entryPoint, function(err, stream) {
              if (err) throw err;
              stream.on('close', function(code, signal) {
                console.log('CLOSING')
                sftp.end();
                conn.end();
              }).on('data', function(data) {
                console.log(data.toString());
              }).stderr.on('data', function(data) {
                if (opts.verbose) console.log("Err: ", data.toString());
              });
            });
          });
        });
      }); 
    });
  }).connect({
    host: config.host,
    port: 22,
    username: config.username,
    privateKey: require('fs').readFileSync(config.keyPath),
    passphrase: config.keyPassphrase
  });
}

module.exports.sftpDeploy = sftpDeploy;
@@ -478,157 +478,8 @@
 );%0A%0A
-    // Start an SFTP Transfer%0A    conn.sftp(function transferStarted (err, sftp) %7B%0A      // Throw any unfortunate errors%0A      if (err) throw err;%0A  
     
@@ -515,26 +515,24 @@
  code...');%0A
-  
     // Comma
@@ -604,26 +604,24 @@
 loy dir%0A    
- 
- 
 conn.exec('r
@@ -711,18 +711,16 @@
 %7B%0A      
-  
 // Gathe
@@ -779,18 +779,16 @@
 ng sent%0A
-  
       va
@@ -883,26 +883,24 @@
 se%7D);%0A      
-  
 // Tar up th
@@ -932,18 +932,16 @@
 r rates%0A
-  
       te
@@ -1004,34 +1004,32 @@
 ndle) %7B%0A        
-  
 tessel.logs.info
@@ -1041,18 +1041,16 @@
 dled.')%0A
-  
         
@@ -1081,34 +1081,32 @@
  errors%0A        
- 
- 
 if (err) throw e
@@ -1110,18 +1110,16 @@
 w err;%0A%0A
-  
         
@@ -1189,26 +1189,24 @@
 ');%0A        
- 
- 
 // Write the
@@ -1237,26 +1237,24 @@
  v2%0A        
-  
 rstdin.end(b
@@ -1262,18 +1262,16 @@
 ndle);%0A%0A
-  
         
@@ -1275,26 +1275,24 @@
    %0A        
-  
 // Wait for 
@@ -1317,18 +1317,16 @@
 nish...%0A
-  
         
@@ -1362,34 +1362,32 @@
 n() %7B%0A          
-  
 tessel.logs.info
@@ -1397,26 +1397,24 @@
 eployed.');%0A
-  
           te
@@ -1460,18 +1460,16 @@
         
- 
- 
 // Once 
@@ -1514,18 +1514,16 @@
 th Node%0A
-  
         
@@ -1607,26 +1607,24 @@
             
- 
- 
 if (err) thr
@@ -1631,18 +1631,16 @@
 ow err;%0A
-  
         
@@ -1691,77 +1691,8 @@
 ) %7B%0A
-                console.log('CLOSING')%0A                sftp.end();%0A  
     
@@ -1725,18 +1725,16 @@
         
-  
 %7D).on('d
@@ -1770,18 +1770,16 @@
         
-  
 console.
@@ -1812,18 +1812,16 @@
         
- 
- 
 %7D).stder
@@ -1842,34 +1842,32 @@
 unction(data) %7B%0A
-  
               if
@@ -1925,26 +1925,8 @@
 ));%0A
-              %7D);%0A
     

Remove unused sftp process