var exec = require('child_process').exec;
var zip = require('gulp-zip');
var del = require('del');
var gulp = require('gulp');
var rename = require('gulp-rename');
var sequence = require('run-sequence');
var packager = require('electron-packager');
var vulcanize = require('gulp-vulcanize');
var winInstaller = require('electron-windows-installer');

var VERSION = require('./src/package.json').version;
var TEMP_BUILD_DIR = 'vulcanized';
var BUILD_DIR = 'build';
var DIST_DIR = 'dist';

gulp.task('make-installer', function() {
	return winInstaller({
		appDirectory: BUILD_DIR + '/Auto-Shutdown-win32-ia32',
		outputDirectory: DIST_DIR,
		iconUrl: __dirname + '/build-assets/logo-gray.ico',
		exe: 'Auto-Shutdown.exe',
		setupExe: 'Auto Shutdown Setup.exe',
		authors: 'Mccxiv Software',
		title: 'Auto Shutdown',
		setupIcon: __dirname + '/build-assets/logo-gray.ico'
	});
});

gulp.task('run-development', function(cb) {
	exec('"src/node_modules/.bin/electron" ./src', cb);
});

gulp.task('run-vulcanized', function(cb) {
	exec('"src/node_modules/.bin/electron" '+TEMP_BUILD_DIR, cb);
});

gulp.task('vulcanize', function() {
	return gulp.src('src/index.html')
		.pipe(vulcanize({
			excludes: [],
			inlineScripts: true,
			inlineCss: true,
			stripExcludes: false
		}))
		.pipe(gulp.dest(TEMP_BUILD_DIR));
});

gulp.task('copy-files', function() {
	return gulp.src(['src/main.js', 'src/package.json'])
		.pipe(gulp.dest(TEMP_BUILD_DIR));
});

gulp.task('copy-modules', function() {
	return gulp.src(['src/modules/**'])
		.pipe(gulp.dest(TEMP_BUILD_DIR+'/modules'));
});

gulp.task('copy-assets', function() {
	return gulp.src(['src/assets/**'])
		.pipe(gulp.dest(TEMP_BUILD_DIR+'/assets'));
});

gulp.task('clean-up-before', function(cb) {
	del([TEMP_BUILD_DIR, BUILD_DIR, DIST_DIR], cb);
});

gulp.task('clean-up-after', function(cb) {
	del([TEMP_BUILD_DIR, BUILD_DIR, 'dist/RELEASES', 'dist/**/*.nupkg'], cb);
});

gulp.task('package', function(cb) {
	var opts = {
		dir: 'vulcanized',
		name: 'Auto-Shutdown',
		platform: 'all',
		arch: 'ia32',
		version: '0.30.2',
		'app-version': VERSION,
		icon: 'build-assets/logo-gray.ico',
		'version-string': {
			CompanyName: 'Mccxiv Software',
			LegalCopyright: 'Copyright 2015 Andrea Stella. All rights reserved',
			ProductVersion: VERSION,
			FileVersion: VERSION,
			OriginalFilename: 'auto-shutdown.exe',
			FileDescription: 'Auto Shutdown',
			ProductName: 'Auto Shutdown'
		},
		out: BUILD_DIR
	};
	packager(opts, cb);
});

gulp.task('make-zip-windows', function() {
	return gulp.src('build/Auto-Shutdown-win32-ia32/**/*')
		.pipe(rename(function(path) {
			path.dirname = path.dirname === '.'? 'AutoShutdown' : 'AutoShutdown/'+path.dirname;
		}))
		.pipe(zip('autoshutdown-windows-'+VERSION+'.zip'))
		.pipe(gulp.dest('dist/'));
});

gulp.task('build', function(cb) {
	sequence('clean-up-before',
		['vulcanize', 'copy-files', 'copy-modules', 'copy-assets'],
		'package',
		['make-installer', 'make-zip-windows'],
		'clean-up-after', cb);
});

gulp.task('default', ['build']);
@@ -575,25 +575,24 @@
 DIR + '/Auto
--
 Shutdown-win
@@ -688,33 +688,32 @@
 o',%0A%09%09exe: 'Auto
--
 Shutdown.exe',%0A%09
@@ -728,25 +728,24 @@
 e: 'Auto
- 
 Shutdown
  Setup.e
@@ -740,14 +740,26 @@
 down
- 
+-
 Setup
+-'+VERSION+'
 .exe
@@ -2004,25 +2004,24 @@
 %09name: 'Auto
--
 Shutdown',%0A%09
@@ -2019,16 +2019,16 @@
 tdown',%0A
+
 %09%09platfo
@@ -2325,50 +2325,8 @@
 ON,%0A
-%09%09%09OriginalFilename: 'auto-shutdown.exe',%0A
 %09%09%09F
@@ -2513,17 +2513,16 @@
 ild/Auto
--
 Shutdown
@@ -2682,13 +2682,13 @@
 ip('
-autos
+AutoS
 hutd
@@ -2694,20 +2694,18 @@
 down-win
-dows
+32
 -'+VERSI

Make file names consistent: no spaces, no dash