#!/bin/sh
#
# duptape [-l] [host:]tapeDev [ tapesetid:tapenum ]
#
#	duplicate a distribution tape.
#
#	If tapesetid:tapenum is not supplied then the filesytsem image will
#	be created from a master tape.  If the -l flag is given, exit
#	immediately after creating filesystem image, otherwise prompt to
#	copy filesystem image to blank tape.
#
#	If tapesetid:tapenum is supplied, then the filesystem image found
#	in IM${tapesetid}.${tapenum}/ will be copied onto tape.
#
#
#############################################################################
#
#	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/bin/X11
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  "$*\c"
}

Beep() {
    tput bel		2>/dev/null
}


Affirmative() {
    while : ; do
	read Affirm
	case $Affirm in
	    [yY]*) return 0 ;;
	    [nN]*) return 1 ;;
	    *)     $jsr EchoN "Please press y (yes) or n (no) : " ;;
	esac
    done
}

Proceed() {
    test "X$1" = 'X-q' || $jsr EchoN "Press [Return] to proceed, q to quit : "
    while : ; do
	read proc < /dev/tty
	case $proc in
	    "")    return 0 ;;
	    [qQ]*) return 1 ;;
	    *) 	   $jsr EchoN "Press [Return] to proceed, q to quit : " ;;
	esac
    done
}


LegalVendor() {
    test "`uname`" = "HP-UX"
    return $?
}



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@\.[^.]*$@@'
}


FlInfo() {
    /bin/ls -ldL $*  |
	sed -e 's@^\(..........\)\([^ 	]\)@\1 \2@'	\
    ;
}
	
FlOwner() {
    $jsr FlInfo $* | awk '{print $3}'
}

FlGroup() {
    $jsr FlInfo $* | awk '{print $4}'
}

FlSize() {
    $jsr FlInfo $* | awk '{print $5}'
}

LinkName() {
    $jsr FlInfo $* | awk '{print $11}'
}

IsSymLink() {
    test -h $1
    return $?
}


DirentExists() {		#non-referencing symLinks included
    test -d $1 -o -f $1 || $jsr IsSymLink $1
    return $?
}
	
FlsNewer() {
    than=`echo $1 | sed 's@[/.]@\\\\&@g'`	#stick in backslashes to escape
    {
	echo ""					#in case none newer
	/bin/ls -Ldtr $* 
    } | sed -e '1,/^'$than'$/d' -e '/^'$than'$/d'
}

FlsOlder() {
    than=`echo $1 | sed 's@[/.]@\\\\&@g'`	#stick in backslashes to escape
    {
	echo ""					#in case none older
	/bin/ls -Ldt $* 
    } | sed -e '1,/^'$than'$/d' -e '/^'$than'$/d'
}


PATH=$sysPATH;    	export PATH

test -z "$Rm"	&& Rm=/bin/rm;		export Rm

useGUI=false
export useGUI
if [ "X$1" = "X-l" ]; then
    loadOnly=true; shift
else
    loadOnly=false
fi

if [ -z "$1" ]; then
    echo "usage: $0 [-l] [host:]tapeDev [tapesetid:tapenum]"		1>&2
    exit 1
fi
dataLoc=$1;	shift
if [ -z "$1" ]; then
    writeOnly=false
else
    writeOnly=true
    tapeset=`echo $1 | sed -n 's@:.*@@p'`
    tapenum=`echo $1 | sed -n "s@.*:@@p"`
    if [ -z "$tapeset" -o -z "$tapenum" ]; then
	echo "usage: $0 [-l] [host:]tapeDev [tapesetid:tapenum]"	1>&2
	exit 1
    fi
    shift
fi
bin_dir=`$jsr Dirpath $0` 		|| exit 1	; export bin_dir

test $# -gt 0 && shift $#	#precaution against interaction w/sh fnxn args


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

dataID=$$
openMedia="$BOURNE  $bin_dir/media_cntrl $dataID open"
readMedia="$BOURNE  $bin_dir/media_cntrl $dataID read"
writeMedia="$BOURNE $bin_dir/media_cntrl $dataID write"
closeMedia="$BOURNE $bin_dir/media_cntrl $dataID close"

