#!/bin/sh
# (c) Copyright 1994 James H. Lowe, Jr.
# $Id: shak_newtape_v 1.37 1997/01/04 01:23:39 jhl Exp $
#
#
# usage: arg1 is date string
# usage: arg2 is program name


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

if [ "$shak_rootonly0" = yes -a \( "$SHAK_REALLOGIN" != root -o "$SHAK_USERLOGIN" != root \) ]; then
      echo "shak_newtape: you must be root to do this." 1>&2
      exit 1
fi

export SHAK_LIBPATH
undoc1=0
stdout="0"
while [ $# -ge "1" ]
do
   case $1 in
      -c)
	stdout="1";
	shift;
        ;;
      -y)
	undoc1=1;
	shift;
        ;;
      
      -*)
         echo "newtape: invalid args" 1>&2
	 exit 2
         ;;
      *)
         break
         ;;
    esac
done

datestring="$1"
progname="$2"

case $# in
0) echo "Usage: shak_newtape "date_string" [ backup_pgm ]" 1>&2
       exit 2
   ;;
1) 
   fullname=""
   ;;

2)
  echo $progname | egrep '^(/|\.)' 1>/dev/null 
  if [ $? -ne 0 ]; then
     name="`basename $progname`"
     fullname="${SHAK_USERPATH}/${name}"
  else
      fullname="$progname"
  fi
  if [ ! -r ${fullname} ]; then
    echo " shak_newtape: ${fullname} not found."
    exit 2
  fi
   ;;
esac

SHAK_useheader=$SHAK_USEHEADERS
export SHAK_useheader

ans="yes"
TMP4V="${SHAK_LIBPATH}/shak0_header"

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

if [ $stdout = "1" ]; then  

	if [ \( ! "$SHAK_TAPECAPACITY" \) -o "$SHAK_TAPECAPACITY" = "$SHAK_DTAPECAPACITY" ]; then
 		echo ""
		echo -n "Enter tape capacity in number of 1024 byte blocks: "
		read SHAK_TAPECAPACITY
		export SHAK_TAPECAPACITY
	fi
	

	if [ \( ! "$SHAK_TAPENUMBER" \) -o "$SHAK_TAPENUMBER" = "NULL" ]; then
  		echo ""
  		echo -n "Enter tape number: "
		read shak_tapenumber
		SHAK_TAPENUMBER="`echo $shak_tapenumber | awk '{ printf ("%04s\n",$1) }'`"
		export SHAK_TAPENUMBER
	fi


	if [ "$fullname" ]; then
		header="`${TMP4V} $SHAK_TAPENUMBER "$datestring" "${fullname}"`"
	else
		header="`${TMP4V} $SHAK_TAPENUMBER "$datestring"`"
	fi
	echo -n "$header"
	exit 0
fi


if [ "$shak_expert" = "0" ]; then
  echo "shak_newtape:"
  cat <<!!
  This program writes a header at the beginning of the tape.
  Information on the tape will be lost !
!!
  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
fi

FLAG=0
if [ \( ! "$SHAK_TAPECAPACITY" \) -o "$SHAK_TAPECAPACITY" = "$SHAK_DTAPECAPACITY" ]; then
    case $shak_expert in
      0)	
 	echo ""
	echo -n "Enter tape capacity in number of 1024 byte blocks: "
	read SHAK_TAPECAPACITY
	export SHAK_TAPECAPACITY
	FLAG=1
        ;;
      *)
	echo "shak_newtape: tapecapacity not set." 1>&2
        exit 1
	;;
     esac
fi
	

if [ \( ! "$SHAK_TAPENUMBER" \) -o "$SHAK_TAPENUMBER" = "NULL" ]; then
    case $shak_expert in
      0)	
  	echo ""
  	echo -n "Enter tape number: "
	read shak_tapenumber
	SHAK_TAPENUMBER="`echo $shak_tapenumber | awk '{ printf ("%04s\n",$1) }'`"
	export SHAK_TAPENUMBER
	FLAG=1	
        ;;
      *)
	echo "shak_newtape: tape number not set." 1>&2
        exit 1
	;;
     esac
fi

if [ $FLAG -eq 1 ]; then
	ans="N"
	echo  -n "Insert a new writable tape, hit enter to continue. "
	read ans
fi

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

if [ "$shak_Q" -eq 0 -a $shak_no_run_user -eq 0 ]; then
        errorstring="`sickdelay=$delay_tape export sickdelay; $SHAK_LIBPATH/$SHAK_MT -f ${SHAK_MTDEVICE} rewind 2>&1`"
else
	errorstring=""
	true
fi

if [ $? -eq 0 -a ! "$errorstring" ]; then
 
  SHAK_INDEXFILE="`${SHAK_LIBPATH}/shak_indexfile`"
  if [ ! $? -eq 0 ]; then
    echo "shak_newtape: Invalid indexfile file name." 1>&2 
    exit 1
  fi
  export SHAK_INDEXFILE

  if [ "$fullname" ]; then
     header="`${TMP4V} $SHAK_TAPENUMBER "$1" "${fullname}"`"
  else
     header="`${TMP4V} $SHAK_TAPENUMBER "$1"`"
  fi
 
 
  if [ "$shak_Q" -eq 0 -a $shak_no_run_user -eq 0 ]; then
	if [ "$shak_rootonly0" != yes ]; then
#	echo -n "$header" | ( sickdelay=$delay_writeheader; ${SHAK_LIBPATH}/shak0_write_tape )
		echo -n "$header" | ${SHAK_LIBPATH}/shak0_write_tape
		retval=$?
	elif [ "$shak_rootonly0" = yes -a "$SHAK_REALLOGIN" = root -a "$SHAK_USERLOGIN" = root ]; then
#		echo -n "$header" | ( sickdelay=$delay_writeheader; ${SHAK_LIBPATH}/shak0_write_tape )
		echo -n "$header" | ${SHAK_LIBPATH}/shak0_write_tape
		retval=$?
	else
        	echo "shak_newtape: USER variable is not root." 1>&2
     		exit 2
	fi
  else
	false
  fi

  if [ ! $retval -eq 0 ]; then
    if [ "$shak_Q" -ne 0 -a $shak_no_run_user -ne 0 ]; then
   	exit 0 
    else 
        echo "shak_newtape: header write error" 1>&2
        exit 2
    fi
  else
    ${SHAK_LIBPATH}/shak_makenewindex
    if [ ! $? -eq 0 ]; then
      echo "shak_newtape: shak_makenewindex failed" 1>&2
      exit 1
    fi
    cachefileheader="`${SHAK_LIBPATH}/shak_makeindexentry 0 "$header"`"
    echo "$cachefileheader" |
    ( sickdelay=$delay_readfile; ${SHAK_LIBPATH}/shak0_append_file ${SHAK_INDEXHOST} ${SHAK_INDEXFILE} )
    if [ ! $? -eq 0 ]; then
       echo "shak_newtape: shak0_append_file failure." 1>&2
       exit 1
    fi 
  fi
  if [ $undoc1 -eq 1 ]; then
  	echo "$SHAK_TAPENUMBER" 1>&2
  fi
else
  echo "shak_newtape: rewind failure" 1>&2
  exit 1
fi
exit 0

