#!/bin/sh

# mkbootdisk
# 14.04.2002 -- version 2.0.3; boot.b is obsoleted
# patch by Tomek Orzechowski <orzech!pld#org$pl>
#
# 11.05.2002 -- version 2.0.2; fixed initrd location by:
# GoTaR <gotar@priv0.onet.pl>
#
# 20.03.2002 -- version 2.0.1; merged some RH changes by:
# GoTaR <gotar@priv0.onet.pl>
#
# 03.02.2002 -- PLDized by:
# djrzulf <przemyslaw@knycz.net>
#
# 19.05.2001 -- updated by:
# GoTaR <gotar@priv0.onet.pl>
#
# basing on mkbootdisk written by:
# Erik Troan <ewt@redhat.com>

pause=yes
unset kernel
device=/dev/fd0
unset verbose
unset witheth
unset geninitrdargs
unset compact

MOUNTDIR=${TMPDIR:-/tmp}/mkbootdisk
PATH=/sbin:$PATH
export PATH

VERSION=2.0.1

usage () {
	cat >&2 <<EOF
usage: `basename $0` [--version] [--noprompt] [--geninitrdargs <args>]
	[--device <devicefile>] [--verbose -v] [--compact] <kernel>
	(ex: `basename $0` --device /dev/fd0 2.2.20-10)
EOF
	exit $1
}

while [ $# -gt 0 ]; do
case $1 in
	--device)
		shift
		device=$1
		;;
	--geninitrdargs)
		shift
		geninitrdargs=$1
		;;
	--help)
		usage 0
		;;
	--noprompt)
		unset pause
		;;
	-v)
		verbose=true
		;;
	--verbose)
		verbose=true
		;;
	--version)
		echo "mkbootdisk: version $VERSION"
		exit 0
		;;
	--compact)
		compact=compact
		;;
	*)
		if [ -z "$kernel" ]; then
			kernel=$1
		else
			usage
		fi
		;;
esac
shift
done

[ -z "$kernel" ] && usage 1

[ -d /lib/modules/$kernel ] || {
	echo "/lib/modules/$kernel is not a directory." >&2
	exit 1
}

[ -f /boot/vmlinuz-$kernel ] || {
	echo "/boot/vmlinuz-$kernel does not exist." >&2
	exit 1
}

[ -f /etc/modules.conf -a -f /lib/modules/$kernel/modules.dep ] && {
	ethmodule=`awk '/^alias[ \t]*eth0/ {print $3; exit 0}' /etc/modules.conf`
	[ -n "$ethmodule" ] && {
		ethmodule=${ethmodule%.o}
		deps=`grep "/net/$ethmodule.o" /lib/modules/$kernel/modules.dep | head -1 | cut -d: -f2`
		for n in $deps; do
			mod=`basename $n | cut -d. -f1`
			witheth="$witheth --with $mod"
		done
		witheth="$witheth --with $ethmodule"
	}
}

rootdev=`awk '$1 ~ /^[^#]/ && $2 ~ /^\/$/ {print $1; exit 0}' /etc/fstab`

if [ $(echo $rootdev | cut -c1-6) = "LABEL=" ]; then
	rootlabel=$(echo $rootdev | cut -c7-)

	# whee, now we have to look through every partition looking for
	# the thing called $rootlabel, which could be raid. Ick.

	list=$(tail +3 /proc/partitions | awk '{print $4}' | grep '^md')
	list="$list $(tail +3 /proc/partitions |
		awk '{print $4}' | grep -v '^md')"
	rootdev=""
	for dev in $list; do
		if tune2fs -l /dev/$dev >/dev/null 2>&1; then
			label=$(tune2fs -l /dev/$dev 2>/dev/null |
				grep "Filesystem volume name" | awk '{print $4}')
			if [ "$label" = $rootlabel ]; then
				rootdev=/dev/$dev
				break
			fi
		fi
	done
fi

[ -z $rootdev ] && {
	echo 'Cannot find root partition in /etc/fstab.' >&2
	exit 1
}

[ -n "$pause" ] && {
	echo "Insert a disk in $device. Any information on the disk will be lost."
	echo -n "Press <Enter> to continue or ^C to abort: "
	read aline
}

[ -n "$verbose" ] && echo -n "Creating filesystem on $device... "
mke2fs $device >/dev/null || {
	echo "Failed to create ext2 filesystem on $device" >&2
	exit 1
}
[ -n "$verbose" ] && echo "done."

rm -rf $MOUNTDIR
mkdir $MOUNTDIR || {
	echo "Failed to create $MOUNTDIR" >&2
	exit 1
}
[ -d $MOUNTDIR ] || {
	echo "$MOUNTDIR is not a directory!" >&2
	exit 1
}

mount -t ext2 $device $MOUNTDIR || {
	rmdir $MOUNTDIR
	exit 1
}

mkdir $MOUNTDIR/boot
mkdir $MOUNTDIR/dev
mkdir $MOUNTDIR/etc
[ -n "$verbose" ] && echo -n "Copying /boot/vmlinuz-$kernel... "
cp -p /boot/vmlinuz-$kernel $MOUNTDIR/boot
[ -n "$verbose" ] && echo "done."

[ -n "$verbose" ] && echo -n "Creating initrd image... "
/sbin/geninitrd $geninitrdargs $witheth --ifneeded $MOUNTDIR/boot/initrd.gz $kernel
[ -n "$verbose" ] && echo "done."

[ -f /boot/boot.b ] && cp -p /boot/boot.b $MOUNTDIR/boot
cp -a $device $MOUNTDIR/dev
cp -a $rootdev $MOUNTDIR/dev

[ -n "$verbose" ] && echo -n "Setting up lilo... "

cat > $MOUNTDIR/etc/lilo.conf <<EOF
boot=$device
$compact
prompt
timeout=100
message=/boot/message
image=/boot/vmlinuz-$kernel
	label=linux
	root=$rootdev
EOF
[ -f $MOUNTDIR/boot/initrd.gz ] && echo "	initrd=/boot/initrd.gz" >> $MOUNTDIR/etc/lilo.conf

cat >> $MOUNTDIR/etc/lilo.conf <<EOF
image=/boot/vmlinuz-$kernel
	label=rescue
	append="load_ramdisk=2 prompt_ramdisk=1"
	root=/dev/fd0
EOF
[ -f $MOUNTDIR/boot/initrd.gz ] && echo "	initrd=/boot/initrd.gz" >> $MOUNTDIR/etc/lilo.conf
chmod 644 $MOUNTDIR/etc/lilo.conf

cat >> $MOUNTDIR/boot/message <<EOF
Press <return> (or wait 10 seconds) to boot your PLD/Linux system from
$rootdev. You may override the default linux kernel parameters by
typing "linux <params>", followed by <return> if you like. If you want
to use a rescue disk, type "rescue" now.

EOF

[ -n "$verbose" ] && echo "done."

if [ -n "$verbose" ]; then
	/sbin/lilo -r $MOUNTDIR
else
	/sbin/lilo -r $MOUNTDIR >/dev/null
fi

umount $MOUNTDIR
rmdir $MOUNTDIR
