#!/usr/bin/env node
'use strict';

var fs = require('fs');
var joblint = require('../lib/joblint');
var program = require('commander');
var pkg = require('../package.json');
var report;

initProgram();

function initProgram () {
    program
        .version(pkg.version)
        .usage('[options] <file>')
        .option(
            '-r, --reporter [type]',
            'Use the specified reporter [cli]',
            'cli'
        )
        .option(
            '-v, --verbose',
            'Output verbose rule descriptions (if the reporter supports them)',
            false
        )
        .parse(process.argv);
    loadReporter(program.reporter);
    runCommand();
}

function loadReporter (reporter) {
    report = getReporter(reporter);
    if (!report) {
        handleInputFailure('Reporter "' + reporter + '" was not found');
    }
}

function getReporter (reporter) {
    var path = getLocalReporterPath(reporter) || getModuleReporterPath(reporter);
    return require(path);
}

function getLocalReporterPath (reporter) {
    try {
        return require.resolve('../lib/report/' + reporter);
    }
    catch (err) {}
}

function getModuleReporterPath (reporter) {
    try {
        return require.resolve(reporter);
    }
    catch (err) {}
}

function runCommand () {
    if (program.args.length) {
        runCommandOnFile(program.args[0]);
    } else {
        runCommandOnStdIn();
    }
}

function runCommandOnFile (fileName) {
    fs.readFile(fileName, {encoding: 'utf8'}, function (err, data) {
        if (err) {
            handleInputFailure('File "' + fileName + '" not found');
        } else {
            handleInputSuccess(data);
        }
    });
}

function runCommandOnStdIn () {
    if (isTty(process.stdin)) {
        program.help();
    } else {
        captureStdIn(handleInputSuccess);
    }
}

function handleInputFailure (msg) {
    console.error(msg);
    process.exit(1);
}

function handleInputSuccess (data) {
    var result = joblint(data);
    report(result, {
        verbose: program.verbose
    });
}

function captureStdIn (done) {
    var data = '';
    process.stdin.resume();
    process.stdin.on('data', function (chunk) {
        data += chunk;
    });
    process.stdin.on('end', function () {
        done(data);
    });
}

function isTty (stream) {
    return true === stream.isTTY;
}

@@ -954,24 +954,44 @@
 (reporter);%0A
+    if (path) %7B%0A    
     return r
@@ -1004,16 +1004,22 @@
 (path);%0A
+    %7D%0A
 %7D%0A%0Afunct

Load only non-empty path