'use strict';

var path = require('path');
var fs = require('fs');
var proc = require('child_process');
var nex = require('nex-api');
var github = require('nex-github');
var _ = require('lodash');
var targz = require('tar.gz');
var rimraf = require('rimraf');
var log = require('npmlog');

var handler = module.exports = new nex.Handler('repository');

/**
 * @override
 */
handler.do = function (pkg) {
  let packageName = pkg.name + '-' + pkg.version;
  let repository = _.extend({ version: pkg.version }, pkg[this.field]);

  // skip, if already cloned
  if (fs.existsSync(path.resolve(process.cwd(), '.git'))) {
    log.info(packageName, 'found existing .git directory. skipping');
    return;
  }

  github.getRelease(repository)
    .then(function (tarball) {
      log.info('tarball', 'extracting', tarball);

      new targz().extract(tarball, path.resolve(process.cwd(), 'extract'), function (err) {
        if (err) {
          log.error('extract', err);
          throw err;
        }

        log.info('tarball', 'extracted');

        proc.execSync([ 'cp -r', path.resolve('extract', packageName, '*'), process.cwd() ].join(' '));
        rimraf.sync(path.resolve(process.cwd(), 'extract'));
        rimraf.sync(path.resolve(process.cwd(), packageName + '.tar.gz'));
      }); 
    },  
    function () {
      log.error('install', 'Failed to download', pkg.name, 'Please try again');
      process.exit(1);
    }); 
};

/**
 * @override
 */
handler.undo = function (pkg) {
  rimraf.sync('node_modules');
};

@@ -546,14 +546,26 @@
 ady 
-cloned
+installed from git
 %0A  i
@@ -611,19 +611,25 @@
 wd(), '.
-git
+npmignore
 '))) %7B%0A 
@@ -673,22 +673,30 @@
 ing 
-.
 git
- directory
+hub release install
 . sk
@@ -701,16 +701,19 @@
 skipping
+...
 ');%0A    
@@ -1528,20 +1528,18 @@
 nc('
-node_modules
+.npmignore
 ');%0A

check for npmignore as indicator of previous install