var path = require('path');
var fs = require('fs');

var prependFile = require('prepend-file');
var falafel = require('falafel');
var glob = require('glob');

var constants = require('./util/constants');

updateHeadersInSrcFiles();

// add headers to dist files

var pathsDist = [
    constants.pathToPlotlyDistMin,
    constants.pathToPlotlyDist,
    constants.pathToPlotlyDistWithMeta,
    constants.pathToPlotlyGeoAssetsDist
];

constants.partialBundleNames.forEach(function(name) {
    var pathToBundle = path.join(constants.pathToDist, 'plotly-' + name + '.js'),
        pathToMinBundle = path.join(constants.pathToDist, 'plotly-' + name + '.min.js');

    pathsDist.push(pathToBundle, pathToMinBundle);
});

function headerLicense(path) {
    prependFile(path, constants.licenseDist + '\n', function(err) {
        if(err) throw err;
    });
}

// add or update header to src/ lib/ files
function updateHeadersInSrcFiles() {
    var srcGlob = path.join(constants.pathToSrc, '**/*.js');
    var libGlob = path.join(constants.pathToLib, '**/*.js');
pathsDist.forEach(headerLicense);

    // remove leading '/*' and trailing '*/' for comparison with falafel output
    var licenseSrc = constants.licenseSrc;
    var licenseStr = licenseSrc.substring(2, licenseSrc.length - 2);

    glob('{' + srcGlob + ',' + libGlob + '}', function(err, files) {
        files.forEach(function(file) {
            fs.readFile(file, 'utf-8', function(err, code) {

                // parse through code string while keeping track of comments
                var comments = [];
                falafel(code, {onComment: comments, locations: true}, function() {});

                var header = comments[0];

                // error out if no header is found
                if(!header || header.loc.start.line > 1) {
                    throw new Error(file + ' : has no header information.');
                }

                // if header and license are the same, do nothing
                if(isCorrect(header)) return;

                // if header and license only differ by date, update header
                else if(hasWrongDate(header)) {
                    var codeLines = code.split('\n');

                    codeLines.splice(header.loc.start.line - 1, header.loc.end.line);

                    var newCode = licenseSrc + '\n' + codeLines.join('\n');

                    fs.writeFile(file, newCode, function(err) {
                        if(err) throw err;
                    });
                }
                else {
                    // otherwise, throw an error
                    throw new Error(file + ' : has wrong header information.');
                }
            });
        });
    });

    function isCorrect(header) {
        return (header.value === licenseStr);
    }

    function hasWrongDate(header) {
        var regex = /Copyright 20[0-9][0-9]-20[0-9][0-9]/g;

        return (header.value.replace(regex, '') === licenseStr.replace(regex, ''));
    }
}

@@ -198,16 +198,49 @@
 nts');%0A%0A
+// main%0AaddHeadersInDistFiles();%0A
 updateHe
@@ -288,17 +288,239 @@
 t files%0A
-%0A
+function addHeadersInDistFiles() %7B%0A    function _prepend(path, header) %7B%0A        prependFile(path, header + '%5Cn', function(err) %7B%0A            if(err) throw err;%0A        %7D);%0A    %7D%0A%0A    // add header to main dist bundles%0A    
 var path
@@ -529,16 +529,20 @@
 ist = %5B%0A
+    
     cons
@@ -568,16 +568,20 @@
 istMin,%0A
+    
     cons
@@ -604,24 +604,28 @@
 lyDist,%0A    
+  
+  
 constants.pa
@@ -648,16 +648,20 @@
 thMeta,%0A
+    
     cons
@@ -696,40 +696,28 @@
 ist%0A
-%5D;%0A%0Aconstants.partialBundleNames
+    %5D;%0A    pathsDist
 .for
@@ -734,20 +734,20 @@
 ion(
-name
+path
 ) %7B%0A    
 var 
@@ -746,164 +746,286 @@
     
-var pathToBundle = path.join(constants.pathToDist, 'plotly-' + name + '.js'),%0A        pathToMinBundle = path.join(constants.pathToDist, 'plotly-' + 
+    _prepend(path, constants.licenseDist);%0A    %7D);%0A%0A    // add header and bundle name to partial bundle%0A    constants.partialBundlePaths.forEach(function(pathObj) %7B%0A        var headerDist = constants.licenseDist%0A            .replace('plotly.js', 'plotly.js (' + pathObj.
 name + '
 .min
@@ -1024,200 +1024,238 @@
  + '
-.min.js
+)
 ');%0A
-%0A
     
-pathsDist.push(pathToBundle, pathToMinBundle);%0A%7D);%0A%0Afunction headerLicense(path) %7B%0A    prependFile(path, constants.licenseDist + '%5Cn', function(err) %7B%0A        if(err) throw err
+    _prepend(pathObj.dist, headerDist);%0A%0A        var headerDistMin = constants.licenseDist%0A            .replace('plotly.js', 'plotly.js (' + pathObj.name + ' - minified)');%0A        _prepend(pathObj.distMin, headerDistMin)
 ;%0A  
@@ -1468,42 +1468,8 @@
 s');
-%0ApathsDist.forEach(headerLicense);
 %0A%0A  

add partial bundle name to dist headers