/*!
 * ndev-framework
 * Copyright(c) 2016-2017 Javanile.org
 * MIT Licensed
 */

var base = require("path").basename;
var exec = require("child_process").exec;
var util = require("./util");

module.exports = {
    /**
     *
     *
     */
    cwd: process.cwd(),

    /**
     *
     * @param args
     */
    cmdTest: function (args, callback) {
        return this.cmdWithNdevModule(
            "test", "testing: ${ndev_module}", args, callback
        );
    },

    /**
     *
     *
     * @param args
     */
    cmdClone: function (args, callback) {
        if (!args[0]) {
            console.error("(ndev) Required repository url or package name.");
            return;
        }

        //
        /*
        if (!args[1].match(/^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/)) {
            var pack = args[1];
            var name = args[2] ? args[2] : base(repo, ".git");

            util.exec("clone", [this.cwd, repo, name], function(resp) {
                callback(util.log(msg + "\n" + resp.trim(), {
                    ndev_module: ndev_module
                }));
            });

            exec(__dirname + "/../exec/ndev-clone-pack.sh " + path + " " + repo + " " + name,
                function (error, stdout, stderr) {
                    console.log("(ndev)", stderr.trim());
                }
            );
        }
        */

        console.log(args);

        var repo = args[0].trim();
        var name = args[1] ? args[1] : base(repo, ".git");

        console.log(this.cwd);

        util.exec("clone-repo", [this.cwd, repo, name], function(resp) {
            callback(util.log(resp.trim()));
        });
    },

    /**
     *
     *
     * @param args
     */
    cmdMount: function (args) {
        if (!args[1]) {
            console.error("(ndev) Required node module to mount.");
            return;
        }

        // cant start

        //
        var node = args[1].trim();
        exec(__dirname + "/../exec/ndev-mount.sh " + path + " " + node,
            function (error, stdout, stderr) {
                i(stderr.trim());
            }
        );
    },

    /**
     *
     * @param args
     */
    cmdInstall: function (args) {
        if (!args[1]) {
            return e("Required package name.");
        }

        //
        console.log("(ndev) Please wait during install...");
        exec(join(__dirname, + "/../exec/ndev-install.sh ")  + path + " " + args.slice(1).join(" "),
            function (error, stdout, stderr) {
                console.log(!stderr ? stdout.trim() : stderr.trim());
            }
        );
    },

    /**
     *
     * @param args
     */
    cmdFreeze: function (args, callback) {
        return this.cmdWithNdevModule(
            "freeze", "freeze", args, callback
        );
    },

    /**
     *
     * @param args
     */
    cmdPublish: function (args) {
        if (!args[1]) {
            return e("Required ndev module to publish.");
        }

        i("Please wait during publish...");
        var name = args[1];
        exec(__dirname + "/../exec/ndev-publish.sh " + path + " " + name,
            function (error, stdout, stderr) {
                console.log(stderr.trim(), stdout);
            }
        );
    },

    /**
     *
     * @param args
     */
    cmdCommit: function (args, callback) {
        return this.cmdWithNdevModule(
            "commit", "commit module: ${ndev_module}", args, callback
        );
    },

    /**
     *
     * @param cmd
     * @param args
     * @param callback
     */
    cmdWithNdevModule: function (cmd, msg, args, callback) {
        if (!args[0]) { return util.err("&ndev-required"); }

        var ndev_module = args[0].trim();

        util.exec(cmd, [this.cwd, ndev_module], function(resp) {
            callback(util.log(msg + "\n" + resp.trim(), {
                ndev_module: ndev_module
            }));
        });
    }
}
@@ -545,32 +545,58 @@
 gs, callback) %7B%0A
+        var self = this;%0A%0A
         if (!arg
@@ -1405,36 +1405,8 @@
 */%0A%0A
-        console.log(args);%0A%0A
     
@@ -1467,19 +1467,10 @@
 %5B1%5D 
-? args%5B1%5D :
+%7C%7C
  bas
@@ -1491,40 +1491,8 @@
 );%0A%0A
-        console.log(this.cwd);%0A%0A
     
@@ -1515,21 +1515,16 @@
 lone
--repo%22, %5Bthis
+%22, %5Bself
 .cwd
@@ -1538,32 +1538,33 @@
  name%5D, function
+ 
 (resp) %7B%0A       

version 0.0.16