#!/bin/sh
# (c) Copyright 1994,1995,1996,1997 by James H. Lowe, Jr.
# Copying policy: Free Software Foundation GPL
# $Id: shak_testtape_v 1.59 1997/03/23 16:12:04 jhl Exp $
#

# Export the watchdog timeout value
sickdelay=$delay_tape
export sickdelay

TMPFILE="${shak_tmpbasename}.testtape$$"
readfile="0"
abridged="0"
abridged2="0"
use_default=1
exportdotfile="0"
debug="0"
shak_nocheck="0"
exclusive=0
dodashx="0"
first=""
defaulttest=0

if [ ! "$shak_no_run_user" ]; then
	shak_no_run_user=0
fi

TAPE_DEVICE="${SHAK_TAPEDEVICE}"

TMPFILEOPT="${shak_tmpbasename}.testtape.getopts$$"

set -- `$shak_getopt abf:o:mtx $* 2>$TMPFILEOPT`

if [ $? != 0 ]; then
   /bin/rm -f $TMPFILEOPT
   echo "shak_testtape: invalid options." 1>&2
   exit 1
fi

grep '.' <$TMPFILEOPT
if [ "$?" = "0" ]; then
   /bin/rm -f $TMPFILEOPT
   echo "shak_testtape: invalid options." 1>&2
   exit 1
fi
/bin/rm -f $TMPFILEOPT

for i in $*; do
    case $1 in
     -x)
# test index file
	shift;
        dodashx="1"
	exclusive=`expr $exclusive + 1`
	;;
     -a)
# test last header on tape and compare to index file.
	abridged="1";
	shift;
	exclusive=`expr $exclusive + 1`
	;;
     -b)
# test last header on tape and compare to index file.
# obtain tape number by reading 1st tape header.
	abridged2="1";
	shift;
	exclusive=`expr $exclusive + 1`
	;;
     -d)
	debug="1";
	shift;
	;;
     -o)
        shift;
        exportdotfile="1";
        if [ $# -ge 1 ]; then 
            TMPFILE="$1"
        else
 	    echo "shak_testtape: missing argument." 1>&2
	    exit 2
	 fi
         shift;
	 ;;

     -f)
	shift;
        if [ "$1" = "-" ]; then 
     	  SHAK_INDEXFILE="$indexfile"
        else
	  indexfile="$1";
     	  SHAK_INDEXFILE="$indexfile"
	fi
        export SHAK_INDEXFILE
        shift;
        use_default=0
	;;
     -m)
	shak_nocheck="1";
	exclusive=`expr $exclusive + 1`
	shift;
	;;
     -t)
	# Same as not specifying a test type.
	defaulttest=1;
	exclusive=`expr $exclusive + 1`
	shift;
	;;
     --)
	 shift;
	 break;
         ;;
    esac
done

if [ $exclusive -ge 2 ]; then
	echo "shak_testtape: incompatible options." 1>&2
	exit 2
fi

if [ "$shak_debug" -ge  1 ]; then
	set -vx
fi

#if [ "$shak_no_run_user" -gt 1 -o "$shak_Q" -eq 1 -o "$shak_batchmode" -ge 3 -o "$rrun" = 1 ]; then
#	:
#else
#	${SHAK_LIBPATH}/shak_waitlock $shak_waittime
#	retval=$?
#	if [ $retval -eq 0 ]; then
#		:
#	else
#		exit $retval
#	fi
#fi

if [ "$shak_minimal" = "1" -o "$shak_batchmode" -ge 3 ]; then
	abridged="0"
	abridged2="0"
	readfile="0"
	shak_nocheck="1"
fi


