var colors = require('colors');
var shelljs = require('shelljs');

var fs = require('../../core/fs.js');

var GethCommands = require('./geth_commands.js');

var Blockchain = function(blockchainConfig, Client, env) {
  this.blockchainConfig = blockchainConfig;
  this.env = env || 'development';

  this.config = {
    geth_bin: this.blockchainConfig.geth_bin || 'geth',
    networkType: this.blockchainConfig.networkType || 'custom',
    genesisBlock: this.blockchainConfig.genesisBlock || false,
    datadir: this.blockchainConfig.datadir || false,
    mineWhenNeeded: this.blockchainConfig.mineWhenNeeded || false,
    rpcHost: this.blockchainConfig.rpcHost || 'localhost',
    rpcPort: this.blockchainConfig.rpcPort || 8545,
    rpcCorsDomain: this.blockchainConfig.rpcCorsDomain || false,
    networkId: this.blockchainConfig.networkId || 12301,
    port: this.blockchainConfig.port || 30303,
    nodiscover: this.blockchainConfig.nodiscover || false,
    mine: this.blockchainConfig.mine || false,
    account: this.blockchainConfig.account || {},
    whisper: (this.blockchainConfig.whisper === undefined) || this.blockchainConfig.whisper,
    maxpeers: ((this.blockchainConfig.maxpeers === 0) ? 0 : (this.blockchainConfig.maxpeers || 25)),
    bootnodes: this.blockchainConfig.bootnodes || "",
    rpcApi: (this.blockchainConfig.rpcApi || ['eth', 'web3', 'net']),
    vmdebug: this.blockchainConfig.vmdebug || false
  };

  this.client = new Client({config: this.config, env: this.env});
};

Blockchain.prototype.runCommand = function(cmd) {
  console.log(("running: " + cmd.underline).green);
  return shelljs.exec(cmd);
};

Blockchain.prototype.run = function() {
  console.log("===============================================================================".magenta);
  console.log("===============================================================================".magenta);
  console.log(("Embark Blockchain Using: " + this.client.name.underline).magenta);
  console.log("===============================================================================".magenta);
  console.log("===============================================================================".magenta);
  var address = this.initChainAndGetAddress();
  var mainCommand = this.client.mainCommand(address);
  this.runCommand(mainCommand);
};

Blockchain.prototype.initChainAndGetAddress = function() {
  var address = null, result;

  // ensure datadir exists, bypassing the interactive liabilities prompt.
  this.datadir = '.embark/' + this.env + '/datadir';
  fs.mkdirpSync(this.datadir);

  // copy mining script
  fs.copySync(fs.embarkPath("js"), ".embark/" + this.env + "/js", {overwrite: true});

  // check if an account already exists, create one if not, return address
  result = this.runCommand(this.client.listAccountsCommand());
  if (result.output === undefined || result.output === '' || result.output.indexOf("Fatal") >= 0) {
    console.log("no accounts found".green);
    if (this.config.genesisBlock) {
      console.log("initializing genesis block".green);
      result = this.runCommand(this.client.initGenesisCommmand());
    }

    result = this.runCommand(this.client.newAccountCommand());
    address = result.output.match(/{(\w+)}/)[1];
  } else {
    console.log("already initialized".green);
    address = result.output.match(/{(\w+)}/)[1];
  }

  return address;
};

var BlockchainClient = function(blockchainConfig, client, env) {
  if (client === 'geth') {
    return new Blockchain(blockchainConfig, GethCommands, env);
  } else {
    throw new Error('unknown client');
  }
};

module.exports = BlockchainClient;


@@ -180,37 +180,15 @@
 ion(
-blockchainConfig, Client, env
+options
 ) %7B%0A
@@ -213,16 +213,24 @@
 onfig = 
+options.
 blockcha
@@ -252,16 +252,24 @@
 s.env = 
+options.
 env %7C%7C '
@@ -281,16 +281,48 @@
 opment';
+%0A  this.client = options.client;
 %0A%0A  this
@@ -1468,17 +1468,25 @@
 t = new 
-C
+options.c
 lient(%7Bc
@@ -3514,16 +3514,35 @@
 ckchain(
+%7BblockchainConfig: 
 blockcha
@@ -3550,16 +3550,24 @@
 nConfig,
+ client:
  GethCom
@@ -3576,16 +3576,22 @@
 nds, env
+: env%7D
 );%0A  %7D e

simplify blockchain constructor