toupper()
{
    echo $1 | tr '[a-z]' '[A-Z]'
}

getdev()
{
    set `ls /dev/rmt/*n`
    NO=$#
    echo "Select no-rewind tape device"
    i=1
    while [ $i -le $NO ]
    do
    echo "${i}:\\t$1"
    shift
    i=`expr $i + 1`
    done
    set `ls /dev/rmt/*n`
    resp=0
    while [ $resp -eq 0 ] || [ $resp -gt $NO ]
    do
    echo "Enter Choice from 1 - ${NO}: \c"
    read resp
    case $resp in
        1) TAPEDEV=$1
            ;;
        2) TAPEDEV=$2
            ;;
        3) TAPEDEV=$3
            ;;
        4) TAPEDEV=$4
            ;;
        5) TAPEDEV=$5
            ;;
        6) TAPEDEV=$6
            ;;
        7) TAPEDEV=$7
            ;;
        8) TAPEDEV=$8
            ;;
        9) TAPEDEV=$9
            ;;
        *) echo "Error -- \c"
            resp=0
    esac
    done
    if [ `echo $TAPEDEV | grep "/dev/rmt/c.s.n" | wc -l` -gt 0 ]
    then
    TAPECNTL=/usr/lib/tape/tapecntl
    TAPEARG=
    else
    TAPECNTL=/usr/lib/scsi/tapecntl
    TAPEARG=y
    fi
    }