# Full comparison test setup
if [ "$dodashx" = "1" ]; then
       # test index file
	readfile="1";
        if [ "$shak_debug" = "1" ]; then
		echo "testtape: reading tapenumber" 1>&2
	fi
	SHAK_SHAKHEADER="`${SHAK_LIBPATH}/shak_gettapeheader`"
        if [ ! $? -eq 0 ]; then
		echo "shak_testtape: gettapeheader failed." 1>&2
		exit 1
	fi
        SHAK_useheader="`echo "${SHAK_SHAKHEADER}" | ${SHAK_LIBPATH}/shak_headermode`"
        SHAK_TAPENUMBER="`${SHAK_LIBPATH}/shak_gettapenumber "${SHAK_SHAKHEADER}"`"
	if [ "$shak_verifyi" -eq 1 ]; then	
		if [ "$SHAK_TAPENUMBER" = "$SHAK_tapenumber" ]; then	
			:
		else
	  		echo "shak_testtape: incorrect tape loaded/specified." 1>&2
			exit 1
		fi
	fi
	SHAK_SHAKDATE="`${SHAK_LIBPATH}/shak_getheaderdate "${SHAK_SHAKHEADER}"`"
        SHAK_TIME_T="`echo -n "${SHAK_SHAKDATE}" | sed -e 's/^.*__CALT__//'`"
        SHAK_SHAKDATE="`echo -n "${SHAK_SHAKDATE}" | sed -e 's/__CALT__.*$//'`"

        if [ "$shak_debug" = "1" ]; then
		echo "testtape: tapenumber : $SHAK_TAPENUMBER" 1>&2
	fi
        export SHAK_TAPENUMBER
        	
        if [ $use_default -eq 1 ]; then
		SHAK_INDEXFILE="`${SHAK_LIBPATH}/shak_indexfile`"
		indexfile="${SHAK_INDEXFILE}"
        fi
fi
# End of Full comparison test setup

if [ "$shak_noindexfile" = "1" ]; then
	abridged="0"
	abridged2="0"
	readfile="0"
fi

if [ "$shak_no_run_user" -lt "2" -a "$shak_Q" -eq 0 -a "$shak_batchmode" -le 2 ]; then
	errorstring="`$SHAK_LIBPATH/$SHAK_MT -f ${SHAK_MTDEVICE} rewind 2>&1`"
	if [ ! $? -eq 0 -o "$errorstring" ]; then
   		echo "shak_testtape: rewind failure" 1>&2
   		exit 1
	fi
else
        if [ $shak_minimal = 0 ]; then
		if [ $shak_noheader -ge 1 ]; then
			:
		else
			shak_nocheck="1"
		fi
        else
		shak_nocheck="1"
	fi
fi

# Minimal Test
if [ "$shak_nocheck" = "1" ]; then
	if [ "$shak_interactive" = "1" ]; then
		if [ "$shak_verifyi" = "1" ]; then
			echo "shak_testtape: tape protection set, gettnu() operation not allowed." 1>&2
			exit 1
		fi
		SHAK_TAPENUMBER="`${SHAK_LIBPATH}/shak_gettnu | grep '^....$'`"
		if [ $? -ne 0 -o "$SHAK_TAPENUMBER" = "X" ]; then
			echo "shak_testtape: shak_gettnu failure." 1>&2
			exit 1
		fi
	else
		if [ "$SHAK_TAPENUMBER" = "NULL" ]; then
			SHAK_TAPENUMBER="`${SHAK_LIBPATH}/shak_gettapenumber`"
		fi
	fi
	if [ "$shak_verifyi" -eq 1 ]; then	
		if [ "$SHAK_TAPENUMBER" = "$SHAK_tapenumber" ]; then	
			:
		else
			echo "shak_testtape: incorrect tape loaded/specified." 1>&2
			exit 1
		fi
	fi
	export SHAK_TAPENUMBER
	if [ $use_default -eq 1 ]; then
		SHAK_INDEXFILE="`${SHAK_LIBPATH}/shak_indexfile`"
		export SHAK_INDEXFILE
		indexfile="${SHAK_INDEXFILE}"
	fi

	lastline="`sickdelay=$delay_readfile export sickdelay; ${SHAK_LIBPATH}/shak0_read_file ${SHAK_INDEXHOST} ${indexfile} | \
	${SHAK_LIBPATH}/shak0_readx`"
    
	if [ ! "$lastline" ]; then
		echo "testtape: read file failed on $indexfile" 1>&2
		exit 2
	fi 

	header="` echo "$lastline" | head -n 1 `"
	SHAK_SHAKDATE="`${SHAK_LIBPATH}/shak_getheaderdate "${header}"`"
	SHAK_TIME_T="`echo -n "${SHAK_SHAKDATE}" | sed -e 's/^.*__CALT__//'`"
	SHAK_SHAKDATE="`echo -n "${SHAK_SHAKDATE}" | sed -e 's/__CALT__.*$//'`"
	lastline="`echo "$lastline" | tail -n 1`"

	if [ $? -eq 0 -a "$lastline" ]; then
		SHAK_useheader="`echo "${lastline}" | ${SHAK_LIBPATH}/shak_headermode`"
		SHAK_lastindex="`echo "${lastline}" | ${SHAK_LIBPATH}/shak0_getlastindex`"
		SHAK_SHAKDATEL="`${SHAK_LIBPATH}/shak_getheaderdate "${lastline}"`"
		SHAK_TIME_TL="`echo -n "${SHAK_SHAKDATEL}" | sed -e 's/^.*__CALT__//'`"
		SHAK_SHAKDATEL="`echo -n "${SHAK_SHAKDATEL}" | sed -e 's/__CALT__.*$//'`"
       
		if [ "$shak_no_run_user" -lt "2" -a "$shak_Q" -eq 0 -a "$shak_batchmode" -le 2 ]; then
			errorstring="`$SHAK_LIBPATH/$SHAK_MT -f ${SHAK_MTDEVICE} ${SHAK_MT_EOM} 2>&1`"
			if [ ! $? -eq 0 -o "$errorstring" ]; then
				echo "shak_testtape: minimal test: move to end-of-recorded-media failed." 1>&2
				exit 1
			fi
		fi
	else
		echo "testtape: read on file $indexfile failed." 1>&2
		echo " Index file missing or other error." 1>&2
		exit 2
	fi
