#!/bin/sh
#
# checktape [-h] [host:]tapeDev
#
#	Check that the filesets on distribution tape are readable by tar.
#
#
#############################################################################
#
#	Copyright (C) Cadence Design Systems, Inc. All rights
#	reserved.  Unpublished -- rights reserved under the
#	copyright laws of the United States of America.
#
#			RESTRICTED RIGHTS LEGEND
#	Use, duplication, or disclosure by the Government is subject
#	to restrictions as set forth in subparagraph (c)(l)(ii) of 
#	the Rights in Technical Data and Computer Software clause 
#	at DFARS 52.227-7013.
#
#			Cadence Design Systems, Inc.
#			555 River Oaks Parkway
#			San Jose, CA 95134	USA
#
#############################################################################
BOURNE=/bin/sh
XTERMPATH=/usr/openwin/bin
XTERMBIN=xterm
XTERMOPTS="-T 'SoftLoad 6.0 Console' -fn 9x15  -sl 200 -g 80x40+0+0 -bg \#e0e0e0 -fg \#800000 -sb -e ${BOURNE} softload_GUI"
sysPATH=/bin:/usr/bin:/usr/ucb:$XTERMPATH

jsr=''

Clear() {
    clear		2>/dev/null	#beware of bad termcap entry
}

Page() {
    MORE=""  more
}

EchoN() {
    echo -n "$*"
}

Beep() {
    $jsr EchoN ""	2>/dev/null
}


Dirname() {					#print argument minus leaf, or .
    case $1 in
	*/* )	echo $1 | sed 's@/[^/]*$@/@' ;;
	*)	echo "." ;;
    esac
}

Leafname() {					#print leaf
    case $1 in
	*/* )	echo $1 | sed 's@.*/@@' ;;
	*)	echo $1 ;;
    esac
}

Fullpath() {					#print rooted path to arg dir
    ( cd $1 && /bin/pwd |
	sed 's@^/tmp_mnt/@/@' )			#print automount access dir
}

Dirpath() {					#print rooted path to arg - leaf
    $jsr Fullpath `$jsr Dirname $1`
}

Suffix() {
    echo $1 | sed -n 's@.*\.@@p'
}

Prefix() {
    echo $1 | sed 's@\.[^.]*$@@'
}


LegalVendor() {
    OSVER=`/bin/uname -r`
    OSTYPE=`/bin/uname -s`
    test $OSTYPE = "SunOS" && test $OSVER -lt 5
    return $?
}



PATH=$sysPATH;    export PATH

if [ "X$1" = "X-h" ]; then
    headerOnly=true; shift
else
    headerOnly=false
fi

if [ -z "$1" ]; then
    echo "Usage: $0 [-h] [host:]tapeDev"				1>&2
    exit 1
fi
dataLoc=$1;	shift

cd `$jsr Dirname $0`		|| exit 1

$jsr LegalVendor || {
    echo "Error: unexpected architecture"				1>&2
    exit 1
}


openMedia="$BOURNE  media_cntrl $$ open"
readMedia="$BOURNE  media_cntrl $$ read"
closeMedia="$BOURNE media_cntrl $$ close"

$jsr EchoN "Press [Return] when tape is mounted on $dataLoc "
read junk < /dev/tty
echo "Please wait ..."

if $BOURNE read_label $dataLoc > /tmp/label$$; then
    . /tmp/label$$                  #source /tmp/label$$
    tapeset=$dataTapeset
    tapenum=$dataTapenum
    rm /tmp/label$$
else
    rm /tmp/label$$
    exit 1
fi
echo "tape ${tapeset}:${tapenum}"

$headerOnly && exit 0

$openMedia -r $dataLoc || exit 1

ioCmnd="./io_fltr x"
tarCmnd="./vtar tfB -"
fscnt=0
while [ $fscnt -lt $dataFilesets ]; do
    echo "Checking FS${fscnt}..."

    cmndStat=/tmp/stat$$; rm -f $cmndStat
    ( $readMedia $fscnt || echo $? > $cmndStat ) | $ioCmnd | $tarCmnd >/dev/null
    if [ $? -ne 0 -o -f $cmndStat ]; then
	rm -f $cmndStat
	exit 1
    fi
    rm -f $cmndStat

    sleep 2				#to avoid "device busy", sometimes...
    fscnt=`expr $fscnt + 1`
done
echo "Rewinding ..."
$closeMedia
