var gulp        = require('gulp');
var concat      = require('gulp-concat');
var jade        = require('gulp-jade');
var stylus      = require('gulp-stylus');
var compiler    = require('gulp-hogan-compile');
var minifyCSS   = require('gulp-minify-css');
var browserify  = require('browserify');
var fs          = require('fs');

var paths = {
  app:        './client/js/app/app.coffee',
  site:       './client/js/site/site.coffee',
  styles:     ['client/css/*'],
  js:         ['client/js/**/**'],
  templates:  ['client/templates/**/*.jade']
};

var envs = {
  development: {
    name: 'development',
    host: 'http://localhost:5000'
  },
  production:{
    name: 'production',
    host: 'https://vmux.co'
  }
};

var env = envs[process.env.NODE_ENV || 'development'];

gulp.task('js', function() {
  var b = browserify(paths.app)
  b.require('./client/js/app/vendor/jquery-2.1.0.js', {expose: 'jquery'})
  b.require('./client/js/app/vendor/hogan-3.0.0.js',  {expose: 'hogan'})
  b.require('./client/js/app/vendor/backbone.js',     {expose: 'backbone'})
  b.transform('coffeeify')
  if (!process.env.DEBUG) b.transform('uglifyify');
  b.bundle({}, function(err, src) {
    if (err) {
      console.log('[browserify][err]', err);
    } else {
      var output = 'window.ENV=' + JSON.stringify(env) + '\n' + src;
      fs.writeFile('./static/vmux.js', output);
      console.log('[browserify][vmux.js done]');
    }
  });
});

gulp.task('site.js', function() {
  var b = browserify(paths.site)
  b.require('./client/js/app/vendor/jquery-2.1.0.js', {expose: 'jquery'})
  b.transform('coffeeify')
  if (!process.env.DEBUG) b.transform('uglifyify');
  b.bundle({}, function(err, src) {
    if (err) {
      console.log('[browserify][err]', err);
    } else {
      fs.writeFile('./static/site.js', src);
      console.log('[browserify][site.js done]');
    }
  });
});

gulp.task('css', function () {
  gulp.src(paths.styles)
    .pipe(stylus({set:['compress']}))
    .pipe(minifyCSS({keepSpecialComments: 0}))
    .pipe(concat('vmux.css'))
    .pipe(gulp.dest('./static'));
});

gulp.task('templates', function () {
  fn = function(file) { return file.relative.replace('.html', ''); }

  gulp.src(paths.templates)
    .pipe(jade())
    .pipe(compiler('templates.js', {
      wrapper: 'commonjs', 
      templateName: fn
    }))
    .pipe(gulp.dest('./client/js/app'))
});

// Continuous build
gulp.task('watch', function() {
  gulp.watch([paths.js],        ['js']);
  gulp.watch([paths.styles],    ['css']);
  gulp.watch([paths.templates], ['templates']);
});

gulp.task('default', ['js', 'css', 'templates', 'site.js', 'watch']);

@@ -784,32 +784,34 @@
 ('js', function(
+cb
 ) %7B%0A  var b = br
@@ -829,16 +829,52 @@
 ths.app)
+;%0A  var bundle_opts = %7Bdebug: true%7D;
 %0A  b.req
@@ -935,24 +935,25 @@
 : 'jquery'%7D)
+;
 %0A  b.require
@@ -1013,16 +1013,17 @@
 hogan'%7D)
+;
 %0A  b.req
@@ -1090,16 +1090,17 @@
 kbone'%7D)
+;
 %0A  b.tra
@@ -1122,34 +1122,80 @@
 fy')
+;%0A
 %0A  if (
-!process.env.DEBUG)
+env.name === 'production') %7B%0A    bundle_opts.debug = false;%0A   
  b.t
@@ -1209,32 +1209,37 @@
 m('uglifyify');%0A
+  %7D%0A%0A
   b.bundle(%7B%7D, f
@@ -1225,34 +1225,43 @@
   %7D%0A%0A  b.bundle(
-%7B%7D
+bundle_opts
 , function(err, 
@@ -1300,37 +1300,38 @@
 ole.log(
-'%5Bbrowserify%5D%5Berr%5D', 
+err);%0A      return cb(
 err);%0A  
@@ -1329,34 +1329,26 @@
 (err);%0A    %7D
- else %7B%0A  
+%0A%0A
     var outp
@@ -1402,26 +1402,24 @@
  + src;%0A    
-  
 fs.writeFile
@@ -1449,65 +1449,46 @@
 tput
-);%0A      console.log('%5Bbrowserify%5D%5Bvmux.js done%5D'
+, function(err) %7B%0A      cb(err
 );%0A    %7D
 %0A  %7D
@@ -1475,32 +1475,34 @@
   cb(err);%0A    %7D
+);
 %0A  %7D);%0A%7D);%0A%0Agulp
@@ -1523,24 +1523,26 @@
 ', function(
+cb
 ) %7B%0A  var b 
@@ -1565,16 +1565,41 @@
 hs.site)
+;%0A  var bundle_opts = %7B%7D;
 %0A  b.req
@@ -1696,33 +1696,78 @@
 y')%0A
+%0A
   if (
-!process.env.DEBUG)
+env.name === 'production') %7B%0A    bundle_opts.debug = false;%0A   
  b.t
@@ -1789,16 +1789,21 @@
 yify');%0A
+  %7D%0A%0A
   b.bund
@@ -1805,18 +1805,27 @@
 .bundle(
-%7B%7D
+bundle_opts
 , functi
@@ -1876,29 +1876,30 @@
 log(
-'%5Bbrowserify%5D%5Berr%5D', 
+err);%0A      return cb(
 err)
@@ -1909,18 +1909,9 @@
    %7D
- else %7B%0A  
+%0A
     
@@ -1950,65 +1950,43 @@
  src
-);%0A      console.log('%5Bbrowserify%5D%5Bsite.js done%5D'
+, function() %7B%0A      cb(err
 );%0A    %7D
 %0A  %7D
@@ -1981,16 +1981,18 @@
 );%0A    %7D
+);
 %0A  %7D);%0A%7D

Fix gulp builds