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

// MODULES //

var fs = require( 'fs' );
var path = require( 'path' );
var parseArgs = require( 'minimist' );
var notifier = require( 'update-notifier' );
var pkg = require( './../package.json' );
var opts = require( './opts.json' );
var detect = require( './../lib' );


// FUNCTIONS //

/**
* Performs initialization tasks.
*
* @private
* @example
* init();
*/
function init() {
	var opts;

	// Check if newer versions exist for this package:
	opts = {
		'pkg': pkg
	};
	notifier( opts ).notify();

	// Set the process title to allow the process to be more easily identified:
	process.title = pkg.name;
	process.stdout.on( 'error', process.exit );
} // end FUNCTION init()

/**
* Prints usage information.
*
* @private
* @example
* help();
* // => '...'
*/
function help() {
	var fpath = path.join( __dirname, 'usage.txt' );
	fs.createReadStream( fpath )
		.pipe( process.stdout )
		.on( 'close', onClose );

	function onClose() {
		process.exit( 0 );
	}
} // end FUNCTION help()

/**
* Prints the package version.
*
* @private
* @example
* version();
* // => '#.#.#'
*/
function version() {
	var msg = pkg.version.toString()+'\n';
	process.stdout.write( msg, 'utf8' );
	process.exit( 0 );
} // end FUNCTION version()


// VARIABLES //

var args;


// MAIN //

init();

// Parse command-line arguments:
args = parseArgs( process.argv.slice( 2 ), opts );

if ( args.help ) {
	return help();
}
if ( args.version ) {
	return version();
}
console.log( detect() );