#!/bin/csh
# backup.disc by Julian H. Stacey
echo Populating target disc with a bootable FreeBSD + Xwindows.

# Where possible I have used constructs such as
#	( cd $BACKUP_TARGET ; rm unrooted file name )
# so that if $BACKUP_TARGET doesnt exist, originals in /whatever will not be destroyed.
# Later some of these sets will become env vars

echo The next bit blows up if you havent done these preceeding
echo "	setenv BACKUP_TARGET /mnt"
echo "	setenv SMALL_TARGET /usr3"
switch (${BACKUP_TARGET})
	case ${SMALL_TARGET}:
		# echo BACKUP_TARGET is ${SMALL_TARGET}
		echo TO DO /usr/local/sbin/faxgetty is not yet copied
		breaksw
	case /a:
		# echo BACKUP_TARGET is /a
		breaksw
	default:
		# echo BACKUP_TARGET was $BACKUP_TARGET
		set BACKUP_TARGET=/a#  DANGER - NEVER LET TARGET BE /
		# echo BACKUP_TARGET now is $BACKUP_TARGET
		breaksw
	endsw  
if ( $BACKUP_TARGET == "${SMALL_TARGET}" ) then
	set size=80
	set amount=small
	set zap="rm -f "
	set zap_r="rm -rf "
else
	set size=650
	set amount=big
	set zap=true
	set zap_r=true
endif
	# Put * in brackets  ( $zap_r crap.* ) to avoid match failure 
echo Target Amount $amount, name $BACKUP_TARGET

echo The next line blows up if you havent done a preceeding
echo "	setenv TMP /usr/tmp"
if ( "${TMP}_" == "_" ) then
	set tmp=/usr2/tmp
else
	set tmp=${TMP}
endif
echo tmp is $tmp

set	owner_name=`logname`#		`whoami` returns root, dont want that
#	owner_name			machine owners name
set	owner_dir=home/$owner_name
#	owner_dir			home of owners personal important dot
#					files, no leading slash
#					still need something better, but for
set	kernel=kernel
#	kernel				becomes kernel with 2.0
set	truncate=/usr/local/bin/truncate
set	clone_dir=$tmp/clone.`date -u "+%Y-%m-%dT%H:%M:%SZ"`

mkdir -p $BACKUP_TARGET

df
echo	"	amount:		$amount"
echo	"	size:		$size"
echo	"	BACKUP_TARGET:		$BACKUP_TARGET"
echo	"Push <Control D> once you are SURE you will be targeting correct disc,"
echo	"and you dont mind losing your backup config for such as hylafax."
set	x=`cat /dev/tty`
# even now user must answer next question too, before we zap_r his disc.
echo	" "
echo	"Needs to be run as root else permissions on target will be wrong,"
echo	"can also be run as a normal user, for test purposes."
echo	"You are currently `whoami`."
echo	"	owner_name:	$owner_name"
echo	"	owner_dir:	$owner_dir"
echo	"	kernel:		$kernel"
echo	"	truncate:	$truncate"
echo	"	clone_dir:	$clone_dir"
echo	" "
# echo	"Assuming the $size disc was created by such as:"
# echo	"	xs disklabel -w -r -B sd2 80m_raw
# # man 8 disklabel:
# # If the -r flag is given, the disk sectors containing the label and
# # bootstrap will be written directly.
# echo	"	disklabel -w -r /dev/rsd3s2c i486_80"
# echo	"	newfs -d 0 -n 1 -m 0 -o space /dev/rsd3a"
# echo	"	tunefs -m 0 /dev/rsd3s2a"
# echo	"	mount /dev/sd3a $BACKUP_TARGET"
echo 	" "
echo Looking to see if $size disc is mounted on target:
mount | grep "$BACKUP_TARGET "
echo	"Push <Control D> to continue."
set	x=`cat /dev/tty`
#	"Information:	Pre-existant sym links on target cause reports of"
#	"	Could not create symlink to ... : File exists"
echo
# echo	"Expected run time: 14 Minutes"

# First go somewhere safe
mkdir -p $tmp
cd $tmp ; cd /usr/tmp ; cd /var/tmp
# Change somewhere safer,
# do a mkdir in case not mounted but user pushed <return> (above).

cd $BACKUP_TARGET		# absolutely vital, dont screw this !
				# if BACKUP_TARGET is null this will   cd ~

