#! bin/sh
#
# This compiles, tests, and times the libraries in their various
# configurations.  The results of each test can be written to a
# separate file, or a summary can be written to one summary file.
#
# USAGE
#	testall [ -l ] [ -h host ] i[ -m makeout ] [ -o output ] [ -v version ]
# Options are:
#	-l		long form of output; in this case the output is
#			taken as a directory name and the results of each
#			test is put into a separate file in the directory
#			Names are wwwtppkssaammii, where:
#			www	is what is being dested (des is the DES,
#				cry the crypt(3))
#			t	is the type of interface (U for UNIX,
#				D for deszip)
#			pp	is the path width (06 or 12)
#			k	is the key permutations (1 or 3)
#			ss	is the salt (of for on fly, pc for
#				prrecompute, xx for not applicable)
#			aa	is for bit accesses (by for packed
#				bytes, sm for shift and mask, bf for
#				bitfields, xx for not applicable)
#			mm	is the addressing mode (ai for autoincrement,
#				im for immediate mode)
#			ii	is the indexing mode (ty for the type punning
#				method, us for the usual indexing)
#			A file named DATA contains information about
#			how the runs were generated
#	-m makeout	output from the makes goes to the file named makeout
#	-o output	output goes to the file or directory named
#	-h host		the CPU is of type host (eg, dec5000, sun3; see
#			the directory ../include for a list)
#	-n		only test, do not time
#	-v version	what deszip library do you want to run (des32, etc.)
#
#################################################################
# Copyright notice.						#
# This software is copyrighted (c) 1991 by Matt Bishop and the	#
# Trustees of Dartmouth College.  All rights reserved.		#
# 								#
# Author:	Matt Bishop					#
# Address:	Department of Mathematics and Computer Science	#
#	 	Dartmouth College				#
# 		Hanover, NH  03755-1831				#
# 		USA						#
# telephone:	+1 603 646 3267					#
# fax:		+1 603 646 1312					#
# internet:	Matt.Bishop@dartmouth.edu			#
# usenet:	...!decvax!dartvax!Matt.Bishop			#
#################################################################
#
# Parameters -- changing these affects how the program works
VERSION="Version GAMMA 6/31/91 Matt.Bishop@dartmouth.edu"	# version
PATH=%%TSTDIR%%:/usr/ucb:/bin:/usr/bin	# execution path
LIBDIR=%%LIBDIR%%			# root for the library
INCDIR=%%INCDIR%%			# machine descriptions are here
TESTDIR=%%TSTDIR%%			# where the test stuff is
OUTPUT=$TESTDIR/testout			# default output file
MAKEOUT=$TESTDIR/Make.out		# output for the makes
VERSION=%%VERSION%%			# default version of deszip
HOSTTYPE=%%HOSTTYPE%%			# default host type
LONG=no					# use long form
TIMEIT=					# if no, this is n else blank
# VARIABLES -- the program will change these
# i=					# counter in a for loop
# x=					# holds part of option after flag
# ac=					# counter for path width
# ky=					# counter for key permutations
# indx=					# counter for array indexing
# mode=					# counter for addressing modes
# mask=					# counter for bit accesses
# salt=					# counter for salt computation
# file=					# output summary file name
# ai=					# 2 char combination for a[i] (typepun)
# mo=					# 2 char combination for mode (autoinc)
# ml=					# 2 char combination for access
# sl=					# 2 char combination for salts
outheader=0				# has header been printed?
computer=				# computer system name
chip=					# CPU chip name
opsys=					# vendor's operating system
compiler=				# compiler version
site=					# where the measurements are taken
flag=					# for options with args
#
# process signals
#
# trap "exit 2"; 1 2 3 15
#
# process arguments
#
for i in $*
do
	# this is an argument to an option
	if test -n "$flag"
	then
		case "$flag" in
		h)	HOSTTYPE="$i"	;;	# -h ...
		m)	MAKEOUT="$i"	;;	# -m ...
		o)	OUTPUT="$i"	;;	# -o ...
		v)	VERSION="$i"	;;	# -v ...
		esac
		flag=
	else
		case $i in
		 -h*)	x=`expr "$i" : '-h\(.*\)'`	# host type
			if test -n "$x"
			then
				HOSTTYPE="$x"
			else
				flag=h
			fi
			;;
		-l)	LONG=yes			# long form
			;;
		-m*)	x=`expr "$i" : '-m\(.*\)'`	# output file
			if test -n "$x"
			then
				MAKEOUT="$x"
			else
				flag=m
			fi
			;;
		-n)	TIMEIT=n			# don't time
			;;
		-o*)	x=`expr "$i" : '-o\(.*\)'`	# output file
			if test -n "$x"
			then
				OUTPUT="$x"
			else
				flag=o
			fi
			;;
		-v*)	x=`expr "$i" : '-v\(.*\)'`	# version
			if test -n "$x"
			then
				VERSION="$x"
			else
				flag=v
			fi
			;;
		*)					# oops ...
			echo "Usage: $0 [ -l ] [ -h cpu ] [ -o output ] [ -v ver ]" 1>&2
			exit 1
			;;
		esac
	fi
done
#
# now do your sanity checks
#
# if output file is a directory, ask user to move it
if test -d $OUTPUT -a "$LONG" != yes
then
	
	echo "$OUTPUT exists and is a directory -- move it" 1>&2
	exit 1
