'use strict';
var pkg = require('./package');

module.exports = function(config) {
  var browsers = require('./browsers');
  var launchers = Object.keys(browsers).reduce(function(launchers, browserType) {
    if (browserType === 'local') {
      return launchers;
    }

    Object.keys(browsers[browserType]).forEach(function(browserKey) {
      launchers[browserKey] = browsers[browserType][browserKey];
    });

    return launchers;
  }, {});

  var cfg = {
    basePath: '.',

    browserDisconnectTimeout: 10000,

    browserDisconnectTolerance: 3,

    browserify: {
      debug: true,
      watch: true,
      transform: [
        'envify'
      ]
    },

    browserNoActivityTimeout: 240000,

    // Inspired by Angular's karma config as recommended by Sauce Labs
    captureTimeout: 0,

    colors: !process.env.XUNIT,

    customLaunchers: launchers,

    files: [
      'test/unit/spec/**/*.js',
      'test/integration/spec/**/*.js'
    ],

    frameworks: [
      'browserify',
      'mocha'
    ],

    hostname: '127.0.0.1',

    client: {
      mocha: {
        retries: (process.env.JENKINS || process.env.CI) ? 1 : 0
      }
    },

    mochaReporter: {
      // Hide the skipped tests on jenkins to more easily see which tests failed
      ignoreSkipped: true
    },

    port: process.env.KARMA_PORT || 9001,

    preprocessors: {
      'src/**/*.js': ['browserify'],
      'test/unit/spec/**/*.js': ['browserify'],
      'test/integration/spec/**/*.js': ['browserify']
    },

    proxies: {
      '/fixtures/': 'http://127.0.0.1:' + process.env.FIXTURE_PORT + '/',
      '/upload': 'http://127.0.0.1:' + process.env.FIXTURE_PORT + '/upload'
    },

    reporters: [
      'mocha',
      'saucelabs'
    ],

    sauceLabs: {
      build: process.env.BUILD_NUMBER || ('local-' + process.env.USER + '-' + Date.now()),
      startConnect: false,
      testName: pkg.name + '(karma)',
      tunnelIdentifier: process.env.SC_TUNNEL_IDENTIFIER
    },

    singleRun: true
  };

  if (process.env.COVERAGE && process.env.COVERAGE !== 'undefined') {
    cfg.coverageReporter = {
      reporters: [{
        type: 'json',
        dir: 'reports/coverage/legacy'
      }]
    };

    cfg.browserify.transform.push('browserify-istanbul');

    cfg.reporters.push('coverage');
  }

  if (process.env.XUNIT) {
    cfg.junitReporter = {
      outputFile: 'karma-legacy.xml',
      outputDir: process.env.XUNIT_DIR || 'reports/junit',
      suite: 'karma-legacy',
      useBrowserName: true
    };

    cfg.reporters.push('junit');
  }

  config.set(cfg);
};

@@ -6,16 +6,45 @@
 strict';
+%0A%0Avar path = require('path');
 %0Avar pkg
@@ -519,32 +519,326 @@
 wser
-DisconnectTimeout: 10000
+ConsoleLogOptions: %7B%0A      path: process.env.CIRCLE_ARTIFACTS ? path.join(process.env.CIRCLE_ARTIFACTS, 'karma.legacy.log') : 'karma.log',%0A      terminal: !process.env.CI%0A    %7D,%0A%0A    browserDisconnectTimeout: 10000,%0A%0A    browsers: process.env.SC_TUNNEL_IDENTIFIER ? Object.keys(launchers) : Object.keys(browsers.local)
 ,%0A%0A 
@@ -1452,16 +1452,40 @@
  ? 1 : 0
+,%0A        timeout: 30000
 %0A      %7D
@@ -1639,16 +1639,25 @@
   port: 
+parseInt(
 process.
@@ -1670,16 +1670,17 @@
 RMA_PORT
+)
  %7C%7C 9001
@@ -2057,298 +2057,157 @@
 cha'
-,%0A      'saucelabs'%0A    %5D,%0A%0A    sauceLabs: %7B%0A      build: process.env.BUILD_NUMBER %7C%7C ('local-' + process.env.USER + '-' + Date.now()),%0A      startConnect: false,%0A      testName: pkg.name + '(karma)',%0A      tunnelIdentifier: process.env.SC_TUNNEL_IDENTIFIER%0A    %7D,%0A%0A    singleRun: true
+%0A    %5D,%0A%0A    singleRun: true%0A  %7D;%0A%0A  // The mocha reporter doesn't honor browserConsoleLogOptions%0A  if (process.env.CI) %7B%0A    cfg.reporters = %5B%5D;
 %0A  %7D
-;
 %0A%0A  
@@ -2497,24 +2497,351 @@
 age');%0A  %7D%0A%0A
+  if (process.env.SC_TUNNEL_IDENTIFIER) %7B%0A    cfg.sauceLabs = %7B%0A      build: process.env.BUILD_NUMBER %7C%7C ('local-' + process.env.USER + '-' + Date.now()),%0A      startConnect: false,%0A      testName: pkg.name + ' (karma)',%0A      tunnelIdentifier: process.env.SC_TUNNEL_IDENTIFIER%0A    %7D;%0A    cfg.reporters.push('saucelabs');%0A  %7D%0A%0A
   if (proces

remove log output system-err, system-out from karma legacy junit output