const gulp = require('gulp');
const async = require('async');
const fs = require('fs');
const rimraf = require('rimraf');
const zip = require('gulp-zip');
const util = require('gulp-util');
const electron = require('gulp-atom-electron');
const symdest = require('gulp-symdest');
const git = require('gulp-git');
const exec = require('child_process').exec;

const BUILD_DIR = 'build';
const PACKAGES = `${BUILD_DIR}/packages`;

gulp.task('python:install', callback => {
    async.waterfall([
        cb => rimraf(PACKAGES, cb),
        cb => async.concat(fs.readFileSync('build/gofbot/requirements.txt')
            .toString()
            .split('\n')
            .map(dep => dep.trim().replace('-e ', '')), (cmd, _) => exec(`pip install ${cmd} --target ${PACKAGES}`, _), err => cb(err)),
        cb => fs.open(`${PACKAGES}/google/__init__.py`, 'wx', cb),
        (file, cb) => fs.close(file, cb)
    ], callback);
});

gulp.task('python:package', ['python:install'], () => {
    return gulp.src(`${PACKAGES}/**/!(*.pyc|*.egg-info)`)
        .pipe(zip('packages.zip'))
        .pipe(gulp.dest('build'))
});

const PACKAGE_SRC = ['app/**', 'LICENSE', 'gofbot/**/*', 'package.json', 'main.js'];

gulp.task('electron:osx', ['python:package'], () => {
    return gulp.src(PACKAGE_SRC, {base: '.'})
        .pipe(electron({
            version: '1.3.3',
            platform: 'darwin',
            darwinIcon: 'src/assets/resources/image/icons/pokemon.icns',
            darwinBundleIdentifier: 'com.github.pokemongof'
        })).pipe(symdest('build'));
});

gulp.task('electron:windows', ['python:package'], () => {
    return gulp.src(PACKAGE_SRC.concat('pywin/**/*'), {base: '.'})
        .pipe(electron({
            version: '1.3.3',
            platform: 'win32',
            arch: 'ia32',
            winIcon: 'src/assets/resources/image/icons/pokemon.ico',
            companyName: 'PokemonGoF',
            copyright: '2016 PokemonGOF, All Rights Reserved.'
        }))
        .pipe(zip('app-windows.exe'))
        .pipe(gulp.dest('build/'));
});

gulp.task('gofbot:reset', () => {
    return gulp.src('gofbot/*')
        .pipe(git.fetch('origin', 'master'))
        .pipe(git.reset('HEAD', {args:'--hard origin/master'}))
        .pipe(git.clean({args: '-f'}));
});

gulp.task('build', ['python:package', 'electron:osx', 'electron:windows']);
@@ -350,16 +350,25 @@
 .exec;%0A%0A
+//CONFIG%0A
 const BU
@@ -427,16 +427,51 @@
 ckages%60;
+%0Aconst BOT = %60$%7BBUILD_DIR%7D/gofbot%60;
 %0A%0Agulp.t
@@ -2083,32 +2083,33 @@
 build/'));%0A%7D);%0A%0A
+%0A
 gulp.task('gofbo
@@ -2114,202 +2114,832 @@
 bot:
-reset', () =%3E %7B%0A    return gulp.src('gofbot/*')%0A        .pipe(git.fetch('origin', 'master'))%0A        .pipe(git.reset('HEAD', %7Bargs:'--hard origin/master'%7D))%0A        .pipe(git.clean(%7Bargs: '-f'%7D)
+update', (callback) =%3E %7B%0A    async.series(%5B%0A        _ =%3E git.exec(%7Bargs: %60submodule deinit -f $%7BBOT%7D%60%7D, _),%0A        _ =%3E git.updateSubmodule(%7B args: '--init' %7D, _)%0A    %5D, callback);%0A%7D);%0A%0Agulp.task('gofbot:prune', %5B'gofbot:update'%5D, (callback) =%3E %7B%0A    async.parallel(%5B%0A        _ =%3E rimraf(%60$%7BBOT%7D/docs%60, _),%0A        _ =%3E rimraf(%60$%7BBOT%7D/web%60, _),%0A        _ =%3E rimraf(%60$%7BBOT%7D/.github%60, _),%0A        _ =%3E rimraf(%60$%7BBOT%7D/tests%60, _),%0A        _ =%3E async.concat(%5B'ws_server.py', 'run.sh', 'setup.sh', 'README.md', 'pylint-recursive.py', 'run.bat',%0A            'LICENSE', 'Dockerfile', 'install.sh', 'CONTRIBUTORS.md', 'docker-compose.yml', '.travis.yml', '.styles.yapf',%0A            '.pylintrc', '.mention-bot', '.pullapprove.yml', '.gitmodules', '.dockerignore', '.gitignore'%5D.map(x =%3E %60$%7BBOT%7D/$%7Bx%7D%60), fs.unlink, _)%0A    %5D, callback
 );%0A%7D

Revert "Test"