/*
 * slush-ember
 * https://github.com/jonkemp/slush-ember
 *
 * Copyright (c) 2014, Jonathan Kemp
 * Licensed under the MIT license.
 */

'use strict';

var gulp = require('gulp'),
    gutil = require('gulp-util'),
    install = require('gulp-install'),
    conflict = require('gulp-conflict'),
    template = require('gulp-template'),
    rename = require('gulp-rename'),
    _s = require('underscore.string'),
    inquirer = require('inquirer'),
    path = require('path');

gulp.task('default', function (done) {

    gutil.log('Out of the box you get Ember.js Starter Kit to build your app.');

    inquirer.prompt([
        {
            type: 'confirm',
            name: 'moveon',
            message: 'Continue?'
        }
    ],
    function (answers) {
        var appname;

        if (!answers.moveon) {
            return done();
        }

        try {
            appname = require(path.join(process.cwd(), 'bower.json')).name;
        } catch (e) {
            try {
                appname = require(path.join(process.cwd(), 'package.json')).name;
            } catch (e) {}
        }

        if (!appname) {
            appname = path.basename(process.cwd());
        }

        answers.appname = appname.replace(/[^\w\s]+?/g, ' ');
        answers.appNameSlug = _s.slugify(answers.appname);

        gulp.src(__dirname + '/templates/**')
            .pipe(template(answers))
            .pipe(rename(function (file) {
                if (file.basename[0] === '_') {
                    file.basename = '.' + file.basename.slice(1);
                }
            }))
            .pipe(conflict('./'))
            .pipe(gulp.dest('./'))
            .pipe(install())
            .on('finish', function () {
                done();
            });
    });
});

@@ -469,18 +469,90 @@
 ('path')
+,%0A    gulpif = require('gulp-if'),%0A    replace = require('gulp-replace')
 ;%0A
-
 %0Agulp.ta
@@ -1463,16 +1463,102 @@
 swers))%0A
+            .pipe(gulpif('**/gulpfile.js', replace('@@contents', '%3C%25= contents %25%3E')))%0A
         
@@ -1894,32 +1894,32 @@
         done();%0A
-
             %7D);%0A
@@ -1917,16 +1917,823 @@
      %7D);
+%0A%0A        process.on('exit', function () %7B%0A            var fs = require('fs'),%0A                wiredep = require('wiredep'),%0A                skipInstall = process.argv.slice(2).indexOf('--skip-install') %3E= 0;%0A%0A            if (!skipInstall) %7B%0A                var bowerJson = JSON.parse(fs.readFileSync('./bower.json'));%0A%0A                // wire Bower packages to .html%0A                wiredep(%7B%0A                    bowerJson: bowerJson,%0A                    directory: 'bower_components',%0A                    src: 'app/index.html'%0A                %7D);%0A            %7D else %7B%0A                gutil.log('After running %60npm install & bower install%60, inject your front end dependencies into');%0A                gutil.log('your HTML by running:');%0A                gutil.log('  gulp wiredep');%0A            %7D%0A        %7D);
 %0A    %7D);

Add wiredep on exit.