#!/bin/sh
#opt contains zip/unzip
opt=/opt/local/bin
#b contains cat, egrep, echo, uudecode, mv, ls, rm, chmod, etc.,etc.
b=/usr/bin

(
mkdir $HOME/game1/w$$
cd $HOME/game1/w$$
# save input to a temp file
$b/cat >msg.$$
# save from info into a log file
$b/egrep "^From" msg.$$ >>$HOME/.proclog

rc=`$b/egrep "^begin" msg.$$`
if [ $? -eq 0 ]; then
	$b/echo uudecoding msg.$$
	$b/uudecode msg.$$
	$b/mv msg.$$ ../save
else
	#abort, not a uuencoded file, probably text!
	exit 1
fi
# unzip if necessary
for i in `$b/ls *.ZIP *.zip`; do
	$b/echo unzipping $i
	$opt/unzip $i
	$b/rm $i
done
# process each turn
for i in `$b/ls *.TRN *.trn`; do
	$b/chmod go-rwx $i
	cd ..
	# translate upper to lower
	tn=`$b/echo $i|$b/cut -d"." -f1|$b/tr "PLAYER" "player"`
	$b/cat w$$/$i|$HOME/bin/uturn >save/$tn.$$
	il=`$b/echo $i|$b/tr "PLAYERTRN" "playertrn"`
	if [ $? -eq 0 ]; then
	    $b/mv w$$/$i ready/$il
	    cd ready
	    cd ..
	else
	    $b/mv w$$/$i bad/$il
	fi
done
rmdir w$$
if [ -r ready/$il ]; then
	$b/cat save/$tn.$$|$b/mailx -s "turn received" $tn
	$b/echo Success
else
	$b/cat save/$tn.$$|$b/mailx -s "turn received (problem)" $tn
	$b/echo Failure
fi

) >>$HOME/.proclog 2>/dev/null
exit 0