checktape()
{
tput clear
echo "
 ###############################################################################
 #                                                                             #
 #          This script will check the readability of a 3B2 Unix               #
 #          SYSV R 3.*.* Distrbution Tape, 3B2 Risc SYSV R 4.*.*               #
 #          Distribution and Add-on Tapes, a 6X86 SYSV R4 Distribution         #
 #          Tape, or any CPIO tape.                                            #
 #          It will check the Boot Area on 3B2 Tapes, the CPIO Area on         #
 #          all Tapes, the No of Installable Packages, No. of images and       #
 #          images on 3B2 SYSV R 3.*.* Tapes and the PKGADD packages on        #
 #          SYSV R4 Tapes. It will also list the available packages on         #
 #          either 3B2 SYSV R 3.*.* or 3B2/6x86 SYSV R 4.*.* tapes.            #
 #                                                                             #
 #                                               Code by W. G. Simeon          #
 #                                                                             #
 ###############################################################################
Hit <enter> to continue\c"
read junk
tput clear

    
    getos()
    {
    FAIL=0
    echo "Which OS Tape are you checking?
        A:    3B2 3.x.x Operating System Utilities
        B:    3b2 4.x.x Operating System Utilities 
        C:    3b2 4.x.x Operating System Add-on Utilities
        D:    Unix SYSV/X86 4.x.x Distribution Tape
        E:    CPIO format tape
        F:    List packages on a 4.0 Add-on Utilities Tape
        G:    List packages on a 3B2 3.x.x Operating System Utilities Tape
        Q:    quit
    enter choice: \c"
    read os
    OS=`toupper $os`
    if [ $OS = "Q" ]
    then
    OS=
    CH=
    main
    fi
    if [ $OS = "C" ] || [ $OS = "D" ] || [ $OS = "F" ] && [ `uname -r | grep '4\.0' | wc -l` -ne 1 ]
    then
    echo "NOTICE: -- a 4.x.x os tape may be checked ONLY on a 4.x.x System"
    sleep 2
    FAIL=1
    fi
    }
    
    chkboot()
    {
    echo "Checking Boot Block"
    if dd if=$TAPEDEV of=/dev/null bs=16b 2>/dev/null
    then
    echo "Boot Block read OK"
    else
    echo "Boot Block ERROR"
    $TAPECNTL -w$TAPEARG $TAPEDEV
    exit 1
    fi
    }
    
    chkrel()
    {
    echo "Testing for Package Name"
    cpio -icumd < $TAPEDEV > /dev/null 2>&1
    ver=`grep UNIX /tmp/pkglist | cut -c 27-30,39-41`
    echo "This is `cat /tmp/pkglist`"
    }
    
    chkcpio()
    {
    echo "Checking CPIO Area"
    if [ "$OS" = "E" ]
    then
    LEN=`echo $TAPEDEV | wc -c`
    RWLEN=`expr $LEN - 2`
    RWTAPEDEV=`echo $TAPEDEV | cut -c1-$RWLEN`
    else
    RWTAPEDEV=`echo $TAPEDEV`
    fi
    if cpio -ictC5120 -I${RWTAPEDEV} >/dev/null 2>&1
    then
    echo "CPIO read OK"
    else
    echo "CPIO read ERROR"
    exit 1
    fi
    }


    chkpkg()
    {
    case $OS in
        A)    osa
            ;;
        C)    osc
            ;;
        D)    osc
            ;;
        *)    echo "Error - unable to chkpkg on OS $OS"
            exit 2
            ;;
    esac
    }
    
    osa()
    {
    echo "Getting pkglist"
    cd /tmp
    if cpio -icdumC5120 -I${TAPEDEV} 2>/dev/null
    then
    PKG=`wc -l /tmp/pkglist|cut -d" " -f6`
    DSK=0
    for i in `cat /tmp/pkglist | cut -d"%" -f2`
    do
    DSK=`expr $DSK + $i`
    done
    echo "This tape contains $PKG installable packages, comprised of"
    echo "$DSK floppy images."
    else
    echo "Could not get pkglist"
    $TAPECNTL -w$TAPEARG $TAPEDEV
    rm -f /tmp/pkglist
    exit 1
    fi
    echo "Checking the Following Packages"
    cat /tmp/pkglist | sed 's/%.*$//' | pr -2tn5w80 | sed 's/=/ /g'
    echo "Please Wait"
    num=0
    while true
    do                                          
    BLK=`dd if=$TAPEDEV of=/dev/null bs=16b 2>&1`  #Read record and count blocks 
    BLKS=`echo $BLK | cut -d" " -f1`
    if [ "$BLKS" = "88+1" ]                        #count blocks read
    then
    num=`expr $num + 1`
    else 
    break
    fi
    done
    if [ $DSK -eq $num ]                          # Check no read against $DSK
    then
    echo "$DSK images read - Packages Check OK"
    rm -f /tmp/pkglist
    else
    rm -f /tmp/pkglist
    echo "Package Check FAILED"
    echo "Only $num of $DSK Images Readable"
    echo "Rewinding Tape"
    $TAPECNTL -w$TAPEARG $TAPEDEV
    exit 1
    fi
    }
    
    
    osc()
    {
    echo "
	Available packages will now be spooled into a Filesystem/temp/spool
    directory. After all packages are read in, the temp files will be deleted."
    echo "
	Enter the FILESYSTEM to hold the temp files, or hit return for Available
    Space -- NOTICE -- avoid using the Root (/) file system if possible!! \c"
    read FSYS
    while [ -z "$FSYS" ]
    do
    echo "Available file space on this machine is:\n\n"
    DFSPACE=`type dfspace|cut -d" " -f3`
    if [ -s "$DFSPACE" ] && [ -x "$DFSPACE" ]
    then
    $DFSPACE 
    else
    df -t | pg
    fi
    echo "Which file system should be used to temporarily store the packages?"
    read FSYS
    done
    echo "You should see a list of Installable Packages available on the tape."
    mkdir -p $FSYS/temp/spool  #make temp dir to avoid symlink usr/tmp->var/tmp
    if [ $TAPECNTL = "/usr/lib/scsi/tapecntl" ]
    then
    PTAPE=qtape1
    else
    PTAPE=ctape1
    fi
    pkgadd -s $FSYS/temp/spool -d $PTAPE all
    if [ "$?" != "0" ]
    then
    tput bel
    echo "Error in Tape"
    rm -rf $FSYS/temp/spool
    rmdir $FSYS/temp > /dev/null 2>&1 # remove temp dir if empty 
    $TAPECNTL -w$TAPEARG $TAPEDEV
    exit 4
    fi
    tput bel
    echo "Tape Checks OK -- wait -- Removing temp files and Rewinding Tape"
    rm -rf $FSYS/temp/spool
    rmdir $FSYS/temp > /dev/null 2>&1 # remove temp dir if empty
    $TAPECNTL -w$TAPEARG $TAPEDEV
    }
    
    getlist4()
    {
    if [ $TAPECNTL = "/usr/lib/scsi/tapecntl" ]
    then
    PTAPE=qtape1
    else
    PTAPE=ctape1
    fi
    pkginfo -d $PTAPE -x | pg
    }
    
    getlist3()
    {
    echo "Positioning Tape"
    $TAPECNTL -${TAPEARG}p 2 $TAPEDEV
    cd /tmp
    if cpio -icvdum < $TAPEDEV 2>/dev/null
    then
    cat /tmp/pkglist | sed 's/%.*$//' | pr -2tn5w80 | sed 's/=/ /g'
    rm -f /tmp/pkglist
    echo "Rewinding Tape"
    $TAPECNTL -w$TAPEARG $TAPEDEV
    else
    echo "Couldn't find pkglist"
    rm -f /tmp/pkglist
    $TAPECNTL -w$TAPEARG $TAPEDEV
    exit 1
    fi
    }
    
    cmain()
    {
    getos
    if [ $FAIL -eq 1 ]
    then
    CH=
    OS=
    main
    fi
    getdev
    if [ $OS = "F" ] || [ $OS = "C" ]
    then
    echo "Insert Tape containing Add-on Utilities, hit return when ready \c"
    elif [ $OS = "E" ]
    then
    echo "Insert CPIO Tape, hit return when ready \c"
    else
    echo "Insert Operating System Utilities Tape, hit return when ready \c"
    fi
    read junk
    trap "$TAPECNTL -w$TAPEARG $TAPEDEV;exit 1" 1 2 3 15  # rewind and exit 
    echo "Rewinding Tape"
    $TAPECNTL -w$TAPEARG $TAPEDEV
    if [ $? -ne 0 ]
    then
    exit 1
    fi
    case $OS in
        A)    chkboot
            chkcpio
            chkpkg    
              ;;
        B)    chkboot
            chkcpio
              ;;
        C)    chkpkg
              ;;
        D)    chkrel
            chkcpio
            chkpkg
              ;;
        E)    chkcpio
              ;;
        F)    getlist4
              ;;
        G)    getlist3
              ;;
        *)    echo ERROR -- Unknown OS $OS
            exit 1
              ;;
    esac
    if [ "$OS" != "E" ]
    then
    echo "Rewinding Tape"
    $TAPECNTL -w$TAPEARG $TAPEDEV # rewind the tape
    fi
    echo "Checktape Finished - Tape checks OK"
    }
    AGAIN=Y
    while [ "$AGAIN" = "Y" ]
    do
    cmain
    AGAIN=
    echo "Do you wish to check another tape? <y/n> \c"
    read AGAIN
    if [ `toupper $AGAIN` = "Y" ]
    then 
    cmain
    else
    tput clear
    AGAIN=
    fi
    echo "Do you wish to perform another action? <y/n> \c"
    ans=""
    read ans
    if [ `toupper $ans` = "Y" ]
    then
    ans=""
    CH=""
    main
    fi

    done
}

