#!/usr/bin/env node
/*
 =============================================================
 = Foundation Utimate                                         =
 = http://github.com/relution-developments/foundation-remix  =
 = --------------------------------------------------------- =
 = Copyright (c) 2016 - Relution Developments Ltd            =
 = Licensed under the MIT license.                           =
 = @author JayDemitri                                        =
 =============================================================
 */

// Variables - Deps
var path        = require("path");
var nopt        = require("nopt");
var color       = require("cli-color");
var updates     = require("update-notifier");
// Variables - Configs
var cwd         = process.cwd();
var pkg         = require(path.join(cwd, "package.json"));
// Variables - Libs
var fu          = require("./cmds.js");
// CliOptions
var opts        = {
    "version": String,
    "global": Boolean
};
// CliShorthands
var shOpts      = {
    "v": "--version",
    "g": ["--global", "true"]
};
// Parse our options
var parsed = nopt(opts,shOpts);
// cmd.args contains basic nodeJs like build and install
// cmd.flags contains options like --silent --logs
var cmd = {
    args: parsed.argv.remain,
    flags: parsed
};
// Check for updates Once a day
// Will check weekly once stable
var notifier = updates({
    packageName: pkg.name,
    packageVersion: pkg.version
});
notifier.notify();
// =============================================================================================
// DECIDE WHAT TO DO NEXT
// =============================================================================================
// If no arguments are given
if(typeof cmd.args[0] === "undefined"){
    // If the version option was given then lets give them that back
    if(typeof cmd.flags.version !== "undefined"){
        process.stdout.write("FoundationUltimate - Version: " + pkg.version + "\n");
    }else{
        // Otherwise will just show the help page
        fu.help();
    }
}
// If arguments are given
else{
    // But doesnt exist, lets show the help (Which displays available options)
    if(typeof  fu[cmd.args[0]] === "undefined"){
        process.stdout.write(
            color.redBright(
                "------------------------------------------------------------------------------" +
                "\n                          Function: " + cmd.args[0] + " Not Found !       \n" +
                "------------------------------------------------------------------------------"
            ));
        // Otherwise will just show the help page
        fu.help();
    }
    // Otherwise run it
    else{
        // Every command function is passed secondary commands and options
        // so if the user types "foundationUltimate install bower --dev false"
        // bower is the secondary command and --dev is an option
        fu[cmd.args[0]](cmd.args.slice(1), cmd.flags);
    }
}
//Console Log for testing
//console.log("Foundation Ultimate | v:" + pkg.version);
//console.log(cmd);