if ( `pwd`  != $BACKUP_TARGET ) then # {
	echo "CWD should be but is not $BACKUP_TARGET"
	exit
else	# }{
	echo "Passed CWD check."
endif # }

switch (${BACKUP_TARGET})
	#  desk specific
	case /a:
		echo "Seagate ST41200N, 1 Gig,sd2s2 sectors 972800" > ${BACKUP_TARGET}/LABEL.JHS
		breaksw
	case /usr2:
		echo "Seagate ST41200N, 1 Gig,sd2s3 sectors 976985" > ${BACKUP_TARGET}/LABEL.JHS
		breaksw
	case ${SMALL_TARGET}:
		echo "SEAGATE ST1096N 76.5 Megabyte, Partition 76.5 Megabyte" > ${BACKUP_TARGET}/LABEL.JHS
		breaksw
	default:
		echo "200 Megabyte Disc, Partition 200 Megabyte, MAXTOR LXT-213S" > ${BACKUP_TARGET}/LABEL.JHS
		breaksw
	endsw  

set	stuff="$kernel bin etc root sbin lkm"
	echo	"Copying $stuff from / to $BACKUP_TARGET"
	echo "Note $kernel will be copied, but not other kernels `echo $kernel.*`"
	( $zap_r etc/uucp >& /dev/null )
	( cd / ; tar cf - $stuff ) | tar xpf - | grep -v "Could not create symlink"
	find . -type f -name \*.core -print -exec rm {} \;
	( strip $kernel	>& /dev/null )
	( strip *bin/* >& /dev/null )		# blows on bin/[
	echo	"Finished $stuff"
	echo

set	stuff="var"
	echo	"Copying $stuff from / to $BACKUP_TARGET"
	mkdir -p $clone_dir
		pushd $clone_dir
		( cd / ; tar cf - $stuff ) | tar xpf - | grep -v "Could not create symlink"
		find . -type f -name \*.core -print -exec rm {} \;
		# optionally save a little space
		echo preserve
		( $zap $stuff/tmp/*	>& /dev/null )
		( $zap_r $stuff/db/pkg	>& /dev/null )
		( $zap $stuff/spool/fax/log/*	>& /dev/null )
		( $zap $stuff/tmp/vi.recover/*	>& /dev/null )
		( $zap_r $stuff/preserve/*	>& /dev/null )
		# ( $zap_r $stuff/spool/uucp	>& /dev/null )
		( $zap_r $stuff/games	>& /dev/null )
		find $stuff/log -type f -print | xargs $truncate
		echo crash
		( $zap_r $stuff/crash/ram* $stuff/crash/system* >& /dev/null )
		$truncate $stuff/cron/log* $stuff/log/messages*
		ls -l $stuff/db/kvm_kernel* # dunno what to do about these
		$truncate $stuff/db/locate.database
		echo spool/fax
		# ( $zap_r $stuff/spool/fax $stuff/tmp/*	>& /dev/null )
		popd
	echo "Copying $clone_dir to $BACKUP_TARGET"
	(cd $clone_dir;tar cf - $stuff)|tar xpf - | grep -v "Could not create symlink"
	rm -rf $clone_dir
	echo	"Finished $stuff"
	echo

set dirs="tmp mnt usr usr/tmp usr1 usr2 usr3 usr4 cd/_ a/var a/usr"
foreach i ( $dirs )	# {
        if ( -d $i ) then 	# {
                echo "$i already exists."
        else 	# }{
                if ( -e $i ) then # {
                        # echo "Error: a non-directory exists: $i"
                else # }{
                        mkdir -p $i
                endif # }
        endif 	# }
	end     # }

chmod 1777 tmp usr/tmp
chmod 755 mnt usr* cdrom*

set	stuff=dev
	echo " "
	echo	"Copying $stuff from / to $BACKUP_TARGET"
	rm -rf $stuff.bak
	mv $stuff $stuff.bak # avoids many Could not make dev/... : File exists
	# ( cd / ; tar cf - $stuff ) | tar xpf - \
	#	| grep -v "Could not create symlink"
	# tar wont take the big minor numbers of sd slices, so use cpio
	# To create an archive:
	# ( cd /$stuff ; ls | cpio --create --format=newc \
	# 	> $BACKUP_TARGET/$stuff.cpio )
	# To extract an archive:
	# ( mkdir $stuff ; cd $stuff ; cpio --extract --make-directories \
	# 	--preserve-modification-time --unconditional \
	# 	<  $BACKUP_TARGET/$stuff.cpio )
	( cd /$stuff ; ls | cpio --create --format=newc ) | \
	( mkdir $stuff ; cd $stuff ; cpio --extract --make-directories \
		--preserve-modification-time --unconditional )
	find $stuff -type f -name \*.core -print -exec rm {} \;
	echo	"Finished $stuff"
	echo

echo Creating a minimal fstab.
mv etc/fstab etc/fstab.3disk
echo "/dev/sd0a / ufs rw 1 1"	> etc/fstab
# grep sd0a etc/fstab.3disk	> etc/fstab

mkdir usr tmp

set	maybe="Considering removing"
set	stuff='bin lib libexec libdata mdec sbin'
	echo " "
	echo	"Copying $stuff from / to $BACKUP_TARGET/usr"
	mkdir -p $clone_dir
		pushd $clone_dir
		mkdir usr
		(cd /usr ; tar cf - $stuff) | ( cd usr ; tar xpf -) \
			| grep -v "Could not create symlink"
		( $zap_r usr/lib/chinese usr/bin/chinese>& /dev/null )
		find . -type f -name \*.core -print -exec rm {} \;
		find usr/*bin usr/libexec -type f -exec strip {} \; >& /dev/null
			# lots of things like usr/bin/uusched are bourne,
			# so wont strip
			echo
		# echo "$maybe 470K `( $zap_r usr/include/g++ >& /dev/null )`"
		# echo "$maybe 1M1-470K `( $zap_r usr/include >& /dev/null )`"
		( $zap_r usr/lib/OLD	>& /dev/null )
		( $zap usr/lib/*_p.a	>& /dev/null )
		# ( $zap usr/lib/*.a	>& /dev/null )
		# ( $zap_r usr/libexec/uucp	>& /dev/null )
		echo "$maybe 10M ncd xterm stuff `( $zap_r usr/lib/x11/ncd >& /dev/null )`"
		popd
	echo "Copying $clone_dir to $BACKUP_TARGET"
	( cd $clone_dir ; tar cf - usr ) | tar xpf - \
		| grep -v "Could not create symlink"
	rm -rf $clone_dir
	echo	"Finished $stuff"
	echo

	echo " "
set	stuff='usr/include'	# Omit for 80m disc
	if ( $amount == "big" ) then # {
		echo "Doing $stuff."
		echo	"Copying $stuff from / to $BACKUP_TARGET/$stuff"
		(cd / ; tar cf - $stuff) | tar xpf - \
			| grep -v "Could not create symlink"
		# note h flag to tar causes loads of errors like:
		#	include/nfs/nfs/nfs : Too many levels of symbolic links
		find $stuff -type f -name \*.core -print -exec rm {} \;
		echo	"Finished $stuff"
		echo
	else # }{
		echo "Not doing $stuff."
	endif # }

	echo " "
set	stuff='usr/local'	# Omit for 80m disc
	if ( $amount == "big" ) then # {
		echo	"Copying $stuff from / to $BACKUP_TARGET/$stuff"
		mkdir -p $clone_dir
			pushd $clone_dir
			(cd / ; tar cf - $stuff) | tar xpf - \
				| grep -v "Could not create symlink"
			find $stuff -type f -name \*.core -print -exec rm {} \;
			echo "$maybe 20M `( $zap_r $stuff/lib/ghostscript >& /dev/null )`"
			strip $stuff/bin/*	>& /dev/null
				# things like $stuff/bin/kbd_american wont strip
			echo "$maybe $stuff/slip/logs"
			rm -rf $stuff/slip/logs
			popd
		echo "Copying $clone_dir to $BACKUP_TARGET"
		( cd $clone_dir ; tar cf - . ) | tar xpf - \
			| grep -v "Could not create symlink"
		rm -rf $clone_dir
		echo	"Finished $stuff"
		echo
	else # }{
		echo "Not doing $stuff."
	endif # }

set	stuff='usr/share'
	echo " "
	echo	"Copying $stuff from / to $BACKUP_TARGET/$stuff"
	mkdir -p $clone_dir
		pushd $clone_dir
		(cd / ; tar cf - $stuff) | tar xpf - \
			| grep -v "Could not create symlink"
		find $stuff -type f -name \*.core -print -exec rm {} \;
		( $zap_r $stuff/games	>& /dev/null )		# 837
		( $zap_r $stuff/dict	>& /dev/null )		# 3.4M
		( $zap_r $stuff/examples	>& /dev/null )	# 100K
		( $zap_r $stuff/groff_font	>& /dev/null )	# 800K
		( $zap_r $stuff/tmac	>& /dev/null )	# 470K
		echo "$maybe 3.4M man `( $zap_r $stuff/man >& /dev/null )`"
		echo "$maybe 825K zipcodes `( $zap_r $stuff/misc/zipcodes >& /dev/null )`"
		echo "$maybe 825K termcap.db `( $zap_r $stuff/misc/termcap.db >& /dev/null )`" # 688K
		echo "$maybe 400K FAQ `( $zap_r $stuff/FAQ >& /dev/null )`"
		echo "$maybe 260K zoneinfo `( $zap_r $stuff/zoneinfo >& /dev/null )`"
		echo "$maybe 5.5M doc `( $zap_r $stuff/doc >& /dev/null )`"
		echo "$maybe	73K sgml `( $zap_r $stuff/sgml >& /dev/null )`"
		echo "$maybe info `( $zap_r $stuff/info >& /dev/null )`"
		popd
	echo "Copying $clone_dir to $BACKUP_TARGET"
	( cd $clone_dir ; tar cf - . ) | tar xpf - \
		| grep -v "Could not create symlink"
	rm -rf $clone_dir
	echo	"Finished $stuff"
	echo

#	echo " "
#set	stuff='usr/local'	# 140 M.byte	# Omit for 80m disc
#	# if ( $amount == "big" ) then # { Omit for desk as gate is fallback 
#	if ( $amount == "never" ) then # {
#		echo "Doing $stuff."
#		echo	"Copying $stuff from / to $BACKUP_TARGET/$stuff"
#		mkdir -p $clone_dir
#			pushd $clone_dir
#			(cd / ; tar cf - $stuff) | tar xpf - \
#				| grep -v "Could not create symlink"
#			find $stuff -type f -name \*.core -print -exec rm {} \;
#			( $zap_r $stuff/interviews	>& /dev/null )	# 9M
#			( $zap_r $stuff/newsspool	>& /dev/null )	# 11K _if_ empty
#			( $zap_r $stuff/lib/pcemu	>& /dev/null )	# 1M
#			( $zap_r $stuff/lib/perl5	>& /dev/null )	# 2.8M
#			( $zap_r $stuff/lib/povray	>& /dev/null )	# 3.4M
#			( $zap_r $stuff/lib/scotty	>& /dev/null )	# 2.0M
#			( $zap_r $stuff/lib/tkined	>& /dev/null )	# 2.0M
#			( $zap_r $stuff/lib/schemetoc	>& /dev/null )	# 3.2M
#			( $zap_r $stuff/lib/gcl-1.1	>& /dev/null )	# 2.6M
#			( $zap_r $stuff/lib/emacs	>& /dev/null )	# 17 M
#			( $zap_r $stuff/lib/texmf	>& /dev/null )	# 7.4M
#			echo "$maybe `( $zap_r $stuff/info >& /dev/null )`"
#			strip $stuff/bin/*	>& /dev/null
#			popd
#		echo "Copying $clone_dir to $BACKUP_TARGET"
#		( cd $clone_dir ; tar cf - . ) | tar xpf - \
#			| grep -v "Could not create symlink"
#		rm -rf $clone_dir
#		echo	"Finished $stuff"
#		echo
#	else # }{
#		echo "Not doing $stuff."
#	endif # }

set	uncopied="obj dos games src dest ingres $owner_name news p ports public tmp"
# above used to use ' not "
	echo	"Omitting:"
	echo	"	( $uncopied )"
	echo	"Finished $stuff"
	echo

set	stuff='usr/X11R6'
	echo " "
	echo	"Copying $stuff from / to $BACKUP_TARGET/$stuff"
	mkdir -p $clone_dir/$stuff
		pushd $clone_dir/$stuff
		(cd /$stuff ; tar cf - .) | tar xpf - \
			| grep -v "Could not create symlink"
		echo "Remove other Xservers (save 23M)"
		(cd bin ; $zap \
			XF86_8514*	\
			XF86_AGX*	\
			XF86_Mach32*	\
			XF86_Mach64*	\
			XF86_Mach8*	\
			XF86_Mono*	\
			XF86_P9000*	\
			XF86_S3*	\
			XF86_W32*	\
			Xmark		\
			Xnest		\
			XF86_VGA16	\
			) # XF86_SVGA
		(cd bin ; $zap 		\
			Mosaic		\
			SuperProbe	\
			netscape	\
			xmille		\
			xmine		\
			xminesweep	\
			xrobots	)
		find . -type f -name \*.core -print -exec rm {} \;
		( $zap_r lib/Server	>& /dev/null )
		( $zap_r lib/X11/fonts/chinese	>& /dev/null )	# 1.7M
		( $zap_r lib/X11/fonts/misc	>& /dev/null )	# 3.7M (more chinese etc)
		( $zap_r lib/X11/cxterm-dicts	>& /dev/null )	# 304K
		( $zap_r lib/X11/pcb	>& /dev/null )	# 90K
		( $zap_r lib/X11/xlife	>& /dev/null )	# 384K
		( $zap_r lib/X11/xhtml	>& /dev/null )	# 31K
		( $zap_r lib/X11/cxterm-dicts	>& /dev/null )	# 304K
		( $zap_r lib/X11/cxterm-dicts	>& /dev/null )	# 304K
		( $zap_r lib/X11/fonts/Type1	>& /dev/null )	# 1.1M
		( $zap lib/X11/*_p.a	>& /dev/null )
		( $zap lib/X11/*.a	>& /dev/null )
		( $zap_r man	>& /dev/null )	# 3M man
		( $zap_r lib/X11/fonts/100dpi	>& /dev/null )	# 1834
		( $zap_r bin/cit2tit bin/cxterm bin/tit2cit	>& /dev/null )	# chinese
		( $zap_r bin/gxditview bin/hzimpath bin/xv	>& /dev/null ) # contrib
		( $zap_r lib/X11/PEX lib/X11/fonts/PEX include/X11/PEX5	>& /dev/null )
		( $zap_r lib/X11/golddig lib/X11/games/xpilot \
			lib/X11/games/xtrek lib/X11/games \
			lib/X11/xboing include/xview	>& /dev/null )
		( $zap_r lib/X11/games lib/golddig \
			lib/xboing	>& /dev/null )
		( $zap_r	XF86_8514 XF86_AGX XF86_Mach32 XF86_Mach8 XF86_Mono \
			XF86_P9000 XF86_S3 XF86_SVGA XF86_VGA16	>& /dev/null )
			# keep X@ -> XF86_SVGA
		( $zap_r include	>& /dev/null )	# 2.8M
		echo $maybe crud installed from ported packages.
		strip bin/*	>& /dev/null
		echo "Compressing X-Windows bin stuff"
		( cd bin ; find . -type f -print | gzip )
		echo "Decompressing essential X-Windows bin stuff"
		( cd bin ; gzip -d XF86_SVGA fvwm xdm xinit xterm )
		popd
	echo "Copying $clone_dir to $BACKUP_TARGET"
	( cd $clone_dir ; tar cf - . ) | tar xpf - \
		| grep -v "Could not create symlink"
	rm -rf $clone_dir
	echo	"Finished $stuff"
	echo

echo	"Looking to see if there are multiple emacs versions"
ls $BACKUP_TARGET/usr/local/lib/emacs
ls $BACKUP_TARGET/usr/local/lib/xemacs*
( $zap_r usr/local/lib/emacs	>& /dev/null )

	echo " "
echo	"Copying $owner_name environment stuff"
set	stuff=".[a-zA-Z]* bin dots  private "
	echo	"Copying $stuff from / to $BACKUP_TARGET/"
	mkdir -p $owner_dir
	( cd /$owner_dir ; tar cf - $stuff ) | ( cd $owner_dir ; tar xpf - )  \
		| grep -v "Could not create symlink"
	( $zap_r $owner_dir/.MCOM-cache	>& /dev/null )	# 23K
	find $owner_dir -type f -name \*.core -print -exec rm {} \;
	echo	"Finished $stuff"
	echo

# we can set the fastboot flag, because when we boot, we boot only this f.s.
# (cd $BACKUP_TARGET; date -u +%Y-%m-%dT%H:%M:%SZ	> fastboot)

	echo " "
echo Copying this script to $BACKUP_TARGET disc for reference.
cp $0 $BACKUP_TARGET
echo	"Remember disc has confidential info such as"
echo	"	/etc/master.passwd & /etc/uucp/*"
echo	"Remember this disc might have no swap space."
echo	"Remember these /usr things were not copied : $uncopied."