#
# End of Minimal Test
#
# Check last header test
#

elif [ $abridged = "1" ]; then
	# abridged test checks to see if last header on tape matches
	# last header in cooresponding tape index file.

	errorstring="`$SHAK_LIBPATH/$SHAK_MT -f ${SHAK_MTDEVICE} ${SHAK_MT_EOM} 2>&1`"
	if [ ! $? -eq 0 -o "$errorstring" ]; then
		echo "shak_testtape: move to end-of-recorded-media failed." 1>&2
		exit 1
	fi
   
	newtape="0"
	errorstring="`$SHAK_LIBPATH/$SHAK_MT -f ${SHAK_MTDEVICE} bsf 3 2>&1`"
	if [ ! $? -eq 0 -o "$errorstring" ]; then
		newtape="1"
		errorstring="`$SHAK_LIBPATH/$SHAK_MT -f ${SHAK_MTDEVICE} rewind 2>&1`"
		if [ ! $? -eq 0 -o "$errorstring" ]; then
			echo "shak_testtape: abridged: rewind failure: #001 " 1>&2
			exit 1
		fi
	fi

	if [ "$newtape" = "0" ]; then
		#     read from end of tape file to tape header i.e. nothing
		header="`sickdelay=$delay_readtape export sickdelay; ${SHAK_LIBPATH}/shak0_read_tape 2>/dev/null`"
		if [ ! $? -eq 0 ]; then
			echo "shak_testtape: abridged: shak0_read_tape failed." 1>&2
			exit 1
		fi
	fi

	header="`sickdelay=$delay_readtape export sickdelay; ${SHAK_LIBPATH}/shak0_read_tape 2>/dev/null`"
	if [ ! $? -eq 0 -o ! "$header" ]; then
		echo "shak_testtape: abridged: shak0_read_tape failed." 1>&2
		exit 1
	fi

	SHAK_SHAKDATEL="`${SHAK_LIBPATH}/shak_getheaderdate "${header}"`"
	SHAK_TIME_TL="`echo -n "${SHAK_SHAKDATEL}" | sed -e 's/^.*__CALT__//'`"
	SHAK_SHAKDATEL="`echo -n "${SHAK_SHAKDATEL}" | sed -e 's/__CALT__.*$//'`"
	SHAK_TAPENUMBER="`echo "$header" | ${SHAK_LIBPATH}/shak0_gettapenumber`"
	if [ "$shak_verifyi" -eq 1 ]; then	
		if [ "$SHAK_TAPENUMBER" = "$SHAK_tapenumber" ]; then	
			:
		else
			echo "shak_testtape: incorrect tape loaded/specified." 1>&2
			exit 1
		fi
	fi
	SHAK_useheader="`echo "${lastline}" | ${SHAK_LIBPATH}/shak_headermode`"
	export SHAK_TAPENUMBER
	export SHAK_useheader

	if [ $use_default -eq 1 ]; then
		SHAK_INDEXFILE="`${SHAK_LIBPATH}/shak_indexfile`"
		indexfile="${SHAK_INDEXFILE}"
	fi
   
	lastline="`sickdelay=$delay_readfile export sickdelay; ${SHAK_LIBPATH}/shak0_read_file ${SHAK_INDEXHOST} ${indexfile} | tail -n 1`"
	if [ ! "$lastline" ]; then
		echo "testtape: read file failed on $indexfile" 1>&2
		exit 2
	fi 

	if [ $? -eq 0 -a "$lastline" ]; then
		headernumber="`echo $lastline | ${SHAK_LIBPATH}/shak0_getlastindex`"
		header1="`${SHAK_LIBPATH}/shak_makeindexentry $headernumber "${header}"`"
		${SHAK_LIBPATH}/shak_compareheaders "$header1" "$lastline"
		if [ $? -ne 0  ] ; then
			echo "shak_testtape: abridged test: corrupt tape index file: ${indexfile}" 1>&2
			errorstring="`$SHAK_LIBPATH/$SHAK_MT -f ${SHAK_MTDEVICE} ${SHAK_MT_EOM} 2>&1`"
			if [ ! $? -eq 0 -o "$errorstring" ]; then
				echo "shak_testtape: abridged test: move to end-of-recorded-media failed." 1>&2
				exit 1
			fi
			exit 1
		fi  
   
		SHAK_lastindex="`echo "${lastline}" | ${SHAK_LIBPATH}/shak0_getlastindex`"
		header="`sickdelay=$delay_readfile export sickdelay; ${SHAK_LIBPATH}/shak0_read_file ${SHAK_INDEXHOST} ${indexfile} | head -n 1`"

		if [ ! "$header" ]; then
			echo "testtape: read file failed on $indexfile" 1>&2
			exit 2
		fi 
		SHAK_SHAKDATE="`${SHAK_LIBPATH}/shak_getheaderdate "${header}"`"
		SHAK_TIME_T="`echo -n "${SHAK_SHAKDATE}" | sed -e 's/^.*__CALT__//'`"
		SHAK_SHAKDATE="`echo -n "${SHAK_SHAKDATE}" | sed -e 's/__CALT__.*$//'`"
		errorstring="`$SHAK_LIBPATH/$SHAK_MT -f ${SHAK_MTDEVICE} ${SHAK_MT_EOM} 2>&1`"

		if [ ! $? -eq 0 -o "$errorstring" ]; then
			echo "shak_testtape: abridged test: move to end-of-recorded-media failed." 1>&2
			exit 1
		fi
	else
		echo "testtape: read failed on $indexfile" 1>&2
		exit 2
	fi
