/*!
 * Module dependencies.
 */

var Command = require('./util/command'),
    project = require('./util/project'),
    platforms = require('./util/platform'),
    cordova = require('cordova'),
    util = require('util');

/*!
 * Command setup.
 */

module.exports = {
    create: function(phonegap) {
        return new BuildCommand(phonegap);
    }
};

function BuildCommand(phonegap) {
    return Command.apply(this, arguments);
}

util.inherits(BuildCommand, Command);

/**
 * Build an App.
 *
 * Automatically detects if local SDK exists.
 *  - true: build application on the local system.
 *  - false: build the application remotely.
 *
 * Options:
 *
 *   - `options` {Object}
 *     - `platforms` {Array} is a list of platforms (limited to 1).
 *   - [`callback`] {Function} is triggered after completion.
 *     - `e` {Error} is null unless there is an error.
 *
 * Returns:
 *
 *   {PhoneGap} for chaining.
 */

BuildCommand.prototype.run = function(options, callback) {
    // require options
    if (!options) throw new Error('requires option parameter');
    if (!options.platforms) throw new Error('requires option.platforms parameter');

    // optional callback
    callback = callback || function() {};

    // build app
    this.execute(options, callback);

    return this.phonegap;
};

/*!
 * Execute.
 */

BuildCommand.prototype.execute = function(options, callback) {
    var self = this,
        platform = platforms.names(options.platforms)[0],
        env = '';

    // change to project directory and delegate errors
    if (!project.cd({ emitter: self.phonegap, callback: callback })) return;

    // detect the platform support
    self.phonegap.emit('log', 'detecting', platform.human, 'SDK environment...');

    cordova.raw.platform.supports('.', platform.local).then(function() {
        // invoke local or remote build
        var env = 'local';
        self.phonegap.emit('log', 'using the', env, 'environment');
        self.phonegap[env].build(options, callback);
    }, function (e) {
        var env = 'remote';
        // emit a non-exiting message to stderr, incase user wants to react to local build failure
        self.phonegap.emit('error',e);
        self.phonegap.emit('log', 'using the', env, 'environment');
        self.phonegap[env].build(options, callback);
    });
};

@@ -215,16 +215,227 @@
 til');%0A%0A
+%0A/**%0A * localization of human readable log messages%0A * eventual migration to its own module%0A */%0Avar PG_STR = %7B%0A    'msgdet' : %5B'detecting', 'SDK environment...'%5D,%0A    'useenv' : %5B'using the', 'environment'%5D%0A%7D;%0A%0A
 /*!%0A * C
@@ -1889,27 +1889,32 @@
 ('log', 
-'detecting'
+PG_STR.msgdet%5B0%5D
 , platfo
@@ -1923,36 +1923,32 @@
 .human, 
-'SDK environment...'
+PG_STR.msgdet%5B1%5D
 );%0A%0A    
@@ -2121,39 +2121,47 @@
 g', 
-'using the', env
+PG_STR.useenv%5B0%5D
 , 
-'
 env
-ironment'
+, PG_STR.useenv%5B1%5D
 );%0A 
@@ -2396,16 +2396,17 @@
 'error',
+ 
 e);%0A    
@@ -2439,39 +2439,47 @@
 g', 
-'using the', env, 'environment'
+PG_STR.useenv%5B0%5D, env, PG_STR.useenv%5B1%5D
 );%0A 

refactor of build log messages