'use strict';

var fs = require('fs');
var path = require('path');
var utils = require('./lib/utils');

/**
 * This is the defalt updater, it can also be used
 * to extend other updaters.
 */

module.exports = function(update, base, env) {
  var async = utils.async;
  var glob = utils.glob;

  /**
   * TODO: User help and defaults
   */

  update.register('defaults', function(app) {
    app.task('init', function(cb) {
      app.questions.set('init', 'Want to generate a new project?');
      app.ask('init', { force: true }, function(err, answers) {
        if (err) return cb(err);

        if (answers.init === 'y') {
          var generate = require('generate')();
          generate.fn(update, update, update.env);
          update.build(['prompt', 'files', 'write'], cb);
        } else {
          cb();
        }
      });
    });

    app.task('help', function(cb) {
      console.log('Would you like to choose a updater to run?');
      console.log('(implement me!)')
      cb();
    });

    app.task('error', function(cb) {
      console.log('update > error (implement me!)');
      cb();
    });
  });

  /**
   * Data store tasks
   */

  update.register('store', function(app) {
    app.task('del', function(cb) {
      update.store.del({ force: true });
      console.log('deleted data store');
      cb();
    });
  });

  /**
   * User prompts
   */

  update.task('prompt', function(cb) {
    var pkg = env.config.pkg;

    if (!pkg || env.user.isEmpty || env.argv.raw.init) {
      forceQuestions(update);
    }

    update.questions.setData(pkg || {});
    update.ask({ save: false }, function(err, answers) {
      if (err) return cb(err);
      if (!pkg) answers = {};

      answers.name = answers.name || utils.project();
      answers.varname = utils.namify(answers.name);
      update.set('answers', answers);
      cb();
    });
  });

  /**
   * Default configuration settings
   */

  update.task('defaultConfig', function(cb) {
    update.lazyIgnores();
    update.engine(['md', 'text'], require('engine-base'));
    update.data({year: new Date().getFullYear()});
    cb();
  });

  /**
   * Load files to be rendered
   */

  update.task('files', ['defaultConfig'], function(cb) {
    var ignore = update.get('cache.ignores');
    update.files(['*'], {cwd: update.cwd, dot: true, ignore: ignore});
    cb();
  });

  /**
   * Write files to disk
   */

  update.task('write', function() {
    var data = update.get('answers');
    return update.toStream('files')
      // .pipe(update.renderFile('text', data))
      // .pipe(update.dest(rename(dest)));
  });

  /**
   * Generate a new project
   */

  update.task('updaters', ['files']);

  /**
   * Default task to be run
   */

  update.task('default', function(cb) {
    update.build('files', cb);
  });
};

function forceQuestions(update) {
  update.questions.options.forceAll = true;
}

@@ -1852,32 +1852,100 @@
 %0A    %7D);%0A  %7D);%0A%0A
+  update.plugin('render', update.renderFile.bind(update, 'text'));%0A%0A
   /**%0A   * Defau
@@ -2481,24 +2481,110 @@
 unction() %7B%0A
+    var plugins = update.get('argv.plugins');%0A    var dest = update.get('argv.dest');%0A
     var data
@@ -2605,24 +2605,25 @@
 'answers');%0A
+%0A
     return u
@@ -2655,56 +2655,44 @@
     
- //
  .pipe(
-update.renderFile('text', data
+base.pipeline(plugins
 ))%0A     
  // 
@@ -2691,11 +2691,8 @@
     
- //
  .pi
@@ -2744,30 +2744,30 @@
   * 
-Generate a new project
+Default task to be run
 %0A   
@@ -2785,24 +2785,23 @@
 e.task('
-updaters
+default
 ', %5B'fil
@@ -2807,78 +2807,217 @@
 les'
-%5D);%0A%0A  /**%0A   * Default task to be run%0A   */%0A%0A  update.task('default',
+, 'write'%5D);%0A%7D;%0A%0A/**%0A * First init questions to be asked%0A */%0A%0Afunction forceQuestions(update) %7B%0A  update.questions.options.forceAll = true;%0A%7D%0A%0A/**%0A * Rename template files%0A */%0A%0Afunction rename(dest) %7B%0A  return
  fun
@@ -3018,34 +3018,36 @@
 rn function(
-cb
+file
 ) %7B%0A    
 update.build
@@ -3038,121 +3038,306 @@
     
-update.build('files', cb);%0A  %7D);%0A%7D;%0A%0Afunction forceQuestions(update) %7B%0A  update.questions.options.forceAll = true
+if (/%5C/templates%5C//.test(file.path)) %7B%0A      file.basename = file.basename.replace(/%5E_/, '.');%0A      file.basename = file.basename.replace(/%5E%5C$/, '');%0A    %7D%0A%0A    file.base = file.dest %7C%7C dest %7C%7C path.dirname(file.path);%0A    file.path = path.join(file.base, file.basename);%0A    return file.base;%0A  %7D
 ;%0A%7D%0A

move `renderFile` to plugin in updatefile. adds `rename` function and pipeline