 # Rerun the task when a file changes
 gulp.task "watch", ->
     gulp.watch(paths.jade, ["jade-watch"])
-    gulp.watch("#{paths.app}/index.jade", ["template"])
-    gulp.watch(paths.scssStyles, ["styles-watch"])
-    gulp.watch(paths.svg, ["svg"])
-    gulp.watch(paths.coffee, ["coffee-watch"])
-    gulp.watch(paths.vendorJsLibs, ["jslibs-watch"])
-    gulp.watch(paths.locales, ["coffee-watch"])
+    gulp.watch(paths.app + "index.jade", ["templates"])
+    gulp.watch(paths.sass, ["styles-watch"])
+    gulp.watch(paths.svg, ["copy-svg"])
+    gulp.watch(paths.coffee, ["app-watch"])
+    gulp.watch(paths.js, ["jslibs-watch"])
+    gulp.watch(paths.locales, ["app-watch"])
+    gulp.watch(paths.images, ["copy-images"])
+    gulp.watch(paths.fonts, ["copy-fonts"])
+
 
 gulp.task "deploy", [
+    "templates",
+    "copy",
     "jade-deploy",
-    "template",
-    "copy",
-    "svg",
-    "coffee-deploy",
+    "app-deploy",
     "jslibs-deploy",
     "styles-deploy"
 ]
 
 # The default task (called when you run gulp from cli)
 gulp.task "default", [
+    "copy",
+    "templates",
+    "styles-watch",
+    "app-watch",
+    "jslibs-watch",
     "jade-deploy",
-    "template",
-    "styles-watch",
-    "svg",
-    "copy",
-    "coffee-watch",
-    "jslibs-watch",
     "express",
     "watch"
 ]

cache = require("gulp-cached")
jadeInheritance = require('gulp-jade-inheritance')

paths = {
    app: "app"
    dist: "dist"
    html: "app/*.html"
    jade: ["app/index.jade", "app/partials/**/*.jade"]
    scssStyles: "app/styles/**/*.scss"
    distStylesPath: "dist/styles"
    distStyles: ["dist/styles/vendor.css",
                 "dist/styles/app.css"]
    sassStylesMain: "app/styles/main.scss"
    css:  "app/styles/vendor/*.css"
    images: "app/images/**/*"
    svg: "app/svg/**/*"
    locales: "app/locales/**/*.json"
    coffee: ["app/coffee/app.coffee",
             "app/config/*.coffee",
             "app/coffee/*.coffee",
             "app/coffee/modules/controllerMixins.coffee",
             "app/coffee/modules/*.coffee",
             "app/coffee/modules/common/*.coffee",
             "app/coffee/modules/backlog/*.coffee",
             "app/coffee/modules/taskboard/*.coffee",
             "app/coffee/modules/kanban/*.coffee",
             "app/coffee/modules/issues/*.coffee",
             "app/coffee/modules/userstories/*.coffee",
             "app/coffee/modules/tasks/*.coffee",
             "app/coffee/modules/wiki/*.coffee",
             "app/coffee/modules/admin/*.coffee",
             "app/coffee/modules/projects/*.coffee",
             "app/coffee/modules/locales/*.coffee",
             "app/coffee/modules/base/*.coffee",
             "app/coffee/modules/resources/*.coffee",
             "app/coffee/modules/user-settings/*.coffee"
             "app/plugins/**/*.coffee"]
    vendorJsLibs: [
        "app/vendor/jquery/dist/jquery.js",
        "app/vendor/lodash/dist/lodash.js",
        "app/vendor/emoticons/lib/emoticons.js",
        "app/vendor/underscore.string/lib/underscore.string.js",
        "app/vendor/angular/angular.js",
        "app/vendor/angular-route/angular-route.js",
        "app/vendor/angular-sanitize/angular-sanitize.js",
        "app/vendor/angular-animate/angular-animate.js",
        "app/vendor/i18next/i18next.js",
        "app/vendor/moment/min/moment-with-langs.js",
        "app/vendor/checksley/checksley.js",
        "app/vendor/pikaday/pikaday.js",
        "app/vendor/jquery-flot/jquery.flot.js",
        "app/vendor/jquery-flot/jquery.flot.pie.js",
        "app/vendor/jquery-flot/jquery.flot.time.js",
        "app/vendor/jquery-flot/jquery.flot.time.js",
        "app/vendor/flot-axislabels/jquery.flot.axislabels.js",
        "app/vendor/jquery-textcomplete/jquery.textcomplete.js",
        "app/vendor/markitup/markitup/jquery.markitup.js",
        "app/vendor/malihu-custom-scrollbar-plugin/jquery.mCustomScrollbar.concat.min.js",
        "app/js/jquery.ui.git.js",
        "app/js/sha1.js",
        "app/plugins/**/*.js"
    ]
}

############################################################################
# Layout/CSS Related tasks

        .pipe(plumber())
        .pipe(cache("jade"))
        .pipe(jade({pretty: false}))
        .pipe(gulp.dest(paths.dist + "partials/"))

