var path = require('path');
var minimist = require('minimist');
var runSeries = require('run-series');
var prependFile = require('prepend-file');

var constants = require('./util/constants');
var common = require('./util/common');
var _bundle = require('./util/browserify_wrapper');

var header = constants.licenseDist + '\n';
var allTransforms = constants.allTransforms;
var allTraces = constants.allTraces;
var mainIndex = constants.mainIndex;

function createList(outList, inList, allList, type) {
    for(var i = 0; i < inList.length; i++) {
        var t = inList[i];
        if(
            outList.indexOf(t) === -1 // not added before
        ) {
            if(allList.indexOf(t) === -1) {
                console.error(t, 'is not a valid ' + type + '!', 'Valid ' + type + 's are:', allList);
            } else {
                outList.push(t);
            }
        }
    }

    return outList.sort();
}

function isFalse(a) {
    return (
        a === 'none' ||
        a === 'false'
    );
}

function inputArray(a, dflt) {
    dflt = dflt.slice();

    return (
        isFalse(a) ? [] :
            !a || a === 'all' ? dflt :
                a.split(',')
    );
}

if(process.argv.length > 2) {
    // command line

    var args = minimist(process.argv.slice(2), {});

    // parse arguments
    var out = args.out ? args.out : 'custom';
    var traces = inputArray(args.traces, allTraces);
    var transforms = inputArray(args.transforms, allTransforms);

    var opts = {
        traceList: createList(['scatter'], traces, allTraces, 'trace'),
        transformList: createList([], transforms, allTransforms, 'transform'),

        name: out,
        index: path.join(constants.pathToLib, 'index-' + out + '.js'),
        distMin: path.join(constants.pathToDist, 'plotly-' + out + '.min.js')
    };

    console.log(opts);

    opts.calendars = true;

    var tasks = [];

    partialBundle(tasks, opts);

    runSeries(tasks, function(err) {
        if(err) throw err;
    });
}

// Browserify the plotly.js partial bundles
function partialBundle(tasks, opts) {
    var name = opts.name;
    var index = opts.index;
    var dist = opts.dist;
    var distMin = opts.distMin;
    var traceList = opts.traceList;
    var transformList = opts.transformList;
    var calendars = opts.calendars;

    tasks.push(function(done) {
        var partialIndex = mainIndex;

        var all = ['calendars'].concat(allTransforms).concat(allTraces);
        var includes = (calendars ? ['calendars'] : []).concat(transformList).concat(traceList);
        var excludes = all.filter(function(e) { return includes.indexOf(e) === -1; });

        excludes.forEach(function(t) {
            var WHITESPACE_BEFORE = '\\s*';
            // remove require
            var newCode = partialIndex.replace(
                new RegExp(
                    WHITESPACE_BEFORE +
                    'require\\(\'\\./' + t + '\'\\)' +
                    (t === 'calendars' ? '' : ','), // there is no comma after calendars require
                'g'), ''
            );

            // test removal
            if(newCode === partialIndex) {
                console.error('Unable to find and drop require for ' + t);
                throw 'Error generating index for partial bundle!';
            }

            partialIndex = newCode;
        });

        common.writeFile(index, partialIndex, done);
    });

    tasks.push(function(done) {
        var bundleOpts = {
            standalone: 'Plotly',
            pathToMinBundle: distMin
        };

        _bundle(index, dist, bundleOpts, function() {
            var headerDist = header.replace('plotly.js', 'plotly.js (' + name + ')');
            var headerDistMin = header.replace('plotly.js', 'plotly.js (' + name + ' - minified)');

            if(dist) prependFile(dist, headerDist, common.throwOnError);
            if(distMin) prependFile(distMin, headerDistMin, common.throwOnError);

            done();
        });
    });
}

module.exports = partialBundle;

@@ -997,24 +997,96 @@
 '%0A    );%0A%7D%0A%0A
+function inputBoolean(a, dflt) %7B%0A    return !a ? dflt : !isFalse(a);%0A%7D%0A%0A
 function inp
@@ -1100,24 +1100,24 @@
 (a, dflt) %7B%0A
-
     dflt = d
@@ -1374,16 +1374,75 @@
 guments%0A
+    var unminified = inputBoolean(args.unminified, false);%0A
     var 
@@ -1859,26 +1859,151 @@
 js')
-,%0A        
+%0A    %7D;%0A%0A    if(unminified) %7B%0A        opts.dist = path.join(constants.pathToDist, 'plotly-' + out + '.js');%0A    %7D else %7B%0A        opts.
 distMin
-:
+ =
  pat
@@ -2055,31 +2055,31 @@
 + '.min.js')
+;
 %0A    %7D
-;
 %0A%0A    consol

add unminified option