#!/bin/sh
# $Id: shak_fixtape_v 1.19 1997/01/18 16:08:23 jhl Exp $
     
TAPEONLY="0"
FILEONLY="0"
COPYLASTF="0"
COPYLASTT="0"
COPYNHEADER="0"


if [ "$SHAK_devicesecure" = "yes" ]; then
	exit 1
fi

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

if [ "$shak_rootonly0" = yes -a \( "$SHAK_REALLOGIN" != root -o "$SHAK_USERLOGIN" != root \) ]; then
      exit 2
fi

TMPFILEOPT="${shak_tmpbasename}fixtape.gts$$"
set -- `$shak_getopt fcltm: $* 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
       -t)
          shift;
	  TAPEONLY="1"
          ;;
       -f)
          shift;
	  FILEONLY="1"
          ;;
       -c)
          shift;
	  COPYLASTF="1"
          ;;
       -l)
          shift;
	  COPYLASTT="1"
          ;;
       -m)
          shift;
          if [ $# -ge "1" ]; then
	     COPYNHEADER="$1"
	     shift;
          else
	     COPYNHEADER="0"
	  fi
	  ;;
         
	--)
	   shift;
	   break
	   ;;
      esac
   done
    
if [ "$TAPEONLY" = "1" -a "$FILEONLY" = "1" ]; then
  echo "fixtape: Invalid args"
  exit 1
fi

if [ "$COPYLASTF" = "1" -a "$COPYLASTT" = "1" ]; then
  echo "fixtape: Invalid args"
  exit 1
fi

if [ "$SHAK_TAPENUMBER" = "NULL" ]; then
	SHAK_TAPENUMBER="`${SHAK_LIBPATH}/shak_gettnu | grep '^....$'`"
fi

if [ $? -ne 0 -o "$SHAK_TAPENUMBER" = "X" ]; then
 exit 1
fi

export SHAK_TAPENUMBER
SHAK_BACKHOST=localhost
export SHAK_BACKHOST
SHAK_useheader="$SHAK_USEHEADERS"
export SHAK_useheader
header="`${SHAK_LIBPATH}/shak_fileheader $SHAK_TAPENUMBER ${SHAK_LIBPATH}/shak_fixtape`"
SHAK_INDEXFILE="`${SHAK_LIBPATH}/shak_indexfile`"
export SHAK_INDEXFILE
if [ $shak_debug -eq "1" ]; then
   echo "shak_fixtape: default:$header"
fi

#--------- COPY LAST HEADER IN INDEX FILE
if [ "$COPYLASTF" = "1" ]; then
     ${SHAK_LIBPATH}/shak0_read_file ${SHAK_INDEXHOST} ${SHAK_INDEXFILE} 1>/dev/null </dev/null 2>&1
     if [ $? -eq 0 ]; then
        header="`${SHAK_LIBPATH}/shak0_read_file ${SHAK_INDEXHOST} ${SHAK_INDEXFILE} < /dev/null | tail -1`"
     else
       exit 1
     fi
     header="`echo "$header" | sed -e 's@^File[^:]*:@@'`"
     if [ $shak_debug -eq "1" ]; then
       echo "shak_fixtape: from indexfile:$header"
     fi
fi
     
#---------- COPY LAST OR SPECIFIED HEADER ON TAPE
if [ "$COPYLASTT" = "1" ]; then

ans="yes"
echo "This program uses the bsf operation, it may be slow."
echo -n "Do you want to continue (yes|no) <$ans> "
read ans

if [ .$ans = . ]; then
   ans="yes"
fi

if [ "$ans" = "yes" -o "$ans" = "YES" ]; then
    :
else
  exit 1
fi

    bsfcount="`expr 1 + \( $COPYNHEADER + 1 \) \* 2 `"
    
    errorstring="`${SHAK_LIBPATH}/${SHAK_MT} -f ${SHAK_TAPEDEVICE} ${SHAK_MT_EOM} 2>&1`"
    if [ ! $? -eq 0 -o "$errorstring" ]; then
      echo "shak_fixtape: move to end-of-recorded-media failed." 1>&2
      exit 1
    fi
   
    newtape="0"
    errorstring="`${SHAK_LIBPATH}/${SHAK_MT} -f ${SHAK_TAPEDEVICE} bsf $bsfcount 2>&1`"
    if [ ! $? -eq 0 -o "$errorstring" ]; then
	exit 1
        newtape="1"
# this exit means you can't fix a tape with no backup on it, you have to start over.       
       errorstring="`${SHAK_LIBPATH}/${SHAK_MT} -f ${SHAK_TAPEDEVICE} rewind 2>&1`"
        if [ ! $? -eq 0 -o "$errorstring" ]; then
           echo "shak_fixtape: rewind failed." 1>&2
           exit 1
        fi
    fi

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

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



if [ "$TAPEONLY" = "1" ]; then
     errorstring="`${SHAK_LIBPATH}/${SHAK_MT} -f ${SHAK_TAPEDEVICE} ${SHAK_MT_EOM} 2>&1`"
     if [ ! $? -eq 0 -o "$errorstring" ]; then
       echo "shak: move to end-of-recorded-media failed." 1>&2
       exit 2
     fi

     errorstring="`echo "$header" | ${SHAK_LIBPATH}/shak0_write_tape`"
     if [ ! $? -eq 0 -o "$errorstring" ]; then
       echo "shak: write tape failure." 1>&2
       exit 2
     fi
fi     



if [ "$FILEONLY" = "1" ]; then
     num="`${SHAK_LIBPATH}/shak0_read_file ${SHAK_INDEXHOST} ${SHAK_INDEXFILE} </dev/null | ${SHAK_LIBPATH}/shak0_getlastindex`"
     
     if [ $num -eq 0 ]; then 
       num="` expr $num + 1 `" 
     else
       num="` expr $num + 2 `" 
     fi 

    if [ $? -eq 0 ]; then
        echo "`${SHAK_LIBPATH}/shak_makeindexentry $num "$header"`" |
	${SHAK_LIBPATH}/shak0_append_file ${SHAK_INDEXHOST} ${SHAK_INDEXFILE}
     else
       exit 1
     fi 
fi
     
     
exit 0