#
# End of check last header test
#
# Check first header test.
#

elif [ "$abridged2" = "1" ]; then
	# abridged test checks to see if first header on tape matches
	# first header in cooresponding tape index file.
	moved="0"
	if [ "$shak_interactive" = "1" ]; then
		if [ "$shak_verifyi" = "1" ]; then
			echo "shak_testtape: tape protection set, gettnu() operation not allowed." 1>&2
			exit 1
		fi
		SHAK_TAPENUMBER="`${SHAK_LIBPATH}/shak_gettnu | grep '^....$'`"
		if [ $? -ne 0 -o "$SHAK_TAPENUMBER" = "X" ]; then
			echo "shak_testtape: shak_gettnu failure." 1>&2
			exit 1
		fi
	else
		if [ "$SHAK_TAPENUMBER" = "NULL" ]; then
			moved="1"
			SHAK_TAPENUMBER="`${SHAK_LIBPATH}/shak_gettapenumber`"
		fi
	fi
    
	if [ "$shak_verifyi" -eq 1 ]; then	
		if [ "$SHAK_TAPENUMBER" = "$SHAK_tapenumber" ]; then	
			:
		else
			echo "shak_testtape: incorrect tape loaded/specified." 1>&2
			exit 1
		fi
	fi
    
	export SHAK_TAPENUMBER
	if [ $use_default -eq 1 ]; then
		SHAK_INDEXFILE="`${SHAK_LIBPATH}/shak_indexfile`"
		export SHAK_INDEXFILE
		indexfile="${SHAK_INDEXFILE}"
	fi

	lastline="`sickdelay=$delay_readfile export sickdelay; ${SHAK_LIBPATH}/shak0_read_file ${SHAK_INDEXHOST} ${indexfile} | \
	${SHAK_LIBPATH}/shak0_readx`"
  
	if [ ! "$lastline" ]; then
		echo "testtape: read file failed on $indexfile" 1>&2
		exit 2
	fi 

	header="` echo "$lastline" | head -n 1 `"
	SHAK_SHAKDATE="`${SHAK_LIBPATH}/shak_getheaderdate "${header}"`"
	retval=$? 
	SHAK_TIME_T="`echo -n "${SHAK_SHAKDATE}" | sed -e 's/^.*__CALT__//'`"
	SHAK_SHAKDATE="`echo -n "${SHAK_SHAKDATE}" | sed -e 's/__CALT__.*$//'`"
    
	lastline="`echo "$lastline" | tail -n 1`"

	if [ $retval -eq 0 -a "$lastline" ]; then
		SHAK_useheader="`echo "${lastline}" | ${SHAK_LIBPATH}/shak_headermode`"
		SHAK_lastindex="`echo "${lastline}" | ${SHAK_LIBPATH}/shak0_getlastindex`"
		SHAK_SHAKDATEL="`${SHAK_LIBPATH}/shak_getheaderdate "${lastline}"`"
		SHAK_TIME_TL="`echo -n "${SHAK_SHAKDATEL}" | sed -e 's/^.*__CALT__//'`"
		SHAK_SHAKDATEL="`echo -n "${SHAK_SHAKDATEL}" | sed -e 's/__CALT__.*$//'`"
      
		if [ $shak_debug -eq 1 ]; then
			echo "last index in index file $SHAK_lastindex" 1>&2  
		fi
       
		if [ "$SHAK_useheader" != yes ]; then
			errorstring="`$SHAK_LIBPATH/$SHAK_MT -f ${SHAK_MTDEVICE} ${SHAK_MT_EOM} 2>&1`"
			if [ ! $? -eq 0 -o "$errorstring" ]; then
				echo "shak_testtape: abridged test: move to end-of-recorded-media failed." 1>&2
				exit 1
			fi
		else

			if [ $SHAK_lastindex -ge 1 ]; then  
				lastidx="`expr $SHAK_lastindex - $moved`"
				errorstring="`$SHAK_LIBPATH/$SHAK_MT -f ${SHAK_MTDEVICE} fsf $lastidx 2>&1`"
				if [ ! $? -eq 0 -o "$errorstring" ]; then
					echo "shak_testtape: mt fsf failed." 1>&2
					exit 1
				fi
   	 
				header="`sickdelay=$delay_readtape export sickdelay; ${SHAK_LIBPATH}/shak0_read_tape 2>/dev/null`"
				if [ ! $? -eq 0 -o ! "$header" ]; then
					echo "shak_testtape: abridged2: shak0_read_tape failed." 1>&2
					exit 1
				fi
				header1="`${SHAK_LIBPATH}/shak_makeindexentry $SHAK_lastindex "${header}"`"
			else
				header1="$header"
			fi
	
			${SHAK_LIBPATH}/shak_compareheaders "$header1" "$lastline"
			if [ $? -ne 0  ] ; then
				echo "shak_testtape: abridged test: corrupt tape index file: ${indexfile}" 1>&2
				errorstring="`$SHAK_LIBPATH/$SHAK_MT -f ${SHAK_MTDEVICE} ${SHAK_MT_EOM} 2>&1`"
				if [ ! $? -eq 0 -o "$errorstring" ]; then
					echo "shak_testtape: abridged test: move to end-of-recorded-media failed." 1>&2
					exit 1
				fi
				exit 1
			fi  
     	 
			errorstring="`$SHAK_LIBPATH/$SHAK_MT -f ${SHAK_MTDEVICE} fsf 2 2>&1`"
			if [ $? -eq 0 -a ! "$errorstring" ]; then
				echo "shak_testtape: abridged2 test: Invalid tape index file: missing headers." 1>&2
				exit 1
			fi
     	 
			errorstring="`$SHAK_LIBPATH/$SHAK_MT -f ${SHAK_MTDEVICE} ${SHAK_MT_EOM} 2>&1`"
			if [ ! $? -eq 0 -o "$errorstring" ]; then
				echo "shak_testtape: minimal test: move to end-of-recorded-media failed." 1>&2
				exit 1
			fi
		fi
	else
		echo "testtape: abridged2: unknown error." 1>&2
		exit 2
	fi
