var gulp = require('gulp');
var concat = require('gulp-concat');
var del = require('del');

// js
var babel = require('gulp-babel');

// css
var sass = require('gulp-sass');
var postcss = require('gulp-postcss');
var autoprefixer = require('autoprefixer-core');
var filter = require('gulp-filter');

// livereload and sync
var browserSync = require('browser-sync');
var reload = browserSync.reload;

// dir vars
var source = '.';
var dest = 'built';
var destStylesDir = dest + '/styles';
var destScriptsDir = dest + '/scripts';
var destFontsDir = dest + '/fonts';
var destImagesDir = dest + '/images';

// js vars
var jsDir = source + '/js';
var jsGlob = jsDir + '/**/*.js';
var jsLibsFile = 'libs.js';
var jsAppFile = 'app.js';
var jsLibsGlob = [
    source + '/bower_components/loader.js/loader.js',
    'node_modules/gulp-babel/node_modules/babel-core/browser-polyfill.js',
    source + '/bower_components/html5shiv/dist/html5shiv.min.js',
    source + '/bower_components/jquery/dist/jquery.min.js',
    source + '/bower_components/bootstrap-sass/assets/javascripts/bootstrap.min.js',
];

// fonts vars
var fontsGlob = [
    source + '/bower_components/bootstrap-sass/assets/fonts/**/*.*',
];

// images vars
var imagesGlob = source + '/images/**/*.*';

// css vars
var sassDir = source + '/sass';
var sassGlob = sassDir + '/**/*.scss';

// tasks
// copy files
gulp.task('copy', ['copy:fonts', 'copy:images']);

gulp.task('copy:fonts', function() {
    return gulp.src(fontsGlob)
        .pipe(gulp.dest(destFontsDir));
});

gulp.task('copy:images', function() {
    return gulp.src(imagesGlob)
        .pipe(gulp.dest(destImagesDir));
});

gulp.task('js:clean', function(cb) {
    del(destScriptsDir, cb);
});

gulp.task('js:clean:libs', function(cb) {
    del(destScriptsDir + '/' + jsLibsFile, cb);
});

gulp.task('js:clean:app', function(cb) {
    del(destScriptsDir + '/' + jsAppFile, cb);
});

// vendor libraries
gulp.task('js:libs', ['js:clean:libs'], function() {
    return gulp.src(jsLibsGlob)
        .pipe(concat(jsLibsFile))
        .pipe(gulp.dest(destScriptsDir));
});

// app source
gulp.task('js:app', ['js:clean:app'], function() {
    return gulp.src(jsGlob)
        .pipe(babel({
            modules: 'amd',
            moduleIds: true,
            sourceRoot: __dirname + '/js',
            moduleRoot: '',
            experimental: 2,
        }))
        .on('error', function (err) { console.error(err.toString()); this.emit('end'); })
        .pipe(concat(jsAppFile))
        .pipe(gulp.dest(destScriptsDir));
});

gulp.task('css:clean', function(cb) {
    del(destStylesDir, cb);
});

gulp.task('sass', ['css:clean'], function() {
    var processors = [
        autoprefixer({ browsers: ['last 3 versions'] })
    ];

    return gulp.src(sassGlob)
        // css preprocessing
        // bootstrap-sass requires a precision >= 8
        .pipe(sass({ precision: 10 }))
        .on('error', function (err) { console.error(err.toString()); this.emit('end'); })
        // css postprocessing
        .pipe(postcss(processors))
        .pipe(gulp.dest(destStylesDir))
        // filter css for livereload
        .pipe(filter('**/*.css'))
        .pipe(reload({ stream: true }));
});

gulp.task('default', ['js:libs', 'js:app', 'sass', 'copy'], function() {
    gulp.watch(jsLibsGlob, ['js:libs']);
    gulp.watch(jsGlob, ['js:app']);
    gulp.watch(sassGlob, ['sass']);
});

@@ -357,16 +357,25 @@
 r-sync')
+.create()
 ;%0Avar re
@@ -448,13 +448,14 @@
  = '
-built
+public
 ';%0Av
@@ -1413,16 +1413,29 @@
 :images'
+, 'copy:html'
 %5D);%0A%0Agul
@@ -1652,32 +1652,139 @@
 agesDir));%0A%7D);%0A%0A
+gulp.task('copy:html', function() %7B%0A    return gulp.src('index.html')%0A        .pipe(gulp.dest(dest));%0A%7D);%0A%0A
 gulp.task('js:cl
@@ -3304,31 +3304,27 @@
 ipe(
-reload(%7B stream: true %7D
+browserSync.stream(
 ));%0A
@@ -3409,114 +3409,539 @@
     
-gulp.watch(jsLibsGlob, %5B'js:libs'%5D);%0A    gulp.watch(jsGlob, %5B'js:app'%5D);%0A    gulp.watch(sassGlob, %5B'sass'%5D
+browserSync.init(%7B%0A        logLevel: 'debug',%0A        online: false,%0A        server: %7B%0A            baseDir: 'public',%0A            index: 'index.html',%0A        %7D,%0A        port: 9000,%0A        ui: %7B%0A            port: 8080,%0A            weinre: %7B%0A                port: 8000,%0A            %7D,%0A        %7D,%0A    %7D);%0A%0A    gulp.watch(jsLibsGlob, %5B'js:libs'%5D);%0A    gulp.watch(jsGlob, %5B'js:app'%5D);%0A    gulp.watch(sassGlob, %5B'sass'%5D);%0A    gulp.watch('index.html', %5B'copy:html'%5D);%0A    gulp.watch(dest + '/index.html').on('change', browserSync.reload
 );%0A%7D

Set up a browserSync static server