:
#ident "@(#)dumpsave.sh	1.7 91/02/01"
#
#	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

clear_swap() {
	# Clear bootinfo magic number.  The following dd command writes
	# 3KB of nulls assuming that the bootinfo structure resides
	# within the first 3KB of the swap device.  See BOOTINFO_LOC
	# and struct bootinfo in <sys/bootinfo.h>.
	echo "\000\c" | \
	dd of=/dev/swap bs=3k count=1 conv=sync >/dev/null 2>&1
}

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

echo '\tThere may be a system dump memory image in the swap device.'
echo '\tDo you want to save it? (y/n)> \c'
while 
	read ans
	[ "$ans" ]
do
	case $ans in
	  y )   break ;;
	  n )   echo "\n"
		clear_swap
		exit 0 ;;
	  * )   echo '???' ;;
	esac
done

echo "\n"

TAPE=
while true
do
	echo "\tUse Floppy Drive 0 (/dev/rfd0) by default\n"
	echo "\tPress ENTER to use default device."
	echo "\tEnter valid Floppy Drive number to use if different."
	echo "\tEnter \"t\" to use tape."
	echo "\t> \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 '\t\nEnter type of floppy :'
		echo '\t  1 - double density 360 KB diskettes'
		echo '\t  2 - quad density 1.2 MB diskettes'
		echo '\t  3 - double density 720 KB diskettes'
		echo '\t  4 - high density 1.44 MB diskettes'
		echo '\t  n - no, QUIT'
		echo '\t> \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 )   clear_swap
			exit ;;
		esac
		echo '???'
	done
else
	while :
	do
		echo '\t\nEnter choice of tape drive :'
		echo '\t\t1 - /dev/rct0'
		echo '\t\t2 - /dev/rctmini'
		echo '\t\tn - no, QUIT'
		echo '\t> \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 $OF 2>&1 > /dev/null
			if [ "$?" -eq 0 ] 
			then 
				[ -w "$OF" ] && break ;
			else
				echo "No tape device : $OF"
			fi
			;;
		  n )   clear_swap
			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 )   exit 0 ;;
#	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 '\tInsert \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 )   clear_swap
		exit 0 ;;
	esac
	echo '\tWait.'
	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\n\tDone.  Use /etc/ldsysdump to copy dump from tape or diskettes'
echo '\tPress return to continue >\c'
read ans
clear_swap
exit 0
