#
#	Initalise the variables for files & dirs
#
function initvars {
	#
	#	check that the root device is mounted 
	#
	NEWROOT=$(mount | grep "$ROOTDEV"|cut -d\  -f3)
	if [ x$NEWROOT = x ];then
		echo 1>&2 ">>> $ROOTDEV is not mounted"
		echo 1>&2 ">>> giving up"
		cleanup
		exit 1
	fi
	echo 1>&2 ">>> " found $ROOTDEV mounted on $NEWROOT
	BOOTDSK=/usr/src/ide-usb/usbboot/mnt
	INITRD=/usr/src/ide-usb/usbboot/mnt/initrd.img
	RAMDSK=/usr/src/ide-usb/usbboot/usbimage
	TMPIMG=/usr/src/ide-usb/usbboot/image.usb
	LINUXRC=$RAMDSK/linuxrc
	LINUXRC1=$RAMDSK/linuxrc.1
	INITRD=$BOOTDSK/initrd.img
	LILOCNF=$BOOTDSK/etc/lilo.conf
	MSGFILE=$BOOTDSK/boot/message
	USBMODULES="usbcore.o usb-uhci.o ide-usb.o"
	LIBDIR=$NEWROOT/lib/modules/$KERNELVERSION/usb/
	BZIMAGE=$NEWROOT/boot/bzImage-$KERNELVERSION
	FD=/dev/fd0
}
#
# sanitiy checks before start of script
#
function sanity {
	#
	#	check that we can mount /dev/fd0
	#
	mounted=$(mount | grep "$FD"|cut -d\  -f3)
	if [ x$mounted != x ];then
		echo 1>&2 ">>> $FD is mounted on $mounted"
		echo 1>&2 ">>> giving up"
		cleanup
		exit 1
	fi
	if [ -z $KERNELVERSION ];then
		echo 1>&2 ">>> cannot examine kernel version"
		echo 1>&2 ">>> giving up"
		cleanup
		exit 1
	fi
	#
	#	check existence of usb modules
	#
	for m in $USBMODULES;do
		if [ ! -f $LIBDIR/$m ];then
			echo 1>&2 ">>> cannot find a file for modules $m"
			echo 1>&2 ">>> giving up"
			cleanup
			exit 1
		fi
	done
	#
	#	check existence of kernel file
	#
	if [ ! -f $BZIMAGE ];then
		echo 1>&2 ">>> kernel image $BZIMAGE does not exists"
		echo 1>&2 ">>> giving up"
		cleanup
		exit 1
	fi
	#
	#	check directories needed
	#
	[ -f $BOOTDSK ]   && rm -f $BOOTDSK 
	[ ! -d $BOOTDSK ] && mkdir $BOOTDSK 
	[ -f $RAMDSK ]    && rm -f $RAMDSK 
	[ ! -d $RAMDSK ]  && mkdir $RAMDSK 
}
#
# cleanup any mounted directories
#
function cleandir { # $1 directory
	if [ -d $1 ];then
		mount | grep "$1" && umount $1
		#rmdir  $1
	fi
}
#
#	get initrd.img from floppy and mount it.
#
function installimg {
	echo 1>&2 ">>> get the original image from floppy disk"
	mount /dev/fd0 $BOOTDSK
	gzip -d <$INITRD >$TMPIMG
	mount -oloop $TMPIMG $RAMDSK
	[ x$KERNELVERSION = x ] && KERNELVERSION=$(cd $BOOTDSK;echo vmlinuz-*|sed 's/vmlinuz-//')
	LIBDIR=/lib/modules/$KERNELVERSION/usb
}
#
#	patch linuxrc
#
function patchrc {
	mv -f $LINUXRC $LINUXRC1
	sed <$LINUXRC1 '/.*usb.*/d'  >$LINUXRC
	mv -f $LINUXRC $LINUXRC1
	(echo "compact"; sed  '/compact/d'  )<$LINUXRC1 >$LINUXRC
	#
	#	copy usb-ide.o to image
	#
	param=""
	echo "#" >>$LINUXRC
	echo "# automagically inserted by $0 $(date) $USER " >>$LINUXRC
	echo "#" >>$LINUXRC
	for file in $USBMODULES;do
		[ $file = "ide-usb.o" ] && param="bootWait=1 retries=10"
		echo 1>&2 ">>> installing $file"
		cp -f $LIBDIR/$file $RAMDSK/lib/
		echo echo "loading $file module" >>$LINUXRC
		echo insmod lib/$file $param	 >>$LINUXRC
		param=""
	done
	chmod +x $LINUXRC
	echo 1>&2 ">>> creating device entries for uda devices"
	(cd /dev/;tar cf - uda*)|(cd $BOOTDSK/dev;tar xf -)
}
function checkkernel {
	vmname=$(cd $BOOTDSK;echo vmlinuz*)
	bzname=bzImage-$KERNELVERSION
	if [ -f $BOOTDSK/$vmname ] ;then
		echo 1>&2 ">>> Kernel $vmname will replaced by $bzname"
		rm -rf $BOOTDSK/vmlinuz*

	fi
	sed <$LILOCNF >${LILOCNF}.1 "s/$vmname/$bzname/"
	mv ${LILOCNF}.1 $LILOCNF
	rm -rf $BOOTDSK/bzImage*
	cp $BZIMAGE $BOOTDSK/
	#
	#	patch root device in lilo.conf
	#
	if ! grep -q $ROOTDEV $LILOCNF ; then
		echo 1>&2 ">>> patching root device in $LILOCNF to $ROOTDEV"
		sed <$LILOCNF >${LILOCNF}.1 "s#\(root=\).*#\1$ROOTDEV#"
		mv ${LILOCNF}.1 $LILOCNF
		head -20 $LILOCNF
	fi
	#
	#	patch root device in message file
	#
	if ! grep -q $ROOTDEV $MSGFILE ; then
		echo 1>&2 ">>> patching root device in $MSGFILE to $ROOTDEV"
		sed <$MSGFILE >${MSGFILE}.1 "s#/dev/[a-z0-9]*#$ROOTDEV#"
		mv ${MSGFILE}.1 $MSGFILE
	fi
}
#
#
#
#
# re-create initrd on floppy
#
function recreate {
	umount $RAMDSK
	echo 1>&2 ">>> writing image back"
	gzip -9 <$TMPIMG >$INITRD
	checkkernel
	echo 1>&2 ">>> executing lilo"
	lilo -r $BOOTDSK
}
function cleanup {
	cleandir  $BOOTDSK
	cleandir  $RAMDSK
	umount /dev/fd0 2>/dev/null
}
function warning {
cat <<EOF >&2
This script is intended to patch a boot disk made with mkbootdisk. It enables 
the boot from the USB disk with the driver ide-usb. The root device given as 
argument must be mounted, since the kernel from there will be installed as 
well as the usb-modules.  
This patch will install kernel $BZIMAGE on device $ROOTDEV
If you do not want to continue, type ^C now
Otherwise the patch will continue
EOF
read
}
#
# main()
#
#trap "read" DEBUG
while [ $# -ge 1 ];do
 case $1 in
 --root) shift; ROOTDEV=$1;shift;break;;
 --version) shift; KERNELVERSION=$1;shift;break;;
 *) echo 1>&2 ">>> unknown parameter $1";shift;;
 esac
done
if [ -z $ROOTDEV ];then
	echo 1>&2 ">>> usage: $(basename $0) --root <rootdev> [ --version <version> ]"
	exit 1
fi
if [ -z $KERNELVERSION ];then
	KERNELVERSION=$(cat /proc/sys/kernel/osrelease)
fi
initvars
warning
trap "cleanup" SIGINT
sanity
cleanup
installimg && patchrc && recreate 
cleanup