#
# End of check last header test
#
# Default test
#

else
	# default test
	default_command="${SHAK_LIBPATH}/${SHAK_MT} -f ${TAPE_DEVICE} fsf"

	case $run_space_command in
		"")
			if [ $# -ge 1 ]; then
				space_command="$@"
				if [ "$space_command" = "" ]; then
					space_command="$default_command"
				fi
			else
				space_command="$default_command"
			fi
			;;
		  *)
		  	space_command="$run_space_command"
			;;
	esac

	if [ $shak_noheader = 0 ]; then
		# Beginning of default test for shak format tape
		iseven () {
			y=`expr $1 / 2 `
			z=`echo "${y} * 2" | bc `
			if [ $1 -eq $z ]; 
			then
				return 1
			fi
			return 0
		}

		i=0
		ret=0
		linecount=0
    
		errorstring="`$SHAK_LIBPATH/$SHAK_MT -f ${SHAK_MTDEVICE} rewind 2>&1`"
		if [ ! $? -eq 0 -o "$errorstring" ]; then
			exit 1
		fi
   	 
		while [ $ret -eq 0 ]
		do
			iseven $i 
			if [ $? -eq 0 -o $i -eq 0 ]; then
				linecount=` expr $linecount + 1 ` 
				header="`sickdelay=$delay_readtape export sickdelay; ${SHAK_LIBPATH}/shak0_read_tape 2>/dev/null`"
				header1="`${SHAK_LIBPATH}/shak_makeindexentry $i "${header}"`"
				if [ "$header" ]; then
					if [ $i -eq 0 ]; then
						SHAK_useheader="`echo "${header}" | ${SHAK_LIBPATH}/shak_headermode`"
						SHAK_TAPENUMBER="`${SHAK_LIBPATH}/shak_gettapenumber "${header}"`"
						if [ "$shak_verifyi" -eq 1 ]; then	
							if [ "$SHAK_TAPENUMBER" = "$SHAK_tapenumber" ]; then	
								:
							else
								echo "shak_testtape: incorrect tape loaded/specified." 1>&2
								exit 1
							fi
						fi
						SHAK_SHAKDATE="`${SHAK_LIBPATH}/shak_getheaderdate "${header}"`"
						SHAK_TIME_T="`echo -n "${SHAK_SHAKDATE}" | sed -e 's/^.*__CALT__//'`"
						SHAK_SHAKDATE="`echo -n "${SHAK_SHAKDATE}" | sed -e 's/__CALT__.*$//'`"
					fi
					if [ $readfile = "1" ]; then 
						xheader="` sickdelay=$delay_readfile export sickdelay; ${SHAK_LIBPATH}/shak0_read_file ${SHAK_INDEXHOST} ${indexfile} 1>/dev/null 2>&1`"
						if [ ! $? -eq 0 ]; then
							echo "testtape: read file failed on $indexfile" 1>&2
							exit 2
						fi   
						xheader="` sickdelay=$delay_readfile export sickdelay; ${SHAK_LIBPATH}/shak0_read_file ${SHAK_INDEXHOST} ${indexfile} | \
						awk NR==${linecount} `"
						if [ "$xheader" = "$header1" ]; then
							:
						else
							echo "testtape: tape index file error, file: ${SHAK_INDEXFILE}" 1>&2
							echo "testtape:   loop number $i; line number $linecount" 1>&2
							exit 1
						fi
					fi
				fi 
    	 
				if [ ! $? -eq 0 -o ! "$header" ]; then
					errorstring="`$SHAK_LIBPATH/$SHAK_MT -f ${SHAK_MTDEVICE} ${SHAK_MT_EOM} 2>&1`"
					if [ ! $? -eq 0 -o "$errorstring" ]; then
						echo "shak_testtape: move to end-of-recorded-media failed." 1>&2
						exit 1
					else
						ret=1
					fi
				else
					HOLD001="${SHAK_LIBPATH}/shak0_delim"
					echo "${header}" | grep -q "^`${HOLD001}`"
					if [ $? -eq 0 ]; then
						echo "`${SHAK_LIBPATH}/shak_makeindexentry $i "${header}"`"
						SHAK_SHAKDATEL="`${SHAK_LIBPATH}/shak_getheaderdate "${header}"`"
						SHAK_TIME_TL="`echo -n "${SHAK_SHAKDATEL}" | sed -e 's/^.*__CALT__//'`"
						SHAK_SHAKDATEL="`echo -n "${SHAK_SHAKDATEL}" | sed -e 's/__CALT__.*$//'`"
						SHAK_lastindex="$i"
						ret=0
					else
						echo "shak_testtape: tape header format exception. loop $i" 1>&2
						exit 2
					fi
				fi
			else
				if [ "$shak_debug" = "1" ]; then
					echo "${SHAK_LIBPATH}/shak_remsh ${SHAK_REMSH} ${SHAK_TAPEHOST} ${space_command}" 1>&2
				fi
				errorstring=""
				if [ "$space_command" = "$default_command" ]; then
					errorstring="`${space_command} 2>&1`"
				else
					sickdelay=0 export sickdelay; ${SHAK_LIBPATH}/shak_userspace ${space_command} </dev/null
				fi
				if [ $? -eq 0 -a ! "$errorstring" ]; then
					ret=0
				else
					echo "shak_testtape: command $space_command failed; tape file $i" 1>&2
					exit 2
				fi
			fi
			i=` expr $i + 1 `
		done
    
		if [ $ret -eq 2 ]; then
			echo "shak_testtape: fatal exception" 1>&2
			exit 1
		fi
		
		# End of default test for standard shak format tape.
	else
		# Here is the default test code for an Abridged Shak Format Tape

		moved=0
		if [ "$shak_interactive" = "1" ]; then
			if [ "$shak_verifyi" = "1" ]; then
				echo "shak_testtape: tape protection set, gettnu() operation not allowed." 1>&2
				exit 1
			fi
			SHAK_TAPENUMBER="`${SHAK_LIBPATH}/shak_gettnu | grep '^....$'`"
			if [ $? -ne 0 -o "$SHAK_TAPENUMBER" = "X" ]; then
				echo "shak_testtape: shak_gettnu failure." 1>&2
				exit 1
			fi
		else
			if [ "$SHAK_TAPENUMBER" = "NULL" ]; then
				moved=1
				SHAK_TAPENUMBER="`${SHAK_LIBPATH}/shak_gettapenumber`"
				if [ "$shak_verifyi" -eq 1 ]; then	
					if [ "$SHAK_TAPENUMBER" = "$SHAK_tapenumber" ]; then	
						:
					else
						echo "shak_testtape: incorrect tape loaded/specified." 1>&2
						exit 1
					fi
				fi
			fi
		fi
		export SHAK_TAPENUMBER
		if [ $use_default -eq "1" ]; then
			SHAK_INDEXFILE="`${SHAK_LIBPATH}/shak_indexfile`"
			export SHAK_INDEXFILE
			indexfile="${SHAK_INDEXFILE}"
		fi
   
		lastline="`sickdelay=$delay_readfile export sickdelay; ${SHAK_LIBPATH}/shak0_read_file ${SHAK_INDEXHOST} ${indexfile} | \
		${SHAK_LIBPATH}/shak0_readx`"
  	 
		if [ ! "$lastline" ]; then
			echo "testtape: read file failed on $indexfile" 1>&2
			exit 2
		fi 
  	 
		header="` echo "$lastline" | head -n 1 `"
		SHAK_SHAKDATE="`${SHAK_LIBPATH}/shak_getheaderdate "${header}"`"
		SHAK_TIME_T="`echo -n "${SHAK_SHAKDATE}" | sed -e 's/^.*__CALT__//'`"
		SHAK_SHAKDATE="`echo -n "${SHAK_SHAKDATE}" | sed -e 's/__CALT__.*$//'`"
		lastline="`echo "$lastline" | tail -n 1`"
  	 
		if [ $? -eq 0 -a "$lastline" ]; then
			SHAK_lastindex="`echo "${lastline}" | ${SHAK_LIBPATH}/shak0_getlastindex`"
			SHAK_useheader="`echo "${lastline}" | ${SHAK_LIBPATH}/shak_headermode`"
			SHAK_SHAKDATEL="`${SHAK_LIBPATH}/shak_getheaderdate "${lastline}"`"
			SHAK_TIME_TL="`echo -n "${SHAK_SHAKDATEL}" | sed -e 's/^.*__CALT__//'`"
			SHAK_SHAKDATEL="`echo -n "${SHAK_SHAKDATEL}" | sed -e 's/__CALT__.*$//'`"
		else
			echo "testtape: read on file $indexfile failed." 1>&2
			echo " Index file missing or other error." 1>&2
			exit 2
       		fi
  	 
		if [ "$shak_no_run_user" -lt "2" -a "$shak_Q" -eq "0" ]; then
			nlines="`sickdelay=$delay_readfile export sickdelay; ${SHAK_LIBPATH}/shak0_read_file ${SHAK_INDEXHOST} ${indexfile} | wc -l`"
			nlines="`expr $nlines - $moved`"
			errorstring="`$SHAK_LIBPATH/$SHAK_MT -f ${SHAK_MTDEVICE} fsf $nlines 2>&1`"
			if [ ! $? -eq 0 -o "$errorstring" ]; then
				echo "shak_testtape: Abridged Tape Format error, too many headers in tape index file." 1>&2
				exit 1
			fi
			errorstring="`$SHAK_LIBPATH/$SHAK_MT -f ${SHAK_MTDEVICE} fsf 2>&1`"
			if [  $? -eq 0 -a ! "$errorstring" ]; then
				echo "shak_testtape: Abridged Tape Format error, too few headers in tape index file." 1>&2
				exit 1
			fi
			errorstring="`$SHAK_LIBPATH/$SHAK_MT -f ${SHAK_MTDEVICE} ${SHAK_MT_EOM} 2>&1`"
			if [ ! $? -eq 0 -o "$errorstring" ]; then
				echo "shak_testtape: move to end-of-recorded-media failed." 1>&2
					exit 1
			fi
		fi
		sickdelay=$delay_readfile ${SHAK_LIBPATH}/shak0_read_file ${SHAK_INDEXHOST} ${indexfile}
		# End of default test for Abridged Shak Format Tape
	fi
