:
#	@(#) dumpsave.sh 23.5 91/08/29 
#
#	Copyright (C) 1988-1991 The Santa Cruz Operation, Inc.
#		All Rights Reserved.
#	The information in this file is provided for the exclusive use of
#	the licensees of The Santa Cruz Operation, Inc.  Such users have the
#	right to use, modify, and incorporate this code into other products
#	for purposes authorized by the license agreement provided they include
#	this notice and the associated copyright notice with any such product.
#	The information in this file is provided "AS IS" without warranty.
#
#	Copyright (c) 1984, 1986, 1987, 1988 AT&T
#	  All Rights Reserved

#	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
#	The copyright notice above does not evidence any
#	actual or intended publication of such source code.

# save a system memory image dump from /dev/swap to tape or floppy

# shell variables used:
#
#       DEV     "f" = floppy; "t" = tape
#
#       OF      output device file used by dd
#       BS      block size used by dd, in 512 byte units
#       COUNT   number of blocks to be copied by dd
#       SKIP    number of blocks for dd to skip
#
#       NB      number of BS size blocks on tape/disk
#       N       number of BS size blocks of memory to copy
#

PATH=/bin:/usr/bin
DEFAULT=/etc/default/boot

remove_dump_exit()
{
	echo 'Do you want to remove the dump from the swap device now? (y/n) \c'
	while 
		read ans
		[ "$ans" ]
	do
		case $ans in
		y)	break ;;
		n)	exit 0 ;;
		*)	echo '? (y/n) \c' ;;
		esac
	done
	dd if=/dev/swap of=/dev/swap \
		bs=1k skip=2 seek=2 count=1 conv=swab 2>/dev/null
	exit 0
}

/etc/memsize /dev/swap > /dev/null 2>&1
case $? in
1)	echo "memsize : Error reading from swap device"
	exit 1
	;;
2)	exit 0		# no dump image in swap device
	;;
esac

echo 'There may be a system dump memory image in the swap device.'
echo 'Do you want to save a copy of it? (y/n) \c'
while 
	read ans
	[ "$ans" ]
do
	case $ans in
	  y )   break ;;
	  n )   remove_dump_exit ;;
	  * )   echo '? (y/n) \c' ;;
	esac
done

echo "\n"

TAPE=
while true
do
	echo "Use Floppy Drive 0 (/dev/rfd0) by default.\n"
	echo "Press <Return> to use the default device."
	echo "Enter valid Floppy Drive number to use if different from default."
	echo "Enter \"t\" to use tape drive."
	echo "> \c"
	
	read ans
	case $ans in
	""|0)
		FDRIVE=/dev/rfd0
		break
		;;
	1)
		FDRIVE=/dev/rfd1
		[ -w $FDRIVE ] && break
		;;
	t)
		TAPE=yes
		break
		;;
	*)
		FDRIVE=/dev/rfd${ans}
		[ -w $FDRIVE ] && break
		echo "No such device : $FDRIVE"
	esac
done

if [ ! "$TAPE" ]
then
	while :
	do
		echo '\nEnter type of floppy :'
		echo '  1 - double density 360 KB diskettes'
		echo '  2 - quad density 1.2 MB diskettes'
		echo '  3 - double density 720 KB diskettes'
		echo '  4 - high density 1.44 MB diskettes'
		echo '  n - no, QUIT'
		echo '> \c'
		read ans
		case $ans in
		  1 )   OF=${FDRIVE}48ds9	BS=18 NB=40     DEV=f ; break ;;
		  2 )   OF=${FDRIVE}96ds15  	BS=30 NB=80     DEV=f ; break ;;
		  3 )   OF=${FDRIVE}96ds9  	BS=18 NB=80     DEV=f ; break ;;
		  4 )   OF=${FDRIVE}135ds18  	BS=36 NB=80     DEV=f ; break ;;
		  n )   remove_dump_exit ;;
		esac
		echo '???'
	done
else
	while :
	do
		echo '\nEnter choice of tape drive :'
		echo '	1 - /dev/rct0'
		echo '	2 - /dev/rctmini'
		echo '	n - no, QUIT'
		echo '> \c'
		read ans
		case $ans in
		  1 )   OF=/dev/rct0     BS=120 NB=10000  DEV=t ;
			tape status /dev/xct0 2>&1 > /dev/null
			if [ "$?" -eq 0 ] 
			then 
				[ -w "$OF" ] && break
			else
				echo "No tape device : $OF"
			fi
			;;
		  2 )   OF=/dev/rctmini  BS=20 NB=2000  DEV=t ;
			tape status /dev/xctmini 2>&1 > /dev/null
			if [ "$?" -eq 0 ] 
			then 
				[ -w "$OF" ] && break ;
			else
				echo "No tape device : $OF"
			fi
			;;
		  n )   remove_dump_exit ;;
		esac
	done
fi

# while :
# do
#	echo 'How many megabytes of memory image do you want to save?'
#	echo 'Enter decimal integer, or q to quit. > \c'
#	read ans
#	case $ans in
#	  q )   remove_dump_exit ;;
#	esac
#	N=`expr \( $ans \* 2048 + $BS - 1 \) / $BS`
#	case $? in
#	  0 )   break;;
#	esac
#	echo '???'
# done

SKIP=0
COUNT=$NB
N=`/etc/memsize /dev/swap`
N=`expr \( \( $N + 511 \) / 512 + $BS - 1 \) / $BS`

while [ $N -gt 0 ]
do
	if [ $COUNT -gt $N ]
	then
		COUNT=$N
	fi
	echo 'Insert \c'
	case $DEV in
	  f )   echo 'diskette \c' ;;
	  t )   echo 'tape cartridge \c' ;;
	esac
	echo 'and press <Return>, or enter q to quit. > \c'
	read ans
	case $ans in
	  q )   remove_dump_exit ;;
	esac
	echo 'Please wait.'
	echo dd if=/dev/swap of=$OF bs=${BS}b count=$COUNT skip=$SKIP
	dd if=/dev/swap of=$OF bs=${BS}b count=$COUNT skip=$SKIP
	N=`expr $N - $COUNT`
	SKIP=`expr $SKIP + $COUNT`
done
echo '\n\nDone.  Use /etc/ldsysdump to copy dump from tape or diskettes.'
remove_dump_exit
