#!/usr/bin/env node

/*eslint-env node, es6 */
/*eslint-disable no-magic-numbers */

"use strict";

const path = require("path");
const fs = require("fs");

const app = require("commander");
const chalk = require("chalk");

const cosi = require(path.resolve(path.join(__dirname, "..", "lib", "cosi")));
const Fetch = require(path.join(cosi.lib_dir, "template", "fetch"));

app.
    version(cosi.app_version).
    option("--id <type-id>", "template id, format type-id (e.g. check-system, graph-cpu, graph-vm, etc.)").
    option("--all", "fetch all templates specific to this host configuration").
    option("--force", "overwrite, if template already exists", false).
    option("-q, --quiet", "no header lines").
    parse(process.argv);

if (!app.quiet) {
    console.log(chalk.bold(app.name()), `v${app.version()}`);
}

if (!app.id && !app.all) {
    console.error(chalk.red("One of, --id or --all is required."));
    app.outputHelp();
    process.exit(1); //eslint-disable-line no-process-exit
}

if (app.id && app.all) {
    console.error(chalk.red("Mutually exclusive, one of --id or --all, not both."));
    app.outputHelp();
    process.exit(1); //eslint-disable-line no-process-exit
}

if (app.id && !app.id.match(/^(check|graph)\-.+$/)) {
    console.error(chalk.red(`Unrecognized template type in ID '${app.id}'`));
    process.exit(1); //eslint-disable-line no-process-exit
}

if (app.config) {
    if (app.config.substr(0, 1) !== "/") {
        app.config = path.resolve(app.config);
    }
}

const fetch = new Fetch(
    cosi.cosi_url,
    cosi.agent_url,
    cosi.reg_dir,
    cosi.cosi_os_type,
    cosi.cosi_os_dist,
    cosi.cosi_os_vers,
    cosi.cosi_os_arch,
    cosi.statsd_type,
    app.force
);

if (app.id) {
    // fetch specific template
    const templateFile = path.resolve(path.join(cosi.reg_dir, `template-${app.id}.json`));

    try {
        const stat = fs.statSync(templateFile);

        if (stat.isFile() && !app.force) {
            console.log(chalk.yellow("Template exits"), `- use --force to overwrite. '${templateFile}'`);
            process.exit(0); //eslint-disable-line no-process-exit
        }
    }
    catch (err) {
        if (err.code !== "ENOENT") {
            throw err;
        }
    }

    fetch.template(app.id,
        (fetchError, template) => {
            if (fetchError) {
                console.error(fetchError);
                throw fetchError;
            }

            if (template.save(templateFile, app.force)) {
                if (!app.quiet) {
                    console.log("Saved template:", templateFile);
                }
            }
        }
    );
}

if (app.all) {
    fetch.all(app.quiet, (fetchError, result) => {
        if (!app.quiet) {
            console.log(result);
        }
        if (fetchError) {
            throw fetchError;
        }
    });
}

@@ -73,16 +73,33 @@
 -numbers
+, no-process-exit
  */%0A%0A%22us
@@ -2339,32 +2339,252 @@
  (fetchError) %7B%0A
+                if (fetchError.code === 404) %7B%0A                    console.error(chalk.red(%22Unknown Template ID%22), app.id, %22not found.%22);%0A                    process.exit(1);%0A                %7D%0A                else %7B%0A    
                 
@@ -2610,16 +2610,20 @@
 Error);%0A
+    
         
@@ -2640,32 +2640,50 @@
 row fetchError;%0A
+                %7D%0A
             %7D%0A%0A 

Clarify message when template id is not found.