#!/bin/sh
# (c) Copyright 1996 James H. Lowe, Jr.
#
#$Id: shak_runbatch_v 1.9 1997/06/29 23:51:23 jhl Exp $
#

batchfilename="$1"


if [ "${shak_batchmode}" -ne 1 ]; then
	echo "shak_runbatch: internal error" 1>&2
	exit 1
fi

${SHAK_LIBPATH}/shak_remsh  ${SHAK_REMSH} ${SHAK_BACKHOST} cat ${batchfilename} </dev/null |

# interpret the batch file and exec the appropriate interpreter.
# which is either  cat(1) or sh(1) based on the first line of the input.
awk '
{ line[NR] = $0 }

END { 

   if ( line[1] == "#!/bin/sh" ) {
      i=2
      while (i <= NR) {
          print line[i] | "sh -s"
	  i = i + 1
       }
   } else {
      i=1
      while (i <= NR) {
          print line[i] | "cat"
	  i = i + 1
       }
   }
}
' |
egrep -v -e '^[ 	]*#' -e '^#' |
egrep -v -e '^[	]*$' -e '^$' |
while  read line
do
	case $shak_batchmode in
		1)
		   :
		   ;;
		*)
		   shak_expert=0
		   export shak_expert
		   ;;
	esac
	shak_batchmode="`expr $shak_batchmode + 1`"
	export shak_batchmode
	sh -c "${SHAK_LIBPATH}/shak_run $line" </dev/null
	retval=$?
	if [ $retval -ne 0 ]; then
	${SHAK_LIBPATH}/shak_mail $shak_mailuser "shak: runbatch failed at command: $line" <<EOF
batch command
$line
returned exit value $retval.

Batch file:
`cat $batchfilename`
EOF
		exit 1
	fi
done
exit 0

