const UglifyJS = require("uglify-js");
const fs = require("fs");
const path = require("path");
const process = require("process");

const args = process.argv.slice(2);
const debug = args[0] && args[0] === "debug";
console.log('debug = ' + debug);

const key = process.env.APPINSIGHTS_INSTRUMENTATIONKEY;
if (key) {
    console.log("key found in env: " + key);
    const scriptsFolder = path.join(__dirname, "..", "Scripts");
    const aiscript = path.join(scriptsFolder, "aikey.js");

    console.log("Merging AppInsights key (" + key + ") into js");
    if (fs.existsSync(aiscript)) {
        console.log("  Deleting " + aiscript);
        fs.unlinkSync(aiscript);
    }

    console.log("Building " + aiscript);
    fs.writeFileSync(aiscript, "/* exported aikey */ window.aikey = function () { return \"" + key + "\"; };", "utf8");
} else {
    console.log("No key found - skipping aikey.js generation");
}

// Simple stupid hash to reduce commit ID to something short
const getHash = function (str) {
    var hash = 42;
    if (str.length) { for (var i = 0; i < str.length; i++) { hash = Math.abs((hash << 5) - hash + str.charCodeAt(i)); } }
    return hash.toString(16);
};

var commitID = process.env.SCM_COMMIT_ID;
if (!commitID) commitID = "test";
const version = getHash(commitID);
console.log("commitID: " + commitID);
console.log("version: " + version);

const scriptsFolderSrc = path.join(__dirname, "..", "src", "Scripts");
const scriptsFolderDst = path.join(__dirname, "..", "Scripts", version);

// Copy files from src to dst, replacing %version% on the way if munge is true
const deploy = function (src, dst, munge) {
    if (!fs.existsSync(dst)) fs.mkdirSync(dst, { recursive: true });

    console.log("Copying from " + src + " to " + dst);
    const srcFiles = fs.readdirSync(src);
    for (const srcFile of srcFiles) {
        var srcPath = path.join(src, srcFile);
        var dstPath = path.join(dst, srcFile);

        console.log("   Copying from " + srcPath + " to " + dstPath);

        var fileBytes = fs.readFileSync(srcPath, "utf8");
        if (munge) fileBytes = fileBytes.replace(/%version%/g, version);
        fs.writeFileSync(dstPath, fileBytes, "utf8");
    }
};

console.log("Deploying pages");
deploy(path.join(__dirname, "..", "src", "Pages"), path.join(__dirname, "..", "Pages"), true);

console.log("Deploying css");
deploy(path.join(__dirname, "..", "src", "Content"), path.join(__dirname, "..", "Content", version), false);

var options = {
    compress: {},
    mangle: {},
    sourceMap: {}
};

const targets = {
    "2047.min.js": ["2047.js"],
    "Antispam.min.js": ["Antispam.js"],
    "Default.min.js": ["Default.js"],
    "DesktopPane.min.js": ["DesktopPane.js"],
    "Errors.min.js": ["Errors.js"],
    "ForefrontAntispam.min.js": ["ForefrontAntispam.js"],
    "Functions.min.js": ["Functions.js"],
    "GetHeaders.min.js": ["GetHeaders.js"],
    "GetHeadersEWS.min.js": ["GetHeadersEWS.js"],
    "GetHeadersRest.min.js": ["GetHeadersRest.js"],
    "Headers.min.js": ["Headers.js"],
    "MobilePane-ios.min.js": ["MobilePane-ios.js"],
    "MobilePane.min.js": ["MobilePane.js"],
    "Other.min.js": ["Other.js"],
    "Received.min.js": ["Received.js"],
    "siteTypesOffice.min.js": ["siteTypesOffice.js"],
    "StandAlone.min.js": ["StandAlone.js"],
    "Strings.min.js": ["Strings.js"],
    "Summary.min.js": ["Summary.js"],
    "Table.min.js": ["Table.js"],
    "uiToggle.min.js": ["uiToggle.js"],
    "diag.min.js": ["diag.js"],
    "unittests/ut-2047.min.js": ["unittests/ut-2047.js"],
    "unittests/ut-DateTime.min.js": ["unittests/ut-DateTime.js"],
    "unittests/ut-GetHeaderList.min.js": ["unittests/ut-GetHeaderList.js"],
    "unittests/ut-ParseError.min.js": ["unittests/ut-ParseError.js"],
    "unittests/ut-parseHeaders.min.js": ["unittests/ut-parseHeaders.js"],
    "unittests/ut-Received.min.js": ["unittests/ut-Received.js"],
    "unittests/ut-XML.min.js": ["unittests/ut-XML.js"]
};

console.log("Deploying script");
for (const targetName of Object.keys(targets)) {
    const fileSet = targets[targetName];
    const mapName = targetName + ".map";
    console.log("   Building " + targetName + " and " + mapName);

    // Create the list of files and read their sources
    const files = {};
    for (const file of fileSet) {
        const fileName = path.basename(file);
        files[fileName] = fs.readFileSync(path.join(scriptsFolderSrc, file), "utf8");
        // Copy original over verbatim to the target directory
        const dstPathOrig = path.join(scriptsFolderDst, file);
        const dstDir = path.dirname(dstPathOrig);
        if (!fs.existsSync(dstDir)) {
            fs.mkdirSync(dstDir);
        }

        fs.writeFileSync(dstPathOrig, files[fileName], "utf8");
        if (debug) {
            // Copy min over to the target directory
            const dstPathMin = path.join(scriptsFolderDst, targetName);
            fs.writeFileSync(dstPathMin, files[fileName], "utf8");
        }
    }

    if (!debug) {
        options.sourceMap.filename = path.basename(targetName);
        options.sourceMap.url = path.basename(mapName);
        const result = UglifyJS.minify(files, options);
        fs.writeFileSync(path.join(scriptsFolderDst, targetName), result.code, "utf8");
        fs.writeFileSync(path.join(scriptsFolderDst, mapName), result.map, "utf8");
    }
}
@@ -5166,16 +5166,234 @@
 tions);%0A
+        if (result.error) %7B%0A            console.log(%22         result.error %22 + result.error);%0A        %7D%0A%0A        if (result.warnings) %7B%0A            console.log(%22         result.warnings%22 + result.warnings);%0A        %7D%0A%0A
         

add error/warning output to build script