#!/opt/local/bin/node
var program = require('commander'),
    tsbs = require('./testswarm-browserstack');

program
    .version('0.0.1')
    .option('--killAll', 'kill all workers now')
    .option('--killWorker [workerid]', 'kill worker', parseInt)
    .option('--getNeeded', 'return the workers required by testswarm')
    .option('-k, --kill', 'if --run specified, kill workers if they are no longer needed.')  
    .option('-r, --run', 'start up workers required by browserstack')
    .option('-u, --user [username]', 'browserstack username', '')
    .option('-p, --pass [password]', 'browserstack password', '')
    .option('-s, --swarmUrl [url]', 'testswarm URL of getneeded call', '')
    .option('-w, --spawnUrl [url]', 'URL for browserstack workers to run', '')
    .option('-v, --verbose', 'print more info')
    .option('-t, --clientTimeout [min]', 'number of minuets to run each client (browserstack timeout)', parseInt)
    .parse(process.argv);
  
if(!process.argv[2]){
    console.log( program.helpInformation() );
    return;
}

tsbs.options(program);

if(program.getNeeded){
    if(!program.swarmUrl){
        console.log('please set --swarmUrl. stopping.');
        return;
    }
    tsbs.getNeeded(function(needed){
        console.log(needed);
    });
}

if(program.run || program.killWorker || program.killAll){
    if(!program.pass || !program.user){
        console.log('please set --user and --pass. stopping.');
        return;
    }
}

if(program.killAll){
    tsbs.killAll(program.killWorker);
}

if(program.killWorker){
    tsbs.killWorker(program.killWorker);
}

if(program.run){
    if(!program.swarmUrl || !program.spawnUrl){
        console.log('please set --swarmUrl and --spawnUrl. stopping.');
        return;
    }
    
    //set default timeout if not set
    program.clientTimeout = program.clientTimeout || 10;
    //convert timeout to min
    program.clientTimeout = program.clientTimeout * 60;
    tsbs.run();
}
@@ -405,18 +405,16 @@
 eeded.')
-  
 %0A    .op
@@ -950,18 +950,16 @@
 .argv);%0A
-  
 %0Aif(!pro
@@ -989,17 +989,16 @@
 ole.log(
- 
 program.
@@ -1014,17 +1014,16 @@
 mation()
- 
 );%0A    r
@@ -1738,20 +1738,16 @@
 ;%0A    %7D%0A
-    
 %0A    //s

clean up