if [ "$writeOnly" != true ]; then
    $jsr EchoN "Press [Return] when master tape is mounted on $dataLoc "
    read junk
    echo "Please wait ..."

    junknum=0 #This is used only for CDROM so we just initialize it to 0
              #Since we are duplicating the tape.

    if $BOURNE $bin_dir/read_label $dataLoc $junknum > /tmp/label$$ ; then
	. /tmp/label$$			#source /tmp/label$$
	tapeset=$dataTapeset
	tapenum=$dataTapenum
	rm /tmp/label$$
    else
	rm /tmp/label$$
	exit 1
    fi
    fsdir=IM${tapeset}.${tapenum}
    $Rm -rf $fsdir
    mkdir $fsdir || exit 1
    #
    # create the filesystem image 
    #
    $openMedia -r $dataLoc || exit 1
    echo "Creating filesystem image of tape ${tapeset}:${tapenum}"
    fscnt=0
    while [ $fscnt -lt $dataFilesets ]; do
	echo "Copying FS${fscnt}..."
	$readMedia $fscnt 2> /tmp/readerrs$$ > ${fsdir}/FS.${fscnt} || {
	    cat /tmp/$readerrs$$				1>&2
	    rm -f /tmp/$readerrs$$
	    exit 1
	}
	rm -f /tmp/$readerrs$$
	sleep 2			#stop occasional "device busy" error
	fscnt=`expr $fscnt + 1`
    done
    echo "Rewinding ..."
    $closeMedia
else
    fsdir=IM${tapeset}.${tapenum}
fi

$loadOnly && exit 0


#
# Now copy filesystem image onto tape
#
if [ ! -d $fsdir ]; then
    echo "Error: unable to locate $fsdir"	1>&2
    exit 1
fi

while : ; do
    $jsr EchoN "Copy ${tapeset}:${tapenum} to blank tape? [y/n] "
    if $jsr Affirmative ; then
	$jsr EchoN "verify duplicate? [y/n] "
	if $jsr Affirmative; then
	    verify="yes"
	else
	    verify="no"
	fi
	$jsr EchoN "Press [Return] when blank tape is mounted on $dataLoc "
	read junk

        $openMedia -w $dataLoc || exit 1
	fscnt=0
	while [ -f ${fsdir}/FS.${fscnt} ]; do
	    echo "Copying FS${fscnt}..."
	    $writeMedia $fscnt < ${fsdir}/FS.${fscnt} 2> /tmp/writeErrs$$ || {
		cat /tmp/writeErrs$$				1>&2
		rm -f /tmp/writeErrs$$
		exit 1
	    }
	    rm -f /tmp/writeErrs$$
	    sleep 2			#stop occasional "device busy" error
	    fscnt=`expr $fscnt + 1`
	done
	if $jsr IsSymLink ${fsdir}/FS.${fscnt} ; then
	    echo "Error: unresolved symbolic link"	1>&2
	    /bin/ls -lL ${fsdir}/FS.${fscnt}
	    $closeMedia
	    exit 1
	fi
	echo "Rewinding ..."
	$closeMedia	|| exit 1

	if [ $verify = "yes" ]; then
	    $openMedia -r $dataLoc || exit 1
	    fscnt=0
	    while [ -f ${fsdir}/FS.${fscnt} ]; do
		echo "verifying FS${fscnt}..."
		tapesum=`$readMedia $fscnt 2>/dev/null | sum`
		disksum=`sum < ${fsdir}/FS.${fscnt}`
		if [ "$tapesum" != "$disksum" ]; then
		    echo "Error: verification"				1>&2
		    echo "tapesum: $tapesum 	disksum: $disksum"	1>&2
		    $jsr Proceed || {
			$closeMedia
			exit 1
		    }
		    break
		fi
		fscnt=`expr $fscnt + 1`
	    done
	    echo "Rewinding ..."
	    $closeMedia	|| exit 1
	fi
    else
	break
    fi
done

$jsr EchoN "Remove ${tapeset}:${tapenum} image from filesystem? [y/n] "
$jsr Affirmative && $Rm -r $fsdir

exit 0
