'use strict';

var shell = require('shelljs'),
    fs = require('fs'),
    config,
    methods = module.exports;


/**
 * Parse Config
 * @use find a config file and try and parse it if available
 * @params  _ {object} settings object
 * @return {object} || {boolean}
 */
methods.parseConfig = function(_)
{
    if (_.verbose || _.debug) {
        console.log(_.inform('Attempting to parse a remotee-sync.json '+
                           'config file'));
    }
    //is there is a config file or command line args
    var configLocation = shell.exec('find . -maxdepth 4 -name ' +
                    _.configFile, {silent:true}).output ?
                    shell.exec('find . -maxdepth 4 -name ' +
                    _.configFile,
                    {silent:true}).output.replace(/[\n\t\r]/g,'') :
                    false;
    if (configLocation) {
        try {
            config = JSON.parse(fs.readFileSync(configLocation));
        } catch(e) {
            console.log(_.error('There was an issue reading your config file.'+
                        ' Please ensure it is proper JSON'));
            if (_.debug) {
                console.log(_.inform('DEBUG: the error output is '+ e));
            }
            process.exit(0);
        }
    } else{
        config = false;
        if (_.verbose || _.debug) {
            console.log(_.inform('No remotee-sync.json file was found'));
        }
    }

    return config;
};

/**
 * Parse SSH
 * @use gather ssh info given env or ssh cli or config.ssh object, required
 * @params  _ {object} settings object
 *          args {object}
 * @return {string} ssh login creds, sends back the modified _ object
 */
methods.parseSSH = function(_, args)
{
    if (_.verbose) {
        console.log(_.inform('Attemtping to parse SSH settings'));
    }
    //grab cli ssh argument
    if (args.ssh) {
        _.ssh = args.ssh;

        return _.ssh;
    }
    _.env = args.env ? args.env : false;
    if (!_.env || !_.config.ssh) {
        console.log(_.error('You must provide ssh information to correctly '+
                            'connect to a remote server'));
        process.exit(0);
    }
    _.ssh = config.ssh[_.env];

    return _.ssh;
};


@@ -298,17 +298,17 @@
 ction(_)
-%0A
+ 
 %7B%0A    if
@@ -1699,17 +1699,17 @@
 _, args)
-%0A
+ 
 %7B%0A    if
@@ -2194,12 +2194,1601 @@
  _.ssh;%0A%7D;%0A%0A
+%0A/**%0A * Fill Commands%0A * @use fill the command variables based on connection types%0A * @return void -- modify program variables%0A */%0Amethods.fillCommands = function(_)%7B%0A    var sshCmd = 'ssh '+ _.verbose + _.ssh + ' mysqldump '+%0A        '--default-character-set=utf8 '+ _.verbose;%0A    var mampCmd = _.mampPath + 'mysql '+ _.verbose;%0A    if (_.multiple === undefined) %7B%0A        //set base and import commands%0A        _.baseCmd = sshCmd + '-h '+ _.connection.hostname +%0A            ' -u '+ _.connection.username +' -p'+ _.connection.password + ' ' +%0A            _.connection.database;%0A        _.importCmd = _.mampCmd + '-h '+ _.connection.hostname +%0A            ' -u '+ _.connection.username +' -p'+ _.connection.password + ' ' +%0A            _.connection.database;%0A%0A    %7D%0A%0A    if (_.multiple) %7B%0A        _.baseCmd = sshCmd + '-h '+ _.connection%5B_.env%5D.hostname +%0A            ' -u '+ _.connection%5B_.env%5D.username +%0A            ' -p'+ _.connection%5B_.env%5D.password + ' ' +%0A            _.connection%5B_.env%5D.database;%0A        _.importCmd = mampCmd + '-h '+ _.connection.local.hostname +%0A            ' -u '+ _.connection.local.username +%0A            ' -p'+ _.connection.local.password + ' ' +%0A            _.connection.local.database;%0A    %7D%0A%0A    //chain save command and run import later, or run right away%0A    _.command = _.save ? _.baseCmd + ' %3E '+ _.location + '/' + _.dumpName :%0A        _.baseCmd + ' %7C '+ _.importCmd;%0A%0A    if (_.verbose %7C%7C _.debug) %7B%0A        console.log(_.inform('The following command will be run now: '+%0A                             _.command));%0A    %7D%0A%0A    return _.command;%0A%7D;%0A

add fillCommands function