#!/bin/sh

#-------------------------------------------------------------------
# John, testing suite, v1.08.   Jim Fougeron.  
#
# No copyright is claimed, and the software is hereby 
# placed in the public domain. In case this attempt to disclaim 
# copyright and place the software in the public domain is deemed 
# null and void, then the software is Copyright  2011 Jim Fougeron
# and it is hereby released to the general public under the following
# terms: 
#
# This software may be modified, redistributed, and used for any
# purpose, in source and binary forms, with or without modification.
#--------------------------------------------------------------------

if [ x"$JOHN_PATH" = x ]; then
  JOHN_PATH="../run"
  JOHN_EXE="$JOHN_PATH/john"
  UNIQUE="$JOHN_PATH/unique"
  export JOHN_PATH
  export JOHN_EXE
fi

rm -f tst.rec
rm -f tst.pot
rm -f tst.log

# since the -pot= command does not work for 'base' john we have to remove
# it from the ../run directory.  However, we do not want to remove a 'valid'
# john.pot file.  So for now, we rename it, run the script, delete the pot
# file, then rename the orignal john.pot file back to what it was.
rm -f $JOHN_PATH/tstxxx
mv $JOHN_PATH/john.pot $JOHN_PATH/tstxxx 2> /dev/null

echo $3 | awk '{printf("%-32.32s", $1);}'

if [ ! -s $1.dic ]; then
  echo "File [$1.dic] is missing or empty."
  exit 1
fi

if [ ! -s $2 ]; then
  echo "File [$2] is missing or empty."
  exit 1
fi

EXPARM=
if [ "$5" != "$EXPARM" ]; then
   EXPARM="$4=$5 $6 $7"
fi
JOHN_OUT=`$JOHN_EXE -sess=./tst -w=./$1.dic $EXPARM $2 2>&1 >/dev/null | awk '{printf("%s %s %s %s %s\n",$1,$2,$3,$4,$5);}' | grep guesses`
if [ "$JOHN_OUT" != "" ]; then
	echo $JOHN_OUT
	echo $3 | cut -b 7-30 | awk '{printf(".pot CHK:  %-21.21s", $1);}'
    cut -f 2-8 -d ":"    < $JOHN_PATH/john.pot > pw2
	rm -f pw3
	$UNIQUE < pw2 pw3 > /dev/null
	rm -f pw2
	mv $JOHN_PATH/john.pot tst2.pot
	JOHN_OUT=`$JOHN_EXE -sess=./tst -w=./pw3 $EXPARM $2 2>&1 >/dev/null | awk '{printf("%s %s %s %s %s\n",$1,$2,$3,$4,$5);}' | grep guesses`
	rm -f pw3
	rm -f tst2.pot
	echo $JOHN_OUT
	echo
else
	echo "This hash not able to run on this build of john. Input file is [$2]"
	#exit 2
fi
JOHN_OUT=
EXPARM=

rm -f tst.rec
rm -f tst.pot
rm -f john.pot
rm -f john.log
rm -f tst.log
# restore any original john.pot that existed in the run directory
rm -f $JOHN_PATH/john.pot
mv $JOHN_PATH/tstxxx $JOHN_PATH/john.pot 2> /dev/null

