:
#	@(#) tapedump.sh 1.4 89/07/10 
#
#	Copyright (C) The Santa Cruz Operation, 1987.
#	This Module contains Proprietary Information of
#	The Santa Cruz Operation, Microsoft Corporation
#	and AT&T, and should be treated as Confidential.
#

# TAPEDUMP
# Name - dumps magnetic tape to an output file.
# Syntax - tapedump [-a|-e][-h|-o][-btsn num] <tape_device> <output_file>.

BARG=""
TARG=""
SARG=""
CVAL=""
OVAL=""
NVAL=""
tape_device=""
output_file=""

    set -- `getopt aeohb:t:s:n: $*`
    for i in $*
    do
        case $i in
	
     	    -b) UNIT=`expr $2 : '[0-9]\{1,\}\(.*\)'` 
		case "$UNIT" in
		"")	UNIT=b;
			;;
		b|k|w)	UNIT="";
			;;
		*)	echo "$0 : -b : Illegal block size factor"
			exit 2
			;;
		esac
		BARG="bs=$2$UNIT";
		shift ; shift ;;
	    -n) NARG="count=$2" ; shift ; shift ;;
            -s) SARG="skip=$2" ; shift ; shift ;;
	    -a) CVAL="conv=ascii" ; shift ;;
	    -e) CVAL="conv=ebcdic" ; shift ;;
	    -h) OVAL="| hd -axbx " ; shift ;;
	    -o) OVAL="| hd -aobo " ; shift ;;
            -t) TVAL=$2 ; shift ; shift ;;
            --) shift ; break ;;
	esac
    done
    case $# in
        0) echo "Usage: $0 [-a|-e] [-o|-h] [-btsn num] tape_device [ output_file ]" >&2
           exit 2 ;;
        1) tape_device=$1 ;;
        2) tape_device=$1 
           output_file="> $2" ;;
    esac
 #
 #  The -t option calls the tape command. The tape first is rewound,
 #  and thereafter, wound forward according to the file mark specified
 #  in the argument to t. i.e. tapedump -t 3 will be moved forward 
 #  three times.
 #
    dev=`expr $tape_device : '\(.*/\).*'`
    ct0=`expr $tape_device : '.*/r\(.*\)'`
    xdevice="$dev"x"$ct0"
    if [ -r "$xdevice" ] 
    then
         tape rewind $xdevice;
    else
         echo "$0 : Invalid tape device $tape_device or use raw device."
	 exit 2;
    fi
    while [ "$TVAL" -gt 0 ]
    do
        tape rfm $xdevice 
        TVAL=`expr $TVAL - 1`
    done

    eval dd if=$tape_device  $BARG $NARG $CVAL $SARG $OVAL $output_file
