var webpack = require('webpack');

module.exports = function (config) {
  // Browsers to run on BrowserStack
  var customLaunchers = {
    BS_Chrome: {
      base: 'BrowserStack',
      os: 'OS X',
      os_version: 'Yosemite',
      browser: 'chrome',
      browser_version: '39.0',
    },
    BS_Firefox: {
      base: 'BrowserStack',
      os: 'OS X',
      os_version: 'Yosemite',
      browser: 'firefox',
      browser_version: '32.0',
    },
    BS_Safari: {
      base: 'BrowserStack',
      os: 'OS X',
      os_version: 'Yosemite',
      browser: 'safari',
      browser_version: '8.0',
    },
//    BS_InternetExplorer_9: {
//      base: 'BrowserStack',
//      os: 'Windows',
//      os_version: '7',
//      browser: 'ie',
//      browser_version: '9.0',
//    },
    BS_InternetExplorer_10: {
      base: 'BrowserStack',
      os: 'Windows',
      os_version: '8',
      browser: 'ie',
      browser_version: '10.0',
    },
    BS_InternetExplorer_11: {
      base: 'BrowserStack',
      os: 'Windows',
      os_version: '8.1',
      browser: 'ie',
      browser_version: '11.0',
    },
    BS_MobileSafari: {
      base: 'BrowserStack',
      os: 'iOS',
      os_version: '8.3',
      device: 'iPhone 6'
    },
  };

  config.set({
    customLaunchers: customLaunchers,

    browsers: [ 'Chrome' ],
    browserNoActivityTimeout: 30000,
    captureTimeout: 30000,
    frameworks: [ 'mocha' ],
    reporters: [ 'mocha' ],

    files: [
      'tests.webpack.js'
    ],

    preprocessors: {
      'tests.webpack.js': [ 'webpack', 'sourcemap' ]
    },

    webpack: {
      devtool: 'inline-source-map',
      module: {
        loaders: [
          { test: /\.js$/, exclude: /node_modules/, loader: 'babel' }
        ]
      },
      plugins: [
        new webpack.DefinePlugin({
          'process.env.NODE_ENV': JSON.stringify('test')
        })
      ]
    },

    webpackServer: {
      noInfo: true
    }
  });

  if (process.env.USE_CLOUD) {
    config.browsers = Object.keys(customLaunchers);
    config.reporters = [ 'dots' ];

    // Karma (with socket.io 1.x) buffers by 50 and 50 tests can take a long time on IEs
    config.browserNoActivityTimeout = 120000;

    // Allocating a browser in Sauce Labs can take a while (e.g. if we are out of
    // capacity and need to wait for another build to finish) so captureTimeout
    // typically kills an in-queue-pending request, which makes no sense.
    config.captureTimeout = 0;

    config.browserStack = {
      username: process.env.BROWSER_STACK_USERNAME,
      accessKey: process.env.BROWSER_STACK_ACCESS_KEY,
      startTunnel: true,
    };
  } else if (process.env.TRAVIS) {
    config.browsers = Object.keys(customLaunchers);
    config.reporters = [ 'dots' ];

    // Karma (with socket.io 1.x) buffers by 50 and 50 tests can take a long time on IEs
    config.browserNoActivityTimeout = 120000;

    // Allocating a browser in Sauce Labs can take a while (e.g. if we are out of
    // capacity and need to wait for another build to finish) so captureTimeout
    // typically kills an in-queue-pending request, which makes no sense.
    config.captureTimeout = 0;

    var buildLabel = 'TRAVIS #' + process.env.TRAVIS_BUILD_NUMBER + ' (' + process.env.TRAVIS_BUILD_ID + ')';

    config.browserStack = {
      username: process.env.BROWSER_STACK_USERNAME,
      accessKey: process.env.BROWSER_STACK_ACCESS_KEY,
      startTunnel: true,
      project: 'history',
      build: buildLabel,
      name: process.env.TRAVIS_JOB_NUMBER,
    };

    config.singleRun = true;
  }
};

@@ -1311,72 +1311,8 @@
  %5D,%0A
-    browserNoActivityTimeout: 30000,%0A    captureTimeout: 30000,%0A
     
@@ -1984,1099 +1984,291 @@
     
-// Karma (with socket.io 1.x) buffers by 50 and 50 tests can take a long time on IEs%0A    config.browserNoActivityTimeout = 120000;%0A%0A    // Allocating a browser in Sauce Labs can take a while (e.g. if we are out of%0A    // capacity and need to wait for another build to finish) so captureTimeout%0A    // typically kills an in-queue-pending request, which makes no sense.%0A    config.captureTimeout = 0;%0A%0A    config.browserStack = %7B%0A      username: process.env.BROWSER_STACK_USERNAME,%0A      accessKey: process.env.BROWSER_STACK_ACCESS_KEY,%0A      startTunnel: true,%0A    %7D;%0A  %7D else if (process.env.TRAVIS) %7B%0A    config.browsers = Object.keys(customLaunchers);%0A    config.reporters = %5B 'dots' %5D;%0A%0A    // Karma (with socket.io 1.x) buffers by 50 and 50 tests can take a long time on IEs%0A    config.browserNoActivityTimeout = 120000;%0A%0A    // Allocating a browser in Sauce Labs can take a while (e.g. if we are out of%0A    // capacity and need to wait for another build to finish) so captureTimeout%0A    // typically kills an in-queue-pending request, which makes no sense.%0A    config.captureTimeout = 0
+config.browserStack = %7B%0A      username: process.env.BROWSER_STACK_USERNAME,%0A      accessKey: process.env.BROWSER_STACK_ACCESS_KEY,%0A      startTunnel: true,%0A    %7D;%0A  %7D else if (process.env.TRAVIS) %7B%0A    config.browsers = Object.keys(customLaunchers);%0A    config.reporters = %5B 'dots' %5D
 ;%0A%0A 

Delete a bunch of timeout stuff