"use strict";

var fs = require('fs');
var exec = require('child_process').exec;
var path = require('path');
var config = require('./config')();

var cwd = process.cwd();

module.exports = function(dir, msg, nopush) {

    if (!dir) {
        dir = path.resolve(cwd + '/' + config['dir'].site);
    }

    if (!msg) {
        msg = 'site update ' + new Date();
    }

    var cmdPrefix = 'git -C ' + dir + ' ';
    exec(cmdPrefix + 'init', function(err) {
        if (err) {
            console.error('Failed to initialize Git directory: ' + e);
            process.exit(1);
        }
        addFiles(cmdPrefix, msg, nopush, commit);
    });

};

var addFiles = function(cmdPrefix, msg, nopush, callback) {
    exec(cmdPrefix + 'add -A', function(err) {
        // dotfiles should be ignored by gitignore
        if (err) {
            console.error('Unable to track files: ' + err);
            process.exit(1);
        }
        callback(cmdPrefix, msg, nopush);
    });
};

var commit = function(cmdPrefix, msg, nopush) {
    exec(cmdPrefix + 'commit -m "' + msg + '"', function(err) {
        if (err) {
            console.error('Unable to commit changes: ' + err);
            console.log('May be there is no changes made?');
            process.exit(1);
        }
        if (nopush) {
            return;
        }
        exec(cmdPrefix + 'push ' + config['deploy'].repo + ' ' + config['deploy'].branch, function(err) {
            if (err) {
                console.error('Unable to push changes: ' + err);
                process.exit(1);
            } else {
                console.log('Site updated.');
            }
        });
    });
};

@@ -28,20 +28,26 @@
 uire('fs
+-extra
 ');%0A
-
 var exec
@@ -104,24 +104,82 @@
 re('path');%0A
+var ncp = require('ncp');%0Avar mkdirp = require('mkdirp');%0A
 var config =
@@ -1373,32 +1373,140 @@
 turn;%0A        %7D%0A
+        if (config%5B'deploy'%5D.s3 && config%5B'deploy'%5D.s3.enabled) %7B%0A            return deploy_s3();%0A        %7D%0A
         exec(cmd
@@ -1809,19 +1809,1768 @@
     %7D);%0A    %7D);%0A
-
 %7D;%0A
+%0Afunction deploy_s3() %7B%0A    var s3 = require('s3');%0A    var fs = require('fs-extra');%0A%0A    var client = s3.createClient(%7B%0A        maxAsyncS3: 20,%0A        s3RetryCount: 3,%0A        s3RetryDelay: 1000,%0A        multipartUploadThreshold: 20971520,%0A        multipartUploadSize: 15728640,%0A        s3Options: %7B%0A            accessKeyId: config%5B'deploy'%5D.s3.accessKeyId,%0A            secretAccessKey: config%5B'deploy'%5D.s3.accessKeySecret,%0A            region: config%5B'deploy'%5D.s3.region%0A        %7D%0A    %7D);%0A%0A    var ncpOpts = %7B%0A        filter: function(filepath) %7B%0A            var filename = path.basename(filepath);%0A            return filename.indexOf('.') !== 0;%0A        %7D%0A    %7D;%0A%0A    var dir = path.resolve(cwd + '/' + config%5B'dir'%5D.site);%0A    var destdir = path.resolve(cwd + '/.deploy');%0A    var s3prefix = config%5B'base'%5D.path === '/' ? '' : config%5B'base'%5D.path;%0A%0A    try %7B%0A        fs.removeSync(destdir);%0A    %7D catch (e) %7B%0A        //%0A    %7D%0A    mkdirp(destdir);%0A%0A    ncp(dir, destdir, ncpOpts, function(err) %7B%0A        var params = %7B%0A            localDir: destdir,%0A            deleteRemoved: true,%0A            s3Params: %7B%0A                Bucket: config%5B'deploy'%5D.s3.bucket,%0A                Prefix: s3prefix,%0A                ACL: 'public-read'%0A            %7D%0A        %7D;%0A        var uploader = client.uploadDir(params);%0A        uploader.on('error', function(err) %7B%0A            console.error(%22Error:%5Cn%22, err.stack);%0A        %7D);%0A        uploader.on('progress', function() %7B%0A            var p = Math.ceil(uploader.progressAmount / uploader.progressTotal * 100);%0A            if (p) %7B%0A                console.log(%22Progress: %22, p + '%25');%0A            %7D%0A        %7D);%0A        uploader.on('end', function() %7B%0A            console.log(%22Site updated.%22);%0A        %7D);%0A    %7D);%0A%0A%7D%0A

add deploy: s3