#!/bin/sh
# mkboot: make the system bootable
# Debian GNU/Linux
# Copyright 1996-1997 Guy Maor <maor@debian.org>
# This is free software; see the GNU General Public License version 2
# or later for copying conditions.  There is NO warranty.

set -e

PATH=$PATH:/sbin:/usr/sbin

# basic devfsd support
if [ -e /dev/.devfsd ]; then
	FLOPPY=/dev/floppy/0
else 
	FLOPPY=/dev/fd0
fi

# root partition
which rdev >/dev/null && rootpart=$(rdev | cut -d ' ' -f 1)
# temporary directory
tmpdir=${TMPDIR-/tmp}

# check whether ELILO is installed
elilocheck() {
    printf "\nChecking for ELILO..."
    if [ -f /etc/elilo.conf ] && [ -x /usr/sbin/elilo ]; then
	echo "Yes"
	return 0
    fi
    echo "No"
    return 1
}

# check whether SILO is installed
silocheck() {
    printf "\nChecking for SILO..."
    if [ -f /etc/silo.conf ] && [ -x /sbin/silo ]; then
       echo "Yes"
       return 0
    fi
    echo "No"
    return 1
}

# check whether GRUB is installed
grubcheck () {
    if ! which grub >/dev/null; then return 1; fi
}

