var gulp          = require('gulp');

var $             = require('gulp-load-plugins')();
var source        = require('vinyl-source-stream');
var del           = require('del');
var browserify    = require('browserify');
var preprocessify = require('preprocessify');
var runSequence   = require('run-sequence');
var domain        = require('domain');

var env           = 'dev';
var webserver     = false;

log = function(task, start) {
  if (!start) {
    setTimeout(function() {
      $.util.log('Starting', '\'' + $.util.colors.cyan(task) + '\'...');
    }, 1);
  } else {
    var time = ((new Date() - start) / 1000).toFixed(2) + ' s';
    $.util.log('Finished', '\'' + $.util.colors.cyan(task) + '\'', 'after', $.util.colors.magenta(time));
  }
};

gulp.task('clean:dev', function() {
  return del(['.tmp']);
});

gulp.task('clean:dist', function() {
  return del(['dist']);
});

gulp.task('scripts', function() {
  var dev = env === 'dev';
  var filePath = './app/scripts/app.js';
  var extensions = ['.jsx'];

  var bundle = function() {
    if (dev) {
      var start = new Date();
      log('scripts:bundle');
    }
    browserify({
      entries: [filePath],
      extensions: extensions,
      debug: env === 'dev'
    }).transform(preprocessify({
      env: env
    }, {
      includeExtensions: extensions
    })).transform('reactify')
    .bundle()
      .pipe(source('app.js'))
      .pipe(gulp.dest('.tmp/scripts/bundle'))
      .pipe($.if(dev, $.tap(function() {
        log('scripts:bundle', start);
        if (!webserver) {
          runSequence('webserver');
        }
      })));
  }

  if (dev) {
    gulp.src(filePath)
      .pipe($.plumber())
      .pipe($.tap(function(file) {
        var d = domain.create();

        d.on('error', function(err) {
          $.util.log($.util.colors.red('Browserify compile error:'), err.message, '\n\t', $.util.colors.cyan('in file'), file.path);
          $.util.beep();
        });

        d.run(bundle);
      }));
  } else {
    bundle();
  }
});

gulp.task('images', function() {
  return gulp.src('app/img/*')
    .pipe(gulp.dest('dist/img'));
});

gulp.task('css', function() {
  return gulp.src('app/css/*')
    .pipe(gulp.dest('dist/css'));
});

gulp.task('font-awesome', function() {
  return gulp.src('app/font-awesome/*')
    .pipe(gulp.dest('dist/font-awesome'));
});

gulp.task('font-awesome', function() {
  return gulp.src('app/fonts/*')
    .pipe(gulp.dest('dist/fonts'));
});

gulp.task('static', function() {
  return gulp.src(['app/*.txt', 'app/*.ico'])
    .pipe(gulp.dest('dist'));
})

gulp.task('bundle', function () {
  var assets = $.useref.assets();
  var revAll = new $.revAll({dontRenameFile: [/^\/favicon.ico$/g, '.html']});
  var jsFilter = $.filter(['**/*.js']);
  var cssFilter = $.filter(['**/*.css']);
  var htmlFilter = $.filter(['*.html']);

  return gulp.src('app/index.html')
    .pipe($.preprocess())
    .pipe(assets)
    .pipe(assets.restore())
    .pipe($.useref())
    .pipe(jsFilter)
    .pipe($.uglify())
    .pipe(jsFilter.restore())
    .pipe(cssFilter)
    .pipe($.autoprefixer({
      browsers: ['last 5 versions']
    }))
    .pipe($.minifyCss())
    .pipe(cssFilter.restore())
    .pipe(htmlFilter)
    .pipe($.htmlmin({collapseWhitespace: true}))
    .pipe(htmlFilter.restore())
    .pipe(revAll.revision())
    .pipe(gulp.dest('dist'))
    .pipe($.size());
});

gulp.task('webserver', function() {
  webserver = gulp.src(['.tmp', 'app'])
    .pipe($.webserver({
      host: '0.0.0.0', //change to 'localhost' to disable outside connections
      livereload: {
        enable: true,
        filter: function(filePath) {
          if (/app\\(?=scripts)/.test(filePath)) {
            $.util.log('Ignoring', $.util.colors.magenta(filePath));
            return false;
          } else {
            return true;
          }
        }
      },
      open: true
    }));
});

gulp.task('serve', function() {
  runSequence('clean:dev', ['scripts']);
  gulp.watch('app/*.html');
  gulp.watch('app/scripts/**/*.js', ['scripts']);
  gulp.watch('app/scripts/**/*.jsx', ['scripts']);
});

gulp.task('build', function() {
  env = 'prod';
  runSequence(['clean:dev', 'clean:dist'],
              ['scripts'],
              'bundle', 'static');
});

gulp.task('default', ['build']);

@@ -2018,16 +2018,21 @@
 p.task('
+copy-
 images',
@@ -2037,32 +2037,32 @@
 ', function() %7B%0A
-
   return gulp.sr
@@ -2126,16 +2126,21 @@
 p.task('
+copy-
 css', fu
@@ -2223,32 +2223,37 @@
 %7D);%0A%0Agulp.task('
+copy-
 font-awesome', f
@@ -2363,28 +2363,26 @@
 p.task('
-font-awesome
+copy-fonts
 ', funct
@@ -2474,22 +2474,31 @@
 p.task('
-static
+copy-root-files
 ', funct
@@ -4223,16 +4223,97 @@
 scripts'
+, 'copy-root-files', 'copy-images', 'copy-fonts', 'copy-font-awesome', 'copy-css'
 %5D,%0A     
@@ -4333,18 +4333,8 @@
 dle'
-, 'static'
 );%0A%7D

Fix deployment script 18