'use strict';

var util = require('./util');
var path = require('path');

exports = module.exports = function(gulp){

  // when the gulpfile is passed as argument default to process.argv
  if(!gulp.argv && (process.argv[1] === gulp.gulpfile || util.fromGulpBin)){
    gulp.argv = process.argv.slice(2);
  } else {
    gulp.argv = util.type(gulp.argv).array;
  }

  gulp.task(':cli(--silent)', function(next){
    gulp.log = false;
    util.log = util.gutil.log = util.silent;
    next();
  });

  gulp.task(':cli(--version|-v)', function(next){
    var packagePath = require.resolve('../package.json');
    util.log('version %s (%s)', require(packagePath).version);
    next();
  });

  gulp.task(':cli(--cwd) :dirname(\\S+)', function(next){
    var state = 'already set';
    var dirname = path.resolve(this.params.dirname);
    if(process.cwd() !== dirname){
      process.chdir(dirname);
      state = 'changed';
    }

    util.log('cwd %s to %s', state, util.format.path(dirname));
    next();
  });

  gulp.task(':cli(--no-color|--color)', function(next){
    util.chalk.enabled = Boolean(this.params.cli === '--color');
    util.log('colors', util.chalk.enabled
      ? util.format.enabled('enabled')
      : 'disabled'
    );
    next();
  });

  gulp.task(':cli(--tasks-simple|--tasks|-T)', function(next){
    var options = {
      simple: this.params.cli === '--tasks-simple'
    };

    if(options.simple){
      console.log(gulp.tree(options).join('\n'));
    } else if(gulp.log){
      var tree = util.archy(
        gulp.tree({label: 'Tasks for ' + util.format.path(gulp.gulpfile)})
      );

      tree.split('\n').forEach(function(line){
        if(line.trim()){ util.log(line); }
      });
    }

    next();
  });

  if(gulp.argv && gulp.argv.length){
    if(gulp.repl){
      gulp.repl.emit('line', argv.join(' '));
    } else {
      gulp.start.apply(gulp, argv);
    }
  }

};

@@ -67,16 +67,63 @@
 ath');%0A%0A
+var gulpfile = path.resolve(process.argv%5B1%5D);%0A%0A
 exports 
@@ -246,162 +246,189 @@
 argv
- && (process.argv%5B1%5D === gulp.gulpfile %7C%7C util.fromGulpBin))%7B%0A    gulp.argv = process.argv.slice(2);%0A  %7D else %7B%0A    gulp.argv = util.type(gulp.argv).array
+.length && gulpfile === gulp.gulpfile)%7B%0A    gulp.argv = process.argv.slice(2);%0A  %7D%0A%0A  if(!/(%5E%7C%5B%5Cs%5D)/.test(util.xargs))%7B%0A    gulp.argv.unshift('--cwd ' + path.dirname(gulp.gulpfile))
 ;%0A  
@@ -620,76 +620,31 @@
     
-var packagePath = require.resolve('../package.json');%0A    util.log('
+util.log('gulp-runtime 
 vers
@@ -653,13 +653,8 @@
 n %25s
- (%25s)
 ', r
@@ -664,19 +664,25 @@
 ire(
+'../
 package
-Path
+.json'
 ).ve
@@ -770,39 +770,8 @@
 t)%7B%0A
-    var state = 'already set';%0A
     
@@ -815,24 +815,25 @@
 s.dirname);%0A
+%0A
     if(proce
@@ -891,38 +891,8 @@
 ;%0A  
-    state = 'changed';%0A    %7D%0A%0A
     
@@ -909,18 +909,23 @@
 cwd 
-%25s
+changed
  to %25s',
  sta
@@ -924,15 +924,8 @@
 %25s',
- state,
  uti
@@ -945,24 +945,31 @@
 (dirname));%0A
+    %7D%0A%0A
     next();%0A
@@ -1297,19 +1297,91 @@
 var 
-options = %7B
+taskTree = gulp.tree(%7B%0A      label: 'Tasks for ' + util.format.path(gulp.gulpfile),
 %0A   
@@ -1433,16 +1433,17 @@
 e'%0A    %7D
+)
 ;%0A%0A    i
@@ -1448,22 +1448,44 @@
  if(
-options.
+this.params.cli === '--tasks-
 simple
+'
 )%7B%0A 
@@ -1505,26 +1505,16 @@
 log(
-gulp.tree(options)
+taskTree
 .joi
@@ -1557,19 +1557,8 @@
     
- var tree =
  uti
@@ -1569,104 +1569,24 @@
 chy(
-%0A        gulp.tree(%7Blabel: 'Tasks for ' + util.format.path(gulp.gulpfile)%7D)%0A      );%0A%0A      tree
+taskTree).trim()
 .spl
@@ -1598,15 +1598,11 @@
 n').
-forEach
+map
 (fun
@@ -1625,25 +1625,8 @@
     
- if(line.trim())%7B
  uti
@@ -1637,18 +1637,16 @@
 g(line);
- %7D
 %0A      %7D
@@ -1692,21 +1692,8 @@
 argv
- && gulp.argv
 .len
@@ -1746,16 +1746,21 @@
 'line', 
+gulp.
 argv.joi
@@ -1810,16 +1810,21 @@
 y(gulp, 
+gulp.
 argv);%0A 

refactor tree, change to gulpfile cwd, cleanup