#!/bin/ksh
#
# gethdr.scr
#
# This script reads the header file (tar format) from a backup tape
# created with the backup.scr script.
#
# If the tape device being used is an 8 mm tape the blocksize is set
# to 1024 for the duration of the read and reset to the previous
# value at the end.  If this script is stopped or aborts the blocksize
# should be manually changed to the previous value.
#
# The format of the tape is as follows:
#
#	BOT
#	header file (format is below)			(tar format)
#	EOF
#	filesystem 1					(backup format)
#	EOF
#	filesystem 2					(backup format)
#	EOF
#	.
#	.
#	filesystem n					(backup format)
#	EOF
#
# Header File Format
#
#	Line 1:  	hostname
#	Line 2:  	date and time
#	Line 3:         Level indicator
#	Lines 4-n+3:	i FILESYSTEM_NAME
#			(where i is 1 for the first, 2 for the second, and
#			 so on.  This is the value of the advance file command
#			 to get this filesystem.)
#
# Parameters:
#
#      p1 : (optional) the tape device to read backups from.  The /dev/
#           qualifier and the .n suffix may be present or absent, i.e.
#           /dev/rmt1.1 is the same as /dev/rmt1 is the same as rmt1.1 is
#           the same as rmt1.  If a .n suffix is used on the device name
#           (i.e. rmt0.3), it MUST be a non-rewinding device, or the
#           successive filesystems will overwrite one another.  The
#           non-rewinding device suffixes are .1, .3, .5, & .7.  If the
#           .n suffix is absent then the value .1 (i.e. $DEFAULT_SUFFIX)
#           is used.  If the parameter is omitted the value /dev/rmt0.1
#           is used.
#
# Exit Status:
#
#	0 : Tape read completed successfully (file is in /tmp/!backuplist)
#	1 : Could not find the header file on the tape
#	92: Error rewinding the tape
#	94: Wrong number of parameters
#	96: Error changing the tape blocksize
#	97: Param 1 must be a tape device on this system
#	98: Param 1 must be a non-rewinding tape device
#
#
DEFAULT_TAPE=/dev/rmt0
DEFAULT_SUFFIX='.1'
DEFAULT_BLOCKSIZE="1024"
USAGE="$0 [optional_tape_device]"
blocksize=-1
basetape=""
#########################################################################
#
# If the selected tape drive is an 8mm unit, this function will set
# the block size of the drive to 1024.  If the current value is
# different it is saved and restored later.
#
set_blocksize()
{
blocksize=-1                                    # -1 means reset unnecessary
if [[ -n `lsdev -Cc tape | grep $basetape | grep 8mm` ]]
  then
  blocksize=`lsattr -E -a block_size -l $basetape | cut -f2 -d' '`
  if [[ $blocksize != $DEFAULT_BLOCKSIZE ]]
    then
    chdev -l $basetape -a block_size=$DEFAULT_BLOCKSIZE >/dev/null
    if [[ $? = 0 ]]
      then
      print "$basetape changed (old blocksize = $blocksize, new = $DEFAULT_BLOCKSIZE)"
    else
      print "Could not change tape blocksize to $DEFAULT_BLOCKSIZE.  Exiting."
      exit 96
    fi
  fi
fi
}
#########################################################################
#
# If the tape blocksize was changed earlier, restore the old
# value.
#
reset_blocksize()
{
if [[ $blocksize != -1 && \
      $blocksize != $DEFAULT_BLOCKSIZE ]]
  then
  chdev -l $basetape -a block_size=$blocksize >/dev/null
  if [[ $? = 0 ]]
    then
    print "$basetape changed (blocksize = previous value of $blocksize)"
  else
    print "Could not change tape blocksize to $blocksize.  Exiting."
    exit 96
  fi
fi
}
#########################################################################
#
# This function rewinds a tape
#
rewind()
{
tctl -f $tapedev rewind
if [[ $? != 0 ]]
  then
  print "Error rewinding $tapedev.  Exiting."
  exit 92
fi
}
#########################################################################
#
# This function checks the parameters and sets up the $tapedev symbol.
#
checkparm()
{
if [[ $savecount > 1 ]]
  then
  print "Wrong number of parameters, usage = $USAGE.  Exiting."
  exit 94
fi
tapedev=${DEFAULT_TAPE}${DEFAULT_SUFFIX}
if [[ $savecount = 1 ]]
  then
  tapedev="/dev/`basename $savep1`"
  if [[ `print $tapedev | cut -c6-8` != "rmt" || \
        -z `ls $tapedev 2>/dev/null` ]]
    then
    print "Sorry, parm 1 must specify a tape device on your system.  Exiting."
    exit 97
  fi
  if [[  -z `print $tapedev | grep "\."` ]]
    then
    tapedev=$tapedev${DEFAULT_SUFFIX}
    else
    suffix=`print $tapedev | cut -f2 -d'.'`
    if [[ $suffix != 1 && \
          $suffix != 3 && \
          $suffix != 5 && \
          $suffix != 7 ]]
      then
      print "Sorry, parm 2 must specify a non-rewinding tape device."
      exit 98
    fi
  fi
fi
basetape=`basename $tapedev | cut -f1 -d'.'`
}
#########################################################################
#
# main
#
savecount=$#
savep1=$1
checkparm					# check the parms
rewind						# rewind the tape
set_blocksize					# set the tape blocksize
cd /tmp
mv !backuplist !backuplist.save.$$ 2>/dev/null	# save previous file if exists
tar -xf $tapedev !backuplist
reset_blocksize					# restore the tape blocksize
if [[ -s !backuplist ]]
  then
###  print "Tape contents file is in /tmp/!backuplist"
  chmod 644 !backuplist
  exit 0
  else
  print "Sorry, could not read !backuplist from the tape."
  exit 1
fi
exit 0
