#!/usr/bin/env node

var fs = require('fs');
var _ = require('lodash');
var replaceStream = require('replacestream');

var TEST_DIR = './test';
var TEST_RUNNER = './test-runner.mustache';
var TEST_TESTEM = './testem.json';
var argv = require('minimist')(process.argv.slice(2));
var opts = {};

function copy(src, dest, opts) {
  if (opts.force || !fs.existsSync(dest)) {
    fs.createReadStream(src)
      .pipe(replaceStream('%Framework%', opts.framework))
      .pipe(fs.createWriteStream(dest));
  }
}

function ensureDir(dir) {
  if (!fs.existsSync(dir)) {
    fs.mkdirSync(dir);
  }
}

_.defaults(opts, argv, {
  force: false,
  framework: 'mocha'
});

ensureDir(TEST_DIR);
copy(__dirname + '/../frameworks/' + opts.framework + '/basic.js', './test/basic.js', opts);
copy(__dirname + '/../package/' + TEST_RUNNER, TEST_RUNNER, opts);
copy(__dirname + '/../package/' + TEST_TESTEM, TEST_TESTEM, opts);

var pjson = JSON.parse(fs.readFileSync('./package.json', 'utf-8'));

if (!pjson.jspm) {
  pjson.jspm = {};
}

if (!pjson.jspm.overrides) {
  pjson.jspm.overrides = {};
}

pjson.jspm.overrides = {
  'github:OrKoN/jspm-testem@master': {
    'dependencies': require('../frameworks/' + opts.framework + '/dependencies')
  }
};

fs.writeFileSync('./package.json', JSON.stringify(pjson, null, 2));

var jspm = require('jspm');
jspm
  .install('jspm-testem',
    'github:OrKoN/jspm-testem@master',
    { dev: true });

@@ -1071,16 +1071,107 @@
  %7B%7D;%0A%7D%0A%0A
+var frameworkDependencies = require('../frameworks/' + opts.framework + '/dependencies');%0A%0A
 pjson.js
@@ -1250,55 +1250,18 @@
 s': 
-require('../frameworks/' + opts.framework + '/d
+frameworkD
 epen
@@ -1267,18 +1267,16 @@
 ndencies
-')
 %0A  %7D%0A%7D;%0A
@@ -1461,10 +1461,275 @@
  true %7D)
+%0A  .then(() =%3E console.log('Installed jspm-testem with ' + opts.framework + ' support'))%0A  .then(() =%3E jspm%0A  .install(frameworkDependencies, %7B dev: true %7D))%0A  .catch((err) =%3E console.error('Failed to install jspm-testem with ' + opts.framework + ' support', err))
 ;%0A
+%0A

Install dev deps