# check whether LILO is installed
lilocheck () {
    printf "\nChecking for LILO..."
    if [ $(whoami) != root ] ; then
	echo "Only root can check for LILO"
        return 1;
    fi
    if [ ! -f /etc/lilo.conf ] || [ ! -x /sbin/lilo ] ; then
	echo "No"
	return 1;
    fi
    bootpart=$(perl -ne 'print $1 if /^\s*boot\s*=\s*(\S*)/' /etc/lilo.conf)
    if [ -z "$bootpart" ] ; then
	# lilo defaults to current root when 'boot=' is not present
	bootpart=$rootpart
    fi
    if [ ${bootpart#/dev/md} != $bootpart ] ||
       [ ${bootpart#/dev/evms/md} != $bootpart ]; then
        echo " - on software RAID device $bootpart - assuming LILO is installed and working."
    	return 0;
    fi
    if ! dd if=$bootpart ibs=16 count=1 2>&- | grep -q LILO ; then
	printf "\nYes, but I couldn't find a LILO signature on $bootpart\n"
	echo "Check your /etc/lilo.conf, or run /sbin/lilo by hand."
	return 1;
    fi
    echo "Yes, on $bootpart"
    return 0;
}

# check whether PALO is installed
palocheck() {
    printf "\nChecking for PALO..."
    if [ -f /etc/palo.conf ] && [ -x /sbin/palo ]; then
       echo "Yes"
       return 0
    fi
    echo "No"
    return 1
}

# make a lilo boot disk
makelilo () {
(
    b=$tmpdir/boot$$
    trap "set +e; cd /; umount $FLOPPY; rmdir $b" EXIT
    mkdir $b
    mke2fs -q $FLOPPY 
    mount -t ext2 $FLOPPY $b
    if [ -e /boot/boot.b ]; then
        cp /boot/boot.b $b/boot.b
    fi
    cp $1 $b/vmlinuz
    # if a symbolic link, look for the real file
    kfile=`readlink -f $1`
    # see if we need an "initrd=" line in lilo.conf
    kdir=`dirname $kfile`
    if [ $kdir = . ]; then kdir=$PWD   # full path
      elif [ $kdir = boot ]; then kdir=/boot; kfile="/$kfile"; fi
    # make sure directory exists
    if [ -d $kdir ]; then
        # extract version
        case $kfile in
            *-*) kvers=${kfile#*-} ;;
    	    *)   kvers="" ;;
        esac
        # look for matching initrd.img
	if [ "$kvers" = "" ]; then ifile="$kdir"/initrd.img
	else ifile="$kdir"/initrd.img-$kvers ; fi
	#
	if [ ! -f $ifile ]; then ifile=""; fi
    else
        kdir="" ; ifile=""
    fi
    # kfile is now the full path to the kernel image, and
    # ifile is now the initrd.img that matches the kernel, or null
    #
    echo "Kernel is at $kfile in $kdir"
    if [ "$ifile" != "" ]; then echo "Matching initrd image is $ifile";fi
    #
    lilo_conf="# floppy lilo.conf
	boot = $FLOPPY
	install = boot.b
	map = map"
    state=G
    usesinitrd=n
    # examine /etc/lilo.conf
    while read line
    do
        case $line in
	    "#"* | "" ) continue ;;   # skip comments and blank lines
	    *boot*=* | *root*=* | *install*=* ) continue ;;
	    *map*=* | *message*=* ) continue    # skip these files as well
	                ;;
	    *image*=* ) if [ $state = L ]; then
			    break
			fi   # done
			#
	                case $line in
			    *$1 )  kimg=`echo ${line#*=}`
			              state=L   # found correct kernel
			              ;;
			    *linu*) kimg=`echo ${line#*=}`
				    # see if this is the right Linux kernel
				    if [ `ls -l $kimg |
				         awk '{print $NF}'` = $kfile ]; then
			                state=L   # found right kernel
				    else
				        state=I   # Ignore wrong kernel
				    fi
			               ;;
			    * ) state=I   # Ignore this stanza
				       ;;
	                esac
			#
			if [ $state = L ]; then
	                     lilo_conf="$lilo_conf
			               # kernel-specific:
				       $line"
			fi
			;;
	    *other*=* ) state=I  # ignore non-Linux stanza
	                ;;
	    * ) if [ $state != I ]; then
	           lilo_conf=`printf "$lilo_conf\n$line\n"`
		fi
		#
		case $line in
		    *initrd*=* )  lcinitrd=`ls -l ${line#*=} |
		                            awk '{print $NF}'`
		                  if [ $lcinitrd = "$ifile" ]; then
				      usesinitrd=y
				  fi
		                  ;;
		esac
		;;
	esac
    done < /etc/lilo.conf
    #
    lilo_conf=`printf "$lilo_conf\nroot = $rootpart\n"`
    #
    if [ $state = L ]; then
        echo "Found kernel image $kimg in existing /etc/lilo.conf"
	if [ "$liloOK" != n ] && [ $usesinitrd = y ] && [ "$ifile" != "" ]; then
	    echo "   and the correct initrd.img as well."
	    problems=n
	elif [ "$liloOK" != n ] && [ $usesinitrd = n ] && [ "$ifile" = "" ]; then
	    problems=n   # no initrd needed
	elif [ "$liloOK" != n ] && [ $usesinitrd = n ] && [ "$ifile" != "" ]; then
	    echo "   but it does not invoke $ifile ."
	    problems=y
	elif [ "$liloOK" = n ] && [ $usesinitrd = y ]; then
	    echo "   and the correct initrd.img as well,"
	    echo "   but there were problems in installing from it."
	    problems=y
	else
	    echo "but it does not mention the required initrd.img ."
	    problems=y
	fi
    else
	echo "Could not find the requested kernel in your"
	echo "   current /etc/lilo.conf ."
	problems=y
    fi
    #
    if [ $problems = n ]; then
        echo "Your current /etc/lilo.conf looks good, and can be used"
        echo "   as the basis for the boot-floppy lilo.conf."
    else
        echo "The mkboot script can probably do better."
    fi
    sleep 8
    #
    echo
    echo "Here is the proposed lilo.conf:"
    echo
    echo "$lilo_conf"
    echo
    echo "You can install the boot-loader from this best guess,"
    echo "or you can try to install from a \`vanilla\' lilo.conf ."
    echo
    reply=""
    while [ "$reply" = "" ]
    do
        printf "Which do you choose? (Enter B for best, V for vanilla): "
        read reply
    done
    #
    if [ $reply = B ] || [ $reply = b ]; then
	echo "Installing the best-guess lilo.conf..."
        cd $b
        echo "$lilo_conf" > lilo.conf
    else
	echo "Installing the vanilla lilo.conf..."
        cd $b
        cat > lilo.conf <<- EOF
	    lba32
	    boot = $FLOPPY
	    install = boot.b
	    map = map
	    compact
	    prompt
	    timeout = 50
	    read-only
	    image = vmlinuz
	    label = linux
	    root = $rootpart
	EOF
    fi
    #
    lilo -C lilo.conf
    cat <<EOF
