#!/bin/csh -f 
# 
# dumdum: script to do incremental dumps 
#

onintr quit
umask 7

# filesystems to be dumped on level 0 and 1
set LOLEV =  "root usr net"

# filesystems to be dumped at every level
set ALWAYS = "usr net"

# dump program to use 
set DUMP = "/etc/dump"

# restore program to use
set RESTORE = "/etc/restore"

# density of the tape
set DEN = "1600"

# device to dump to
set DUMPDEV = "/dev/mt0"

#directory where all dump information is stored
set DUMPDIR = "/usr/local/adm/dumpdir"
cd ${DUMPDIR}

#set up temporary file names
set FLAGDIR = "${DUMPDIR}/flagdir"
if (!(-d $FLAGDIR )) mkdir $FLAGDIR 
set FLAGFILE = "${FLAGDIR}/dumpit"
set FNAME = "tmp"
set TEMPFILE = "${FLAGDIR}/${FNAME}$$"

# get the sequence information from the WHICHDUMP file
set which = `cat ${DUMPDIR}/WHICHDUMP`
set DATEFILE = "${DUMPDIR}/DUMPDATES"
@ lessone = ${#which} - 1
set nonomatch

start:

#if the last dump did not finish, restart it
set junk = (`ls ${FLAGDIR}`)
set isnotdone = (${junk})
if (${#isnotdone} >= 1) then
	echo "THIS IS THE CONTINUATION OF AN ABORTED DUMP"
	echo "Continuing with a level ${which[${which[${#which}]}]} dump."
	echo " "
	echo -n "Is this correct? "
	set junk = $<
	while ((${junk} != "yes") && (${junk} != "no") &&  \
	    (${junk} != "y") && (${junk} != "n"))
		echo -n "Please answer yes or no: "
		set junk = $<
	end
	if ((${junk} == "n") || (${junk} == "no")) then
		rm -f ${FLAGDIR}/*
		goto start
	endif
	set restart
	goto getlevel
else

# increment the position counter (last element of array)
	@ which[$#which]++

# if the position counter points to its own position in the array, recycle
# back to the beginning of the array
	if (${which[${#which}]} == ${#which}) set which[${#which}] = 1
recover:
	echo ""
	echo -n "My records"
	if (${?limit}) echo -n " now"
	echo -n " indicate that this should be a "
	echo " level ${which[${which[${#which}]}]} dump."
	echo -n "Is this correct? "
	set junk = $<
	while ((${junk} != "yes") && (${junk} != "no") &&  \
  	    (${junk} != "y") && (${junk} != "n"))
		echo -n "Please answer yes or no: "
		set junk = $<
	end
	if ((${junk} == "yes") || (${junk} == "y")) then
		echo $which > ${DUMPDIR}/WHICHDUMP

# set the level - lolevel contains the number without sublevel information
getlevel:
		set level = ${which[${which[${#which}]}]}
		set lolevel = `echo $level | tr -d "a-z"`
	else
#
# list the dump sequence and corresponding numbers
#
		echo ""

		@ limit = $#which /  5 
		set counter = 1
		while ($counter <= $limit) 
			set temp = $counter;
			set i = 1
				while ($i <= 5) 
					echo -n "${temp}==$which[$temp]"
					if ($temp == $which[$#which]) then
						echo -n "*"
					endif
					echo -n "	"
					@ temp += 9;
					@ i++
				end
				echo ""
				@ counter++;
		end

		echo "* marks the dumps I thought we were supposed to do."
		echo ""
		echo -n "What is the number of the correct dump? "


# get the correct dump number
		set junk = $<
		while ((${junk} < "1") || (${junk} >= "${#which}"))
			echo "Please choose a number between 1 and ${lessone}\!"
			echo -n "What is the number of the correct dump? "
			set junk = $<
		end
		set which[${#which}] = ${junk}
		goto recover
	endif
endif

echo ""

if ((${lolevel} == "0") || (${lolevel} == "1")) then
	set fs = "${LOLEV}"
else
	set fs = "${ALWAYS}"
endif

echo "Doing these file systems: $fs"

echo " "

# cycle for each filesystem that needs to be done
foreach filesystem ( ${fs} )
	if (!(-e ${FLAGFILE}.${filesystem})) then
		unset restart

#look up raw device name in /etc/fstab
		if ($filesystem == "root") then
			set DEV = `awk -F: '$2 ~ /\/$/ { print $1}'\
				 /etc/fstab`
		else
			set DEV = `awk -F: '$2 ~ /'${filesystem}'$/\
				 { print $1}' /etc/fstab`
		endif

#do the dump
		echo -n "Type 'yes' when the /${filesystem} tape is online: "
		set junk=$<
		while ((${junk} != "yes") && (${junk} != "y"))
			echo -n "Type 'yes' when the /$filesystem tape is online:"
			set junk=$<
		end

		echo "Doing the dump...."
		${DUMP} ${lolevel}udf ${DEN} ${DUMPDEV} ${DEV} |& tee $TEMPFILE

		touch ${FLAGFILE}.${filesystem}

	else
		echo "Apparently the ${filesystem} dump has been done."
	endif

# do the dumptoc
	if (!(-e ${FLAGFILE}.r${filesystem})) then

		echo -n "Doing the dumptoc..."
		${RESTORE} tf ${DUMPDEV} > ${DUMPDIR}/lev${level}.${filesystem}
		touch ${FLAGFILE}.r${filesystem}
		echo "Done"
		if (-z ${DUMPDIR}/lev${level}.${filesystem}) then
			echo "PANIC\!"
			echo "${DUMPDIR}/lev${level}.${filesystem} is empty\!"
		endif
		echo -n "Level ${level} /${filesystem}       " >> ${DATEFILE}
		echo -n `date` >> ${DATEFILE}
		echo " by ${USER}" >> ${DATEFILE}
	else
		echo "And the dumptoc for ${filesystem} has been done also."
	endif
end

rm -f ${FLAGFILE}.*
	
goto clean

quit:
echo ""
echo "The dump has been killed.... I hope you know what you are doing\!"
echo ""

clean:
rm -f ${FLAGDIR}/${FNAME}*
cd
