var conf = require('./conf.js');
var async = require('async');
var mkdirp = require('mkdirp');
var rimraf = require('rimraf');
var path = require('path');

async.series([
  setup,
  download,
  chmodChromeDr.bind(null, conf.chromeDr.path)
], end);

function setup(cb) {
  async.series([
    rimraf.bind(null, path.join(__dirname, '.selenium')),
    mkdirp.bind(null, path.dirname(conf.selenium.path))
  ], cb);
}

function download(cb) {
  async.parallel([
    installChromeDr.bind(null, conf.chromeDr.path, conf.chromeDr.v),
    installSelenium.bind(null, conf.selenium.path, conf.selenium.v)
  ], cb)
}

function end(err) {
  console.log('Installation finished');
  if (err) {
    throw err
  }
}

function installSelenium(to, version, cb) {
  var seleniumStandaloneUrl =
    'http://selenium-release.storage.googleapis.com/%s/selenium-server-standalone-%s.jar';

  var dl = require('util').format(seleniumStandaloneUrl,
    version.slice(0, version.lastIndexOf('.')),
    version);

  getDownloadStream(dl, function(err, stream) {
    if (err) {
      return cb(err);
    }

    stream
      .pipe(require('fs').createWriteStream(to))
      .once('error', cb.bind(null, new Error('Could not write to ' + to)))
      .once('finish', cb);
  });
}

function chmodChromeDr(where, cb) {
  console.log('chmod+x chromedriver');
  require('fs').chmod(where, 0755, cb);
}

function installChromeDr(to, version, cb) {
  var path = require('path');
  var util = require('util');

  var chromedriverUrl = 'http://chromedriver.storage.googleapis.com/%s/chromedriver_%s.zip';
  var platform = getChromeDriverPlatform();

  if(platform instanceof Error) {
    return cb(platform);
  }

  var dl = util.format(chromedriverUrl, version, platform);

  getDownloadStream(dl, function(err, stream) {
    if (err) {
      return cb(err);
    }

    var unzip = require('unzip');

    console.log('Unzipping ' + dl);

    stream
      .pipe(require('unzip').Parse())
      .once('entry', function(file) {
        file
          .pipe(require('fs').createWriteStream(to))
          .once('error', cb.bind(null, new Error('Could not write to ' + to)))
          .once('finish', cb)
      })
      .once('error', cb.bind(null, new Error('Could not unzip ' + dl)))
  })
}

function getDownloadStream(dl, cb) {
  var r =
    require('http')
      .request(dl, function(res) {
        console.log('Downloading ' + dl, res.statusCode);

        if (res.statusCode === 302 && res.headers.location) {
          r.abort();
          return getDownloadStream(res.headers.location, cb);
        }

        if (res.statusCode !== 200) {
          return cb(new Error('Could not download ' + dl));
        }

        cb(null, res);
      })
      .once('error', cb.bind(null, new Error('Could not download ' + dl)))

  // initiate request
  r.end();
}

function getChromeDriverPlatform() {
  var platform;

  if (process.platform === 'linux') {
    platform = 'linux' + ( process.arch === 'x64' ? '64' : '32' );
  } else if (process.platform === 'darwin') {
    platform = 'mac32';
  } else if (process.platform === 'win32') {
    platform = 'win32'
  } else {
    return new Error('Platform not supported');
  }

  return platform;
}
@@ -2276,24 +2276,327 @@
 m(dl, cb) %7B%0A
+  var proxy = process.env.HTTP_PROXY %7C%7C process.env.http_proxy;%0A%0A  if (proxy) %7B%0A    var regexp = /(https?:%5C/%5C/)?(%5B%5E:/%5D*)/;%0A    dl = %7B%0A      host: proxy.match(regexp)%5B2%5D,%0A      port: proxy.match(/:(%5Cd+)/)%5B1%5D %7C%7C 8080,%0A      path: dl,%0A      headers: %7B%0A        Host: dl.match(regexp)%5B2%5D%0A      %7D%0A    %7D;%0A  %7D%0A%0A
   var r =%0A  
@@ -2685,18 +2685,31 @@
 ing ' + 
+(
 dl
+.path %7C%7C dl)
 , res.st
@@ -2968,18 +2968,31 @@
 oad ' + 
+(
 dl
+.path %7C%7C dl)
 ));%0A    
@@ -3095,26 +3095,39 @@
 ownload ' + 
+(
 dl
+.path %7C%7C dl)
 )))%0A%0A  // in

use proxy settings if they exist