dupetape()
{
TERM=${TERM:-ansi}
export TERM
tput clear
echo "
 ###############################################################################
 #                                                                             #
 #          This script will duplicate a 3B2 Unix SYSV R 3.*.*                 #
 #          Distrbution Tape, 3B2 Risc SYSV R 4.*.* Distribution and           #
 #          Add-on Tapes, a 6X86 SYSV R4 Distribution Tape, or any CPIO        #
 #          tape. Files will be packed or compressed (depending on what        #
 #          is available on your system) to try and reduce the space           #
 #          required. Try and pick the filesystem with the most space,         #
 #          as tape files tend to be very large!                               #
 #          This script makes no distinction as to the media - i.e.            #
 #          it will allow you to duplicate a 60mb tape on a 120mb drive,       #
 #          effectivly converting the tape to 120mb.                           #
 #          CAUTION: Unix Distribution Tapes (Operating System Utilities       #
 #          and Operating System Add-on Utilities) should ONLY be made on      #
 #          a 60mb drive for compatibility.                                    #
 #                                                                             #
 #                                               Code by W. G. Simeon          #
 #                                                                             #
 ###############################################################################
Hit <enter> to continue\c"
read junk
tput clear


tapein()
{
echo "Insert tape to be $ACTION, hit <enter> to continue\c"
read junk
trap "$TAPECNTL -w$TAPEARG $TAPEDEV ;rm -rf ${TAPEDIR}/*${FNAME}*;exit 1"  1 2 3 15
num=1     
if [ -z "$COMP" ] || [ "$COMP" = "pack" ]
then
echo "Reading file number $num"
while `dd if=$TAPEDEV of=${TAPEDIR}/${num}_${FNAME}_${num} bs=16k 2>/dev/null` 
do
if [ "$COMP" = "pack" ]
then
$COMP ${TAPEDIR}/${num}_${FNAME}_${num}
fi
num=`expr $num + 1`
echo "Reading file number $num"
done
elif [ "$COMP" = "compress" ]
then
echo "Reading File $num"
while `dd if=$TAPEDEV bs=16k | $COMP 2>/dev/null > ${TAPEDIR}/${num}_${FNAME}_${num}.Z`
do
num=`expr $num + 1`
echo "Reading File $num"
done
fi
$TAPECNTL -w$TAPEARG $TAPEDEV # rewind tape
trap 1 2 3 15
}

tapeout()
{
echo "Insert tape to be $ACTION, hit return to continue\c"
read junk
trap "$TAPECNTL -w$TAPEARG $TAPEDEV ;exit 1"  1 2 3 15
num=0            
echo "Do you wish to erase the tape before writing the files? <y/n> \c"
read ans
if [ `toupper $ans` = "Y" ]
then
echo "Erasing Tape"
$TAPECNTL -e$TAPEARG $TAPEDEV
if [ $? -ne 0 ]
then
echo "Erase Failed"
trap 1 2 3 15
exit 1
fi
fi
ans=""

echo "Do you wish to retension the tape before writing the files? <y/n> \c"
read ans
if [ `toupper $ans` = "Y" ]
then
echo "Retensioning Tape"
$TAPECNTL -t$TAPEARG $TAPEDEV
if [ $? -ne 0 ]
then
echo "Retension Failed"
trap 1 2 3 15
exit 1
fi
fi
ans=""

if [ `ls $TAPEDIR/*${FNAME}*.Z 2>/dev/null | wc -l ` -gt 0 ]
then
COMP=compress
elif [ `ls $TPAEDIR/*${FNAME}*.z 2>/dev/null | wc -l` -gt 0 ]
then
COMP=pack
else
COMP=""
fi
echo "Placing Files on Tape"
cd $TAPEDIR
for file in `ls *${FNAME}* | sort -n`
do
if [ "$COMP" = "compress" ]
then
echo "Uncompressing and writing file $file"
zcat $file | dd of=$TAPEDEV bs=16k 2>/dev/null # copy files out
elif [ "$COMP" = "pack" ]
then
echo "Unpacking and writing file $file"
pcat $file | dd of=$TAPEDEV bs=16k 2>/dev/null # copy files out
else
echo "Writing file $file"
dd if=$file of=$TAPEDEV bs=16k 2>/dev/null # copy files out
fi
done
echo "Rewinding Tape"
$TAPECNTL -w$TAPEARG $TAPEDEV # rewind tape
echo "Do you wish to remove these files from the hard disk? <y/n> \c"
read ans
if [ `toupper $ans` = "Y" ]
then
rm -f ${TAPEDIR}/*${FNAME}*
fi
ans=""
trap 1 2 3 15
}

getfsys()
{
if [ $ACTION = "read" ]
then
echo "
    Enter the Directory to hold the tape files, or hit return for Available
    Space -- NOTICE -- avoid using the Root (/) file system if possible!!"
    read TAPEDIR
    while [ -z "$TAPEDIR" ]
    do
    echo "Available file space on this machine is:\n\n"
    df -t
    echo "Which Directory should be used to store the tape files?"
    read TAPEDIR
    done
    if [ ! -d $TAPEDIR ]
    then
	if `mkdir -p $TAPEDIR`
	then
	   :
	else
	   echo "Failed to make directory $TAPEDIR"
	   exit 1
	   fi
    fi
    echo "Enter a filename to be assigned to the files \c"
    read FNAME
    FNAME=${FNAME:-tape}
elif [ $ACTION = "written" ]
then
echo "
    Enter the Directory containing the files to be copied to tape."
    read TAPEDIR
    if [ ! -d $TAPEDIR ]
    then
    echo "No such Directory $TAPEDIR"
    exit 1
    fi
    echo "Enter the filename of the files to be copied"
    read FNAME
fi
}

getcomp()
{
echo "Do you wish to compress the files as they are read? <y/n>"
read ans
if [ `toupper $ans` = "Y" ]
then
if `type compress > /dev/null`
then
COMP=compress
elif `type pack > /dev/null`
then
COMP=pack
else
echo "You have neither compress or pack - files will not be compressed!"
COMP=""
fi
fi
ans=
}

tape_to_tape()
{
echo "I am assuming that you have two tape drives on this system!"
echo "Choose the drive to be used for the SOURCE tape: "
getdev
SRC=$TAPEDEV
SRCCTRL=$TAPECNTL
SRCARG=$TAPEARG
echo "Chose the drive to be used for the TARGET tape: "
getdev
TGT=$TAPEDEV
TGTCTRL=$TAPECNTL
TGTARG=$TAPEARG
if [ "$SRC" = "$TGT" ]
then
echo "You cannot use the same drive for both Source and Target!"
tape_to_tape
fi
echo "Insert the tapes in the correct drives, hit return when ready:
Source Drive = $SRC
Target Drive = $TGT"
read junk
echo "Do you wish to erase the Target Tape before writing it? <y/n> \c"
ans=""
read ans
if [ `toupper $ans` = "Y" ]
then
echo "Erasing Tape"
$TGTCTRL -e$TGTARG $TGT
if [ $? -ne 0 ]
then
echo "Erase Failed"
trap 1 2 3 15
exit 1
fi
fi
ans=""
echo "Do you wish to retension the Target Tape before writing it? <y/n> \c"
read ans
if [ `toupper $ans` = "Y" ]
then
echo "Retensioning Tape"
$TGTCTRL -t$TGTARG $TGT
if [ $? -ne 0 ]
then
echo "Retension Failed"
trap 1 2 3 15
exit 1
fi
fi
ans=""

echo "Copying Tape $SRC to Tape $TGT"
num=1
echo "Transferring File $num"
while `dd if=$SRC of=$TGT bs=8k 2>/dev/null `
do
num=`expr $num + 1`
echo "Transferring File $num"
done
echo "Rewind Tapes - Please Wait"
$SRCCTRL -w$SRCARG $SRC
$TGTCTRL -w$TGTARG $TGT
echo "Tapes Duplicated"
ans=""
}

tmain()
{
ans=""
while [ "$ans" = "" ]
do
echo "
		  A:   Read in a tape
		  B:   Write out a tape
		  C:   Tape to Tape Copy (Requires two tape drives)
		  Q:   Quit

	      Enter Choice \c"
read ans
case `toupper $ans` in
		A) tput clear
		   getcomp
		   ACTION="read"
		   getdev
		   getfsys
		   tapein
		     ;;
		B) tput clear
		   ACTION="written"
		   getdev
		   getfsys
		   tapeout
		     ;;
		C) tput clear
		   tape_to_tape
		     ;;
		Q) CH=""
		   ans=""
		   main
		     ;;
		*) echo "Invalid Choice $ans"
		   ans=""
		     ;;
esac
done
}
if [ -z "$ans" ]
then
tmain
else
ans=""
echo "Do you wish to perform another Tape Duplication action? <y/n> \c"
read ans
if [ `toupper $ans` = "Y" ]
then
ans=""
tmain
fi
ans=""
echo "Do you wish to perform another action? <y/n> \c"
read ans
if [ `toupper $ans` = "Y" ]
then
ans=""
main
else
exit 0
fi
fi
}

###MAIN PROGRAM LOOP###
main()
{
TERM=${TERM:-ansi}
export TERM
tput clear

while [ -z "$CH" ]
do
echo "Which Function do you wish to perform?
	1. Check a Tape
	2. Duplicate a Tape
	3. Erase Tape
	4. Rewind Tape
	Q. Exit

Your Choice (1,2,3,4,Q) \c"
read CH
    case $CH in
        1)    checktape
              ;;
        2)    dupetape
              ;;
	3)    getdev
	      $TAPECNTL -e $TAPEDEV
	      ;;
	4)    getdev
	      $TAPECNTL -w $TAPEDEV
	      ;;
	q|Q)    exit 0
	      ;;
        *)    echo "ERROR -- Invalid Choice $CH - Enter one of 1, 2, or 3"
            CH=
              ;;
    esac
done
}
main
exit 0

