#!/bin/bash

# mkbootdisk
#
# Written by Erik Troan <ewt@redhat.com>
#     and    Jakub Jelinek <jakub@redhat.com>

pause=yes
unset kernel
device=/dev/fd0
unset verbose
unset witheth
unset mkinitrdargs

PATH=/sbin:$PATH
export PATH
MOUNTDIR=/tmp/mkbootdisk-mnt

VERSION=1.2.5

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

while [ $# -gt 0 ]; do
    case $1 in
	--device)
	    shift
	    device=$1
	    ;;
	--mkinitrdargs)
	    shift
	    mkinitrdargs=$1
	    ;;
	--help)
	    usage 0
	    ;;
	--noprompt)
	    unset pause
	    ;;
	-v)
	    verbose=true
	    ;;
	--verbose)
	    verbose=true
	    ;;
	--version)
	    echo "mkbootdisk: version $VERSION"
	    exit 0
	    ;;
	*)
	    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/conf.modules -a -f /lib/modules/$kernel/modules.dep ] && {
    ethmodule=`sort -u /etc/conf.modules | awk '/^alias eth0/ { print $3 }'`
    [ $n "$ethmodule" ] && {
	if echo $ethmodule | grep '\.o$'; then
	    ethmodule=`echo ${ethmodule} | sed "s/\.o$//"`
	fi
	deps=`grep "/net/$ethmodule.o" /lib/modules/$kernel/modules.dep | head -1`
	deps=`echo $deps | cut -d: -f2`
	for n in $deps; do
	    mod=`basename $n | cut -d. -f1`
	    witheth="$witheth --with $mod"
	done
	witheth="$witheth --with $ethmodule"
    }
}

[ -f /lib/modules/$kernel/fs/isofs.o ] &&
	withfs="$withfs --with isofs"
[ -f /lib/modules/$kernel/fs/nfs.o ] &&
	withfs="$withfs --with nfs"
[ -f /lib/modules/$kernel/fs/fat.o ] &&
	withfs="$withfs --with fat"
[ -f /lib/modules/$kernel/fs/msdos.o ] &&
	withfs="$withfs --with msdos"

rootdev=`awk '$1 ~ /^[^#]/ && $2 ~ /^\/$/ { print $1 ; exit }' /etc/fstab`
[ -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
}

TMPFILE=`mktemp /tmp/mkbootdisk-tmp.XXXXXX` || exit 1

[ -n "$verbose" ] && echo -n "Formatting temp file... "
dd if=/dev/zero of=$TMPFILE bs=1k count=3500 >/dev/null 2>&1
mke2fs -m 0 -q $TMPFILE 3500 >/dev/null 2>&1 <<EOF
y
EOF
[ -n "$verbose" ] && echo "done."

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

mount -o loop -t ext2 $TMPFILE $MOUNTDIR || {
    rmdir $MOUNTDIR
    rm -f $TMPFILE
    exit 1
}

[ -n "$verbose" ] && echo -n "Copying /boot/vmlinuz-$kernel... "
cp -a /boot/vmlinuz-$kernel $MOUNTDIR
[ -n "$verbose" ] && echo "done."

[ -n "$verbose" ] && echo -n "Creating initrd image... "
/sbin/mkinitrd $mkinitrdargs $witheth --ifneeded $MOUNTDIR/initrd.img $kernel
[ -n "$verbose" ] && echo "done."

[ -n "$verbose" ] && echo -n "Setting up silo... "
cp -a /boot/fd.b $MOUNTDIR/fd.b
cp -a /boot/second.b $MOUNTDIR/second.b

cat > $MOUNTDIR/silo.conf <<EOF
partition=1
timeout=100
message=/welcome.msg
image=/vmlinuz-$kernel
	label=linux
	root=$rootdev
EOF
[ -f $MOUNTDIR/initrd.img ] && echo "	initrd=/initrd.img" >> $MOUNTDIR/silo.conf

[ -f $MOUNTDIR/initrd.img ] && echo "	initrd=/initrd.img" >> $MOUNTDIR/silo.conf
chmod 644 $MOUNTDIR/silo.conf

cat >> $MOUNTDIR/welcome.msg <<EOF
Press <return> (or wait 10 seconds) to boot your Red Hat Linux system from
$rootdev. You may override the default linux kernel parameters by typing
"linux <params>", followed by <return> if you like.

EOF

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

[ -n "$verbose" ] && echo -n "Creating romfs image on $device... "

genromfs -V "rescue boot floppy" -d $MOUNTDIR/ -f $device -a 512 -A 2048,/..

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

umount $MOUNTDIR

if [ -f /proc/modules ] && ! grep romfs /proc/modules >/dev/null 2>&1; then
    /sbin/insmod romfs
fi

mount -t romfs $device $MOUNTDIR || {
    rmdir $MOUNTDIR
    rm -f $TMPFILE
    exit 1
}

if [ -n "$verbose" ]; then
    /sbin/silo -r $MOUNTDIR -i /fd.b -b /second.b -C /silo.conf -F
else
    /sbin/silo -r $MOUNTDIR -i /fd.b -b /second.b -C /silo.conf -F > /dev/null
fi

umount $MOUNTDIR
rmdir $MOUNTDIR
rm -f $TMPFILE
