#!/usr/bin/env node

"use strict";

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

var commander = require("commander");

var libPath = path.join(path.dirname(fs.realpathSync(__filename)), "../lib/main.js");
var lib = require(libPath);

var packageJson = require("../package.json");

commander.description("CloudFront Sign").version(packageJson.version)
	.option("-r --resource [resource]", "Resource. [Same as URL]")
	.option("-l --date-less-than [date]", "Date less than. [5 mins from now]")
	.option("-g --date-greater-than [date]", "Date greater than.")
	.option("-a --ip-address [n.n.n.n/nn]", "IP Address in CIDR format.")
	.option("-k --key-pair-id <key-pair-id>", "Key Pair ID.")
	.option("-p --private-key-path <private-key-path>", "Private Key Path.")
	.parse(process.argv);

console.log(commander.description() + " " + commander.version());
var args = commander.args;
if (args.length == 0) {
	console.error("URL is missing");
	commander.help();
}
var url = commander.args[0];

fs.realpath(commander.privateKeyPath, function (err, path) {
	if (err) console.error(err)
	else fs.readFile(path, function (err, data) {
		if (err) console.error(err);
		else {
			var privateKey = data.toString();
			var result = lib(url, commander.resource, commander.dateLessThan, commander.dateGreaterThan, commander.ipAddress, commander.keyPairId, privateKey);
			console.log(JSON.stringify(result, null, "\t"));
		}
	});
});
@@ -986,17 +986,27 @@
 rgs%5B0%5D;%0A
-%0A
+var path = 
 fs.realp
@@ -1008,16 +1008,20 @@
 realpath
+Sync
 (command
@@ -1041,68 +1041,10 @@
 Path
-, function (err, path) %7B%0A%09if (err) console.error(err)%0A%09else 
+)%0A
 fs.r
@@ -1080,17 +1080,16 @@
 data) %7B%0A
-%09
 %09if (err
@@ -1110,17 +1110,16 @@
 r(err);%0A
-%09
 %09else %7B%0A
@@ -1120,17 +1120,16 @@
 lse %7B%0A%09%09
-%09
 var priv
@@ -1154,17 +1154,16 @@
 ring();%0A
-%09
 %09%09var re
@@ -1306,17 +1306,16 @@
 Key);%0A%09%09
-%09
 console.
@@ -1359,16 +1359,11 @@
 ));%0A
-%09
 %09%7D%0A
-%09
 %7D);%0A
-%7D);

Use realpathsync for privateKeyPath