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

var program = require('commander');
var pluck = require('pluck-keys');
var credential = require('../credential');

var stdin = '';

program
	.command('hash [password]')
	.description('Hash password')
	.option('-w --work <work>', 'relative work load (0.5 for half the work)', Number)
	.option('-k --key-length <key-length>', 'length of salt', Number)
	.action(function( password, options ){
		credential.configure(pluck([
			'keyLength',
			'hashMethod',
			'work'
		], options));

		credential.hash(stdin || password, function( err, result ){
			if (err)
				return console.error(err);

			console.log(result);
		});
	});

program
	.command('verify [hash] <password>')
	.description('Verify password')
	.action(function( hash, password ){
		credential.verify(stdin || hash, password, function( err, result ){
			if (err)
				return console.error(err);

			console.log(result ? 'Verified' : 'Invalid');
			process.exit(result ? 0 : 1);
		});
	});

if (process.stdin.isTTY) {
	program.parse(process.argv);
} else {
	process.stdin.on('readable', function(){
		stdin += this.read() || '';
	});

	process.stdin.on('end', function(){
		program.parse(process.argv);
	});
}

@@ -395,18 +395,18 @@
 function
-(
  
+(
 password
@@ -414,22 +414,30 @@
  options
- 
 )%7B%0A%09%09
+var pw = 
 credenti
@@ -442,18 +442,8 @@
 tial
-.configure
 (plu
@@ -509,26 +509,18 @@
 s));%0A%0A%09%09
-credential
+pw
 .hash(st
@@ -536,34 +536,34 @@
 ssword, function
-(
  
+(
 err, result )%7B%0A%09
@@ -549,33 +549,32 @@
 ion (err, result
- 
 )%7B%0A%09%09%09if (err)%0A%09
@@ -563,32 +563,33 @@
 lt)%7B%0A%09%09%09if (err)
+%7B
 %0A%09%09%09%09return cons
@@ -595,32 +595,37 @@
 sole.error(err);
+%0A%09%09%09%7D
 %0A%0A%09%09%09console.log
@@ -742,18 +742,18 @@
 function
-(
  
+(
 hash, pa
@@ -758,17 +758,16 @@
 password
- 
 )%7B%0A%09%09cre
@@ -773,16 +773,18 @@
 edential
+()
 .verify(
@@ -820,10 +820,10 @@
 tion
-(
  
+(
 err,
@@ -829,17 +829,16 @@
 , result
- 
 )%7B%0A%09%09%09if
@@ -843,16 +843,17 @@
 if (err)
+%7B
 %0A%09%09%09%09ret
@@ -875,16 +875,21 @@
 or(err);
+%0A%09%09%09%7D
 %0A%0A%09%09%09con
@@ -1076,24 +1076,25 @@
 e', function
+ 
 ()%7B%0A%09%09stdin 
@@ -1121,16 +1121,16 @@
 ;%0A%09%7D);%0A%0A
-
 %09process
@@ -1154,16 +1154,17 @@
 function
+ 
 ()%7B%0A%09%09pr

use the new credential api