'use strict';

require('dotenv').load();

var pkg = require('./package.json'),
    path = require('path');

var gulp = require('gulp'),
    gutil = require('gulp-util'),
    inject = require('gulp-inject'),
    plumber = require('gulp-plumber'),
    pgbuild = require('gulp-phonegap-build'),
    bowerFiles = require('main-bower-files');

var dir = {
    out: path.join(__dirname, './out'),
    dist: path.join(__dirname, './dist'),
    assets: path.join(__dirname, './public'),
};

gulp.task('default', ['inject', 'build'], function () {
    gutil.log('Building... This might take a while.');
});

gulp.task('build', function () {
    gulp.src(dir.assets + '/**')
        .pipe(plumber())
        .pipe(pgbuild({
            appId: process.env.PG_BUILD_APP_ID,
            download: {
                android: dir.out + '/onesquare-' + pkg.version + '.apk'
            },
            user: {
                token: process.env.PG_BUILD_AUTH_TOKEN
            }
        }));
});

gulp.task('inject', ['copy'], function () {
    var sources = [
        dir.dist + '/lib/**/*.html'
    ];

    return gulp.src(dir.dist + '/index.html')
        .pipe(inject(gulp.src(sources, {read: false}), {relative: true}))
        .pipe(inject(gulp.src(bowerFiles(), {read: false}), {name: 'bower', relative: true}))
        .pipe(gulp.dest(dir.dist));
});

gulp.task('copy', function () {
    var sources = [
        dir.assets + '/**'
    ];

    return gulp.src(sources)
        .pipe(gulp.dest(dir.dist));
});

@@ -329,16 +329,61 @@
 -files')
+,%0A    livereload = require('gulp-livereload')
 ;%0A%0Avar d
@@ -1370,24 +1370,52 @@
 t(dir.dist))
+%0A        .pipe(livereload())
 ;%0A%7D);%0A%0Agulp.
@@ -1557,16 +1557,219 @@
 dir.dist));%0A%7D);%0A
+%0Agulp.task('watch', function () %7B%0A    var sources = %5B%0A        dir.assets + '/index.html',%0A        dir.assets + '/lib/**'%0A    %5D;%0A%0A    livereload.listen();%0A%0A    return gulp.watch(sources, %5B'inject'%5D);%0A%7D);%0A

Add Gulp LiveReload task.