If you need to modify the floppy's lilo.conf, run the following:

    	mount $FLOPPY /mnt
    	cd /mnt
    	vi lilo.conf		# edit the file
    	lilo -C lilo.conf	# run lilo on floppy
    	cd
    	umount $FLOPPY
EOF
)
}


# make a simple boot disk
makesimple () {
(
    dd if=$1 of=$FLOPPY
    rdev $FLOPPY $rootpart
    rdev -R $FLOPPY 1
)
}    



# make a boot disk
makedisk () {
    kernel=${1:-/vmlinuz}
    if [ ! -r $kernel ] ; then
	echo "Error: Can't read $kernel."
	exit 1
    fi

    boottype="lilo"
    if [ $(whoami) != root ] ; then
	echo "Since you don't have root permissions, I can't put LILO on the diskette."
	echo "I will make a non-LILO diskette instead, but it won't be as useful.  You"
	echo "can hit <Ctrl-C> to cancel."
	boottype="simple"
    fi

    printf "\nInsert a floppy diskette into your boot drive, and press <Return>. "
    read input
    diskok=0
    while [ "$diskok" != 1 ] ; do
	printf "\nCreating a $boottype bootdisk...\n"
	make$boottype $kernel
	if [ $? -eq 0 ] ; then
	    diskok=1
	else
	    printf "\nThere was a problem creating the boot diskette.  Please make sure that\n"
	    echo "you inserted the diskette into the correct drive and that the diskette"
	    echo "is not write-protected."
	    printf "\nWould you like to try again? (y/n) "
	    read input
	    if [ "$input" != "y" ] ; then
		return 1
	    fi
	fi
    done
    echo "...Success."
    return 0
}

usage="$0 [-r rootpartition] [-i] [-d device] [kernel]"

while getopts "r:id:h-" opt ; do
    case "$opt" in
	r) rootpart="$OPTARG" ;;
	i) installkernel=1 ;;
	d) FLOPPY="$OPTARG" ;;
	h) echo $usage ; exit 0 ;;
	-) break ;;
	*) echo $usage 1>&2 ; exit 1 ;;
    esac
done
shift $(($OPTIND - 1))

if [ "$installkernel" ] ; then
    echo "In order to use the new kernel image you have just installed, you"
    echo "will need to reboot the machine.  First, however, you will need to"
    echo "either make a bootable floppy diskette, re-run LILO, or have GRUB"
    echo "installed."

    if elilocheck; then
	printf "\nShould I run /usr/sbin/elilo? (y/N) "
	read input
	if [ "$input" = "y" ] ; then
	    /usr/sbin/elilo && exit 0
            echo "There was a problem running /usr/sbin/elilo."
	fi
    fi

    if grubcheck; then
        printf "\nGRUB is installed. To automatically switch to new kernels, point your\n"
        echo "default entry in menu.lst to $1"
        exit 0
    fi

    if lilocheck; then
        liloOK=y
	printf "\nShould I run /sbin/lilo? (y/N) "
	read input
	if [ "$input" = "y" ] ; then
	    /sbin/lilo && exit 0
            echo "There was a problem running /sbin/lilo."
            liloOK=n
	fi
    fi

    if silocheck; then
        printf "\nSILO is installed. To automatically switch to new kernels, point your\n"
        echo "default entry in silo.conf to $1"
        exit 0
    fi

    if palocheck; then
        printf "\nPALO is installed. To automatically switch to new kernels, point your\n"
        echo "default entry in palo.conf to $1"
        exit 0
    fi

    printf "\nShould I make a bootdisk? (y/N) "
    read input
    if [ "$input" = "y" ] ; then
	makedisk $1 && exit 0
    fi

    printf "\nWARNING: Your system is probably unbootable now.  After correcting any\n"
    echo "problems, rerun this script with the command \`mkboot -installkernel'."
    exit 1
fi

makedisk $1
