#!/bin/sh
# (c) Copyright 1994 James H. Lowe, Jr.
#$Id: shak_copyheader_v 1.20 1997/01/04 01:23:32 jhl Exp $
#
#

TAPE_DEVICE="${SHAK_TAPEDEVICE}"
readfile=0

while [ $# -ge "1" ]
do
   case $1 in
     -f)
        if [ $# -lt 2 ]; then
           echo "shak_copyheader: invalid args." 1>&2
           exit 2
        fi
        shift;
        hfilename="$1"
        readfile="1";
        shift;
	if [ ! -f "$hfilename" ]; then
           echo "shak_copyheader: file $hfilename not found." 1>&2 
           exit 1
        fi
        ;;
      
      -*)
         echo "shak_copyheader invalid args" 1>&2
         exit 2
         ;;
      *)
         break
         ;;
    esac
done

if [ "$shak_verifyi" -eq 1 ]; then
        if [ "$SHAK_TAPENUMBER" = "$SHAK_tapenumber" ]; then
                :
        else
                echo "shak_copyheader: incorrect tape loaded/specified." 1>&2
                exit 1
        fi
fi

if [ $readfile = "1" ]; then

if [ "$shak_no_run_user" -gt "1" -o "$shak_Q" -eq "1" -o "$SHAK_devicesecure" != "yes" ]; then
	:
else
	${SHAK_LIBPATH}/shak_waitlock $shak_waittime
	retval=$?
	if [ $retval -eq 0 ]; then
		:
	else
		exit $retval
	fi
fi

errorstring="`$SHAK_LIBPATH/$SHAK_MT -f ${SHAK_MTDEVICE} rewind 2>&1`"

  if [ $? -eq 0 -a ! "$errorstring" ]; then
   
   delim="`${SHAK_LIBPATH}/shak0_delim`"
   header="`cat $hfilename | grep $delim  |  head -n 1`"
   
   SHAK_TAPENUMBER="`${SHAK_LIBPATH}/shak_gettapenumber "$header"`"   
   export SHAK_TAPENUMBER

   SHAK_INDEXFILE="`${SHAK_LIBPATH}/shak_indexfile`"
   if [ ! $? -eq 0 ]; then
     echo "shak_copyheader: invalid indexfile file name." 1>&2
     exit 1
   fi 
   export SHAK_INDEXFILE
   
   echo -n "$header" | ${SHAK_LIBPATH}/shak0_write_tape
   if [ $? -eq 0 ]; then
      ${SHAK_LIBPATH}/shak_makenewindex
      if [ ! $? -eq 0 ]; then
        echo "shak_copyheader: shak_makenewindex failed" 1>&2
        exit 1
      fi
      cachefileheader="`${SHAK_LIBPATH}/shak_makeindexentry 0 "$header"`"
      echo "$cachefileheader" |
      ${SHAK_LIBPATH}/shak0_append_file ${SHAK_INDEXHOST} ${SHAK_INDEXFILE}
      if [ ! $? -eq 0 ]; then
        echo "shak_copyheader: shak0_append_file failure." 1>&2
        exit 1
      fi 
   else
     echo "shak_copyheader: write tape failed." 1>&2
     exit 1
   fi 
   exit 0
  else
# rewind failed
     echo "shak_copyheader: rewind failed." 1>&2
     exit 1
  fi

else

cat <<!!
shak_copyheader:
This program reads the header of a shak compatible tape and
prompts the user to copy the header to other tapes allowing
for tape number correction.

Hit return to continue. 
!!
read ans
echo ""
echo "Enter source tape, then hit return "
read ans
echo "reading tape header"

errorstring="`$SHAK_LIBPATH/$SHAK_MT -f ${SHAK_MTDEVICE} rewind 2>&1`"

if [ $? -eq 0 -a ! "$errorstring" ]; then
  HEADER="`${SHAK_LIBPATH}/shak0_read_tape`"
else
  exit 1
fi
echo ${HEADER}
ans="N"
echo  "Insert new writable tape, enter y or Y to write another header to tape. "
echo -n "any other letter to quit <$ans> : "
read ans
if [ "$ans" = "" ]; then
  ans="N"
fi

while [ .$ans = .y -o .$ans = .Y ]
do
 echo -n "Enter tape number: "
 read shak_tapenumber
 SHAK_TAPENUMBER="`echo $shak_tapenumber | awk '{ printf ("%04s\n",$1) }'`"
 export SHAK_TAPENUMBER

 SHAK_INDEXFILE="`${SHAK_LIBPATH}/shak_indexfile`"
 if [ ! $? -eq 0 ]; then
   echo "shak_copyheader: invalid indexfile file name." 1>&2
   exit 1
 fi 
 export SHAK_INDEXFILE
	  
 errorstring="`$SHAK_LIBPATH/$SHAK_MT -f ${SHAK_MTDEVICE} rewind 2>&1`"

 if [ $? -eq 0 -a ! "$errorstring" ]; then
   header="`echo -n "$HEADER" | sed -e 's/__TAPE_NO__[0-9][0-9][0-9][0-9]__/'"__TAPE_NO__${SHAK_TAPENUMBER}__/"`"

   if [ ! $? -eq 0 ]; then
     echo "shak_copyheader: sed failed in tape number replacement." 1>&2
     exit 1
   fi

   echo -n "$header" | ${SHAK_LIBPATH}/shak0_write_tape
   if [ $? -eq 0 ]; then
      ${SHAK_LIBPATH}/shak_makenewindex
      if [ ! $? -eq 0 ]; then
        echo "shak_copyheader: shak_makenewindex failed" 1>&2
        exit 1
      fi
      cachefileheader="`${SHAK_LIBPATH}/shak_makeindexentry 0 "$header"`"
      echo "$cachefileheader" |
      ${SHAK_LIBPATH}/shak0_append_file ${SHAK_INDEXHOST} ${SHAK_INDEXFILE}
      if [ ! $? -eq 0 ]; then
        echo "shak_copyheader: shak0_append_file failure." 1>&2
        exit 1
      fi 
   fi 
   echo "$SHAK_TAPENUMBER" 1>&2
 else
   echo "fatal exception" 1>&2
   exit 1
 fi
 ans="N"
 echo  "Insert new writable tape, enter y or Y to write another header to tape. "
 echo -n "any other letter to quit <$ans> : "
 read ans
 if [ "$ans" = "" ]; then
   ans="N"
 fi
done
echo "Remove last tape and/or press any key to continue."
read ans
exit 0

fi
exit 0