gulp.task "jade-watch", ->
    gulp.src(paths.jade)
        .pipe(plumber())
        .pipe(cache("jade"))
        .pipe(jadeInheritance({basedir: "./app"}))
        .pipe(jade({pretty: true}))
        .pipe(gulp.dest(paths.dist))

gulp.task "templates", ->
    gulp.src(paths.app + "index.jade")
        .pipe(plumber())
        .pipe(jade({pretty: true, locals:{v:(new Date()).getTime()}}))
        .pipe(gulp.dest(paths.dist))

gulp.task "sass-lint", ->
    gulp.src(paths.sass)
        .pipe(cache("sasslint"))
        .pipe(scsslint({config: "scsslint.yml"}))

gulp.task "sass-watch", ["sass-lint"], ->
    gulp.src(paths.app + "styles/main.scss")
        .pipe(plumber())
        .pipe(sass())
        .pipe(rename("app.css"))
        .pipe(gulp.dest(paths.tmp))

gulp.task "sass-deploy", ->
    gulp.src(paths.app + "styles/main.scss")
        .pipe(plumber())
        .pipe(sass())
        .pipe(rename("app.css"))
        .pipe(gulp.dest(paths.tmp))

gulp.task "css-vendor", ->
    gulp.src(paths.css)
        .pipe(concat("vendor.css"))
        .pipe(gulp.dest(paths.tmp))

gulp.task "css-lint-app", ["sass-watch"],  ->
    gulp.src(paths.tmp + "app.css")
        .pipe(csslint("csslintrc.json"))
        .pipe(csslint.reporter())

gulp.task "styles-watch", ["sass-watch", "css-vendor", "css-lint-app"], ->
    _paths = [
        paths.tmp + "vendor.css",
        paths.tmp + "app.css"
    ]

    gulp.src(_paths)
        .pipe(concat("main.css"))
        .pipe(gulp.dest(paths.dist + "styles/"))

gulp.task "styles-deploy", ["sass-deploy", "css-vendor"], ->
    _paths = [
        paths.tmp + "vendor.css",
        paths.tmp + "app.css"
    ]

    gulp.src(_paths)
        .pipe(concat("main.css"))
        .pipe(minifyCSS())
        .pipe(gulp.dest(paths.dist + "styles/"))

##############################################################################
# JS Related tasks
##############################################################################

gulp.task "conf", ->
    gulp.src("conf/main.json")
        .pipe(wrap("angular.module('taigaBase').value('localconf', <%= contents %>);"))
        .pipe(concat("conf.js"))
        .pipe(gulp.dest(paths.tmp));

gulp.task "locales", ->
    gulp.src("app/locales/en/app.json")
        .pipe(wrap("angular.module('taigaBase').value('localesEn', <%= contents %>);"))
        .pipe(rename("locales.en.js"))
        .pipe(gulp.dest(paths.tmp))

gulp.task "coffee", ->
    gulp.src(paths.coffee)
        .pipe(plumber())
        .pipe(coffee())
        .pipe(concat("app.js"))
        .pipe(gulp.dest(paths.tmp))

gulp.task "jslibs-watch", ->
    gulp.src(paths.js)
        .pipe(plumber())
        .pipe(concat("libs.js"))
        .pipe(gulp.dest("dist/js/"))

gulp.task "jslibs-deploy", ->
    gulp.src(paths.js)
        .pipe(plumber())
        .pipe(concat("libs.js"))
        .pipe(uglify({mangle:false, preserveComments: false}))
        .pipe(gulp.dest("dist/js/"))

gulp.task "app-watch", ["coffee", "conf", "locales"], ->
    _paths = [
        paths.tmp + "app.js",
        paths.tmp + "conf.js",
        paths.tmp + "locales.en.js"
    ]

    gulp.src(_paths)
        .pipe(concat("app.js"))
        .pipe(gulp.dest(paths.dist + "js/"));

gulp.task "app-deploy", ["coffee", "conf", "locales"], ->
    _paths = [
        paths.tmp + "app.js",
        paths.tmp + "conf.js",
        paths.tmp + "locales.en.js"
    ]

    gulp.src(_paths)
        .pipe(concat("app.js"))
        .pipe(uglify({mangle:false, preserveComments: false}))
        .pipe(gulp.dest(paths.dist + "js/"));

##############################################################################
# Common tasks
##############################################################################

# SVG
gulp.task "copy-svg",  ->
    gulp.src("#{paths.app}/svg/**/*")
        .pipe(gulp.dest("#{paths.dist}/svg/"))

gulp.task "copy-fonts",  ->
    gulp.src("#{paths.app}/fonts/*")
        .pipe(gulp.dest("#{paths.dist}/fonts/"))

gulp.task "copy-images",  ->
    gulp.src("#{paths.app}/images/**/*")
        .pipe(gulp.dest("#{paths.dist}/images/"))

gulp.task "copy-plugin-templates",  ->
    gulp.src("#{paths.app}/plugins/**/templates/*")
        .pipe(gulp.dest("#{paths.dist}/plugins/"))

gulp.task "copy", ["copy-fonts", "copy-images", "copy-plugin-templates", "copy-svg"]

gulp.task "express", ->
    express = require("express")