fi
# if output directory is a file, ask user to move it
if test -w $OUTPUT -a "$LONG" = yes
then
	echo "$OUTPUT exists and is a file -- move it" 1>&2
	exit 1
fi
# if output directory does not exist, create it
if test ! -d $OUTPUT -a "$LONG" = yes
then
	mkdir $OUTPUT
	if test $? -ne 0
	then
		# oops ... bye!
		echo "the directory $OUTPUT cannot be made" 1>&2
		exit 1
	fi
fi
#
# put in the headers for a little bit of context
#
echo 'What computer is this being done on? '
read COMPUTER
echo 'What chip does this computer use? '
read chip
echo 'What operating system (include version) is this using? '
read opsys
echo 'What C compiler (include version) is this using? '
read compiler
echo 'What is the name of this host (use full domain form)? '
read site
#
# get the output file name
# if a directory, this just maintains the prologue
#
if test -d $OUTPUT
then
	file=$OUTPUT/DATA
else
	file=$OUTPUT
fi
#
# print a prologue
#
echo 'Testing and timing the DES and CRYPT implementations' > $file
echo 'Computer:        ' $computer >> $file
echo 'Chip:            ' $chip >> $file
echo 'Operating System:' $opsys >> $file
echo 'C Compiler:      ' $compiler >> $file
echo 'Host name (site):' $site >> $file
echo 'Date:            ' `date` >> $file
echo ' ' >> $file
#
# now do the testing
# blow away old versions to avoid contamination
#
cd $TESTDIR
rm -f tsttim tsttim.o $MAKEOUT
rm -f $INCDIR/des.h $INCDIR/desproto.h
#
# copy in the new headers
#
cp ../$VERSION/des.h $INCDIR
cp ../$VERSION/desproto.h $INCDIR
#
# note you are setting THREE variables here
# the layout is cosmetic only, to avoid going off the right
#
for indx in TYPEPUN USUAL
do
for mode in AUTOINC IMMED
do
for mask in SHIFTANDMASK BITFIELDS BYTES
do
for salt in ONFLY PRECOMPUTE
do
	# the 64-bit version does not handle TYPEPUN
	if test \( "$VERSION" = des64 \) -a \( "$indx" = TYPEPUN \)
	then
		continue
	fi
	# set up the parts of the file names
	case $indx in
	TYPEPUN)	ai=ty ;;
	USUAL)		ai=in ;;
	*)		ai=xx ;;
	esac;
	case $mode in
	AUTOINC)	mo=ai ;;
	IMMED)		mo=im ;;
	*)		mo=xx ;;
	esac;
	case $mask in
	BITFIELDS)	ml=bf ;;
	SHIFTANDMASK)	ml=sm ;;
	BYTES)		ml=by ;;
	*)		ml=xx ;;
	esac;
	case $salt in
	ONFLY)		sl=of ;;
	PRECOMPUTE)	sl=pc ;;
	*)		sl=xx ;;
	esac;
	# copy the machine header file to the appointed place
	cp $INCDIR/$HOSTTYPE.h $INCDIR/mach.h
	# now loop, once for each possibility
	for ac in 06 12; do for ky in 3 1
	do
		# build the library version
		( cd $LIBDIR/$VERSION
		  make clobber
		  make DESKEY=$ky DESPATH=$ac DESBITS=$mask \
			 DESSALT=$salt CDESKEY=$ky CDESPATH=$ac\
			 CDESBITS=$mask CDESSALT=$salt ADDRMODE=$mode \
			 ARRINDEX=$indx lib
		) 2>&1 >> $MAKEOUT
		# build the test file
		rm -f tsttim
		make tsttim LIB=$LIBDIR/$VERSION/libdes.a 2>&1 >> $MAKEOUT
		# run the tests
		if test "$LONG" = yes
		then
			tsttim -vd$TIMEIT  < deslist | \
					 tee $OUTPUT/desD$ac$ky$ml$sl$mo$ai
			tsttim -vdu$TIMEIT < deslist | \
					 tee $OUTPUT/desU$ac$ky$ml$sl$mo$ai
			tsttim -vc$TIMEIT  < pwdlist | \
					 tee $OUTPUT/cryD$ac$ky$ml$sl$mo$ai
			tsttim -vcu$TIMEIT < pwdlist | \
					 tee $OUTPUT/cryU$ac$ky$ml$sl$mo$ai
		else
			# print a header the first time through
			if test "$outheader" -eq 0
			then
				tsttim -o | tee -a $OUTPUT
				outheader=1
			fi
			tsttim -d$TIMEIT  < deslist | tee -a $OUTPUT
			tsttim -du$TIMEIT < deslist | tee -a $OUTPUT
			tsttim -c$TIMEIT  < pwdlist | tee -a $OUTPUT
			tsttim -cu$TIMEIT < pwdlist | tee -a $OUTPUT
		fi
 	done; done
done
done
done
done
#
# now do the standard function
#
if test "$LONG" = yes
then
	tsttim -dsf < deslist |\
		tee $OUTPUT/desUsyslib
	tsttim -csf < pwdlist |\
		tee $OUTPUT/cryUsyslib
else
	tsttim -dsf < deslist | tee -a $OUTPUT
	tsttim -csf < pwdlist | tee -a $OUTPUT
fi
#
# bye
#
exit 0
