:
#	@(#) diskcmp.sh 22.1 89/11/14 
#
#	Copyright (C) The Santa Cruz Operation, 1985, 1986, 1987, 1988, 1989.
#	This Module contains Proprietary Information of
#	The Santa Cruz Operation and should be treated
#	as Confidential.
#

#
# Floppy disk comparison routine
#
# Diagnostics:
#	Returns 1 on syntax errors, 2 on interrupts
#	

#   	BLSZ is block size arg for dd to use
#	DISK0 is always where the source comes from 
#	SRC is always where the target copy comes from
#	TARGET is always where the target copy goes to
#	SUM is the flag for the argument
#	SSUM is the source sum and TSUM is the target sum

PATH=/etc:/bin:/usr/bin	TEMP=/tmp/disk$$	
BLSZ=9k  		DISK0=/dev/rfd048ds9   	TARGET=
DUALDISK=		DISK1=/dev/rfd148ds9 	SRC=$TEMP
SUM=			SSUM=			TSUM=

usage() {
	echo "
Usage: diskcmp [-d] [-s] [-u] [-96ds9 | -96ds15 | -135ds9 | -135ds18]
	-d		use dual drives
	-s		sum source and target disks (default is no sum)
	-u		print this usage message
	-48ds9		low density 48tpi 5.25in disks
	-96ds9		high density 96tpi 5.25in disks (default is 48tpi)
	-96ds15		quad density 96tpi 5.25in disks (default is 48tpi)
	-135ds9		high density 135tpi 3.25in disks (default is 48tpi)
	-135ds18	quad density 135tpi 3.25in disks (default is 48tpi)"
exit 1
}

#	Trap interrupts, etc for a clean exit.

trap "rm -f $TEMP; echo $0 interrupted; exit 2" 1 2 3 15

#
# FUNCTION DEFINITIONS
#
# Prompt for yes or no answer - returns non-zero for no
getyn() {
	while	echo "\n$* (y/n) \c">&2
	do	read yn rest
		case $yn in
		[yY])	return 0 			;;
		[nN])	return 1			;;
		*)	echo "Please answer y or n" >&2	;;
		esac
	done
}

# Prompt with mesg, return non-zero on q
prompt() {
	while	echo "\n$* \nPress Return to proceed or type 'q' to quit: \c" >&2
	do	read cmd
		case $cmd in
		Q|q)	rm -f $TEMP; exit 1	;;
		"")	break			;;
		*)	continue		;;
		esac
	done
}

#	Send all echos to stderr for the duration of this script.
exec 1>&2

#	Argument processing

while	case $1 in
	"")		break
			;;

	-s)		SUM=1
			;;

	-48ds9)
			;;

	-96ds9)		DISK0=/dev/rfd096ds9
			DISK1=/dev/rfd196ds9
			BLSZ=9k
			;;

	-96ds15)	DISK0=/dev/rfd096ds15
			DISK1=/dev/rfd196ds15
			BLSZ=15k
			;;

	-135ds9)	DISK0=/dev/rfd0135ds9
			DISK1=/dev/rfd1135ds9
			BLSZ=9k
			;;

	-135ds18)	DISK0=/dev/rfd0135ds18
			DISK1=/dev/rfd1135ds18
			BLSZ=9k
			;;

	-d)		DUALDISK=1
			;;

	*)		usage
			;;

	esac
do	shift
done

# set target once drive type is known (48 vs. 96)

TARGET=$DISK0
[ $DUALDISK ] && SRC=$DISK0 TARGET=$DISK1 

#	If single disk, we need to put the floppy on the hard disk.
#	If dual disk, simply prompt with the right drive name.

while	prompt "Please insert the first disk in drive 0."
	trap "rm -f $TEMP; exit 1" 1 2 3 15

	[ "$SUM" ] && {
	SSUM="`dd if=$DISK0 bs=$BLSZ | sum -r`" || {
		echo "Sum of first floppy disk failed."
		SSUM="not available"
		}
	echo "\nSum of first floppy is -- $SSUM --\n"
	}

	[ "$DUALDISK" ] || {
		`dd bs=$BLSZ if=$DISK0 of=$TEMP` || { 
		echo "Error, please try again."
		continue
		}
	}

do
	while prompt "Please insert the second disk in drive ${DUALDISK:-0}."

	do	
		if 	[ "$DUALDISK" ]
		then	cmp $SRC $TARGET
		else	dd bs=$BLSZ if=$TARGET  | cmp - $SRC
		fi
			case $? in
	
			0)	echo "The floppy disks are identical."
				;;
	
			1)	echo "The floppy disks are different."
				;;
	
			*)	echo "System error, please try again"
				continue 
				;;
			esac
	
		[ "$SUM" ] && {
		TSUM="`dd if=$TARGET bs=$BLSZ | sum -r`" || {
			echo "Sum of second floppy disk failed."
			TSUM="not available"
			}
		echo "Sum of second floppy is -- $TSUM --\n"
		[ "$SSUM" != "$TSUM" ] && echo "
WARNING:  Sum of second floppy differs from sum of first floppy."
		}

		#	If dual drives start from beginning again (no 2nd copy)

		[ "$DUALDISK" ] && break

		#	Copy's on disk, ask if they want a another run

		getyn "Do you want to compare another disk to the first?" || break

	done

	#	Should whole process be repeated with new source

	getyn "Do you want to compare two more disks?" || {
		rm -f $TEMP
		exit 0
		}
done

