/* jslint node: true */
'use strict';

var generators = require('yeoman-generator');
var path = require('path');
var spawn = require('child_process').execSync;

var prompts = [{
  type: 'input',
  name: 'appName',
  message: 'Your project name',
  default: path.basename(process.cwd())
}, {
  type: 'input',
  name: 'version',
  message: 'Your project initial version',
  default: '0.1.0'
}, {
  type: 'input',
  name: 'description',
  message: 'Quick project description'
}, {
  type: 'input',
  name: 'keywords',
  message: 'Give some keywords (comma separated)'
}, {
  type: 'input',
  name: 'repository',
  message: 'Git repo ?',
  default: defaultGitRepository()
}];

module.exports = generators.Base.extend({
  constructor: function () {
    generators.Base.apply(this, arguments);
    this.skeletonDirectory = '.';
    this.appPath = '.';
  },
  prompting: function () {
    var done = this.async();
    this.log('Welcome to Lightstream scaffolder !');
    this.log('Let’s try to scaffold your new Lightstream app !');
    this.log('I hope you’ll get a lot of money with this one :)');

    this.prompt(prompts, function (answers) {
      this.appName = answers.appName;
      this.version = answers.version;
      this.description = answers.description;
      this.keywords = answers.keywords ? answers.keywords.split(',') : null;
      done();
    }.bind(this));
  },
  writing: {
    copyDevelopmentFiles: function () {
      var files = ['.editorconfig', '.eslintrc', 'Gruntfile.js', 'nginx.conf.hbs'];
      files.forEach(function (file) {
        this.copy(
          path.join(this.skeletonDirectory, file),
          path.join(this.appPath, file)
        );
      }.bind(this));
    },
    copyPackageJson: function () {
      this.fs.copyTpl(
        this.templatePath('_package.json'),
        this.destinationPath('package.json'),
        {
          appName: this.appName,
          version: this.version,
          description: this.description,
          repository: this.repository,
          keywords: this.keywords ? JSON.stringify(this.keywords.map(function (keyword) {
            return keyword.trim();
          })) : null
        }
      );
    },
    copyAppFiles: function () {
      var files = ['config/index.js', 'app/front/js/main/index.js', 'app/front/less/main/index.less', 'public/index.html'];
      files.forEach(function (file) {
        this.copy(
          path.join(this.skeletonDirectory, file),
          path.join(this.appPath, file)
        );
      }.bind(this));
    }
  },
  install: function () {
    var done = this.async();
    this.installDependencies({
      npm: true,
      callback: function () {
        this.spawnCommand('yo', 'lightstream-config') // yo lightstream config
          .on('close', function () {
            this.spawnCommand('grunt', 'nginx') // grunt nginx
              .on('close', done);
          }.bind(this));
      }.bind(this)
    });
  },
  end: function () {
    this.log('It seems we’re done now !');
    this.log('’Hope everything’s ok and run !');
    this.log('Don’t forget to run : yo lightstream-config and grunt nginx ;)');
  }
});

function defaultGitRepository() {
  try {
    return spawn('git remote -v | grep fetch')
      .match(/:[^:.]*/g)[0]
      .replace(':', '');
  } catch (e) { }
}
@@ -1332,16 +1332,60 @@
 : null;%0A
+      this.repository = answers.repository;%0A
       do
@@ -2025,16 +2025,33 @@
 sitory: 
+formatRepository(
 this.rep
@@ -2057,16 +2057,17 @@
 pository
+)
 ,%0A      
@@ -2608,37 +2608,8 @@
 ) %7B%0A
-    var done = this.async();%0A
     
@@ -2694,291 +2694,359 @@
     
-this.spawnCommand('yo', 'lightstream-config') // yo lightstream config%0A          .on('close', function () %7B%0A            this.spawnCommand('grunt', 'nginx') // grunt nginx%0A              .on('close', done);%0A          %7D.bind(this));%0A      %7D.bind(this)
+spawn('yo lightstream-config', %7B stdio: 'inherit' %7D);%0A        spawn('grunt nginx', %7B stdio: 'inherit' %7D);%0A        spawn('git init', %7B stdio: 'inherit' %7D);%0A        if (this.repository) %7B%0A          spawn('git remote add origin ' + this.repository, %7B stdio: 'inherit' %7D);%0A          //spawn('git pull origin master', %7B stdio: 'inherit' %7D);
 %0A    
-%7D);%0A  %7D,%0A  end: function () %7B%0A
+    %7D%0A%0A    
     
@@ -3080,32 +3080,36 @@
 ne now !');%0A    
+    
 this.log('%E2%80%99Hope 
@@ -3106,289 +3106,707 @@
 og('
-%E2%80%99Hope everything%E2%80%99s ok and run !');%0A    this.log('Don%E2%80%99t forget to run : yo lightstream-config and grunt nginx ;)');%0A  %7D%0A%7D);%0A%0Afunction defaultGitRepository() %7B%0A  try %7B%0A    return spawn('git remote -v %7C grep fetch')%0A      .match(/:%5B%5E:.%5D*/g)%5B0%5D%0A      .replace(':', '');%0A  %7D catch (e) %7B %7D
+Git repository has been created and is ready for your first commit');%0A        this.log('Don%E2%80%99t forget to create a symlink of %3Clightstream-directory%3E/app/node in the local app directory !');%0A        this.log('%E2%80%99Hope everything%E2%80%99s ok and run ! ;)');%0A      %7D.bind(this)%0A    %7D);%0A  %7D%0A%7D);%0A%0Afunction defaultGitRepository() %7B%0A  try %7B%0A    return spawn('git remote -v %7C grep fetch')%0A      .match(/:%5B%5E:.%5D*/g)%5B0%5D%0A      .replace(':', '');%0A  %7D catch (e) %7B %7D%0A%7D%0A%0A//git@github.com:lightstream-company/lightstream-scaffolder.git%0Afunction formatRepository(repository) %7B%0A  if (/http/g.test(repository)) %7B%0A    return repository;%0A  %7D%0A%0A  return 'https://' + repository.replace('git@', '').replace(':', '/').replace('.git', '');
 %0A%7D

fix install scripts launch and add scripts for git init