fi

# End of tests

if [ "$shak_debug" = "1" ]; then
	echo "shak_testtape ---------------------" 1>&2
	${SHAK_LIBPATH}/shak_debug 
	echo "shak_testtape =====================" 1>&2
fi

if [ "$shak_no_run_user" -ge "1" ]; then
   echo "SHAK_TAPENUMBER=$SHAK_TAPENUMBER" 1>&2
   echo "SHAK_SHAKDATE=$SHAK_SHAKDATE" 1>&2
   echo "SHAK_SHAKDATEL=$SHAK_SHAKDATEL" 1>&2
   echo "SHAK_TIME_T=$SHAK_TIME_T" 1>&2
   echo "SHAK_TIME_TL=$SHAK_TIME_TL" 1>&2
   echo "SHAK_lastindex=$SHAK_lastindex" 1>&2
   echo "SHAK_useheader=$SHAK_useheader" 1>&2
fi

if [ "$exportdotfile" = "1" ]; then
   umask 077
   >$TMPFILE
   echo "SHAK_TAPENUMBER=\"$SHAK_TAPENUMBER\"" >>$TMPFILE
   echo "SHAK_SHAKDATE=\"$SHAK_SHAKDATE\"" >>$TMPFILE
   echo "SHAK_SHAKDATEL=\"$SHAK_SHAKDATEL\"" >>$TMPFILE
   echo "SHAK_TIME_T=\"$SHAK_TIME_T\"" >>$TMPFILE
   echo "SHAK_TIME_TL=\"$SHAK_TIME_TL\"" >>$TMPFILE
   echo "SHAK_lastindex=\"$SHAK_lastindex\"" >>$TMPFILE
   echo "SHAK_useheader=\"$SHAK_useheader\"" >>$TMPFILE

   if [ "$debug" = "1" ]; then
     echo "shak_testtape ---------------------" 1>&2
     cat $TMPFILE 1>&2
     echo "shak_testtape =====================" 1>&2
   fi

   if [ "$shak_debug" = "1" ]; then
     echo "shak_testtape export dot file--------" 1>&2
     cat $TMPFILE 1>&2
     echo "shak_testtape export dot file========" 1>&2
   fi
fi

if [ "$shak_no_run_user" -lt "2" ]; then
	exit 0
else
	exit 1
fi

