#!/bin/sh
# Copyright 1995, 1998 Patrick Volkerding, Moorhead, Minnesota USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#

# Make sure there's a proper temp directory:
TMP=/var/log/setup/tmp
# If the $TMP directory doesn't exist, create it:
if [ ! -d $TMP ]; then
  rm -rf $TMP # make sure it's not a symlink or something stupid
  mkdir $TMP
  chmod 700 $TMP # no need to leave it open
fi

ROOT_DEVICE="`mount | fgrep ' on / ' | cut -f 1 -d ' '`"
# We'll look for the kernel in 4 standard locations, and use the most recent
# copy we find:
KERNEL=""
K1=/vmlinuz
K2=/usr/src/linux/arch/i386/boot/zImage
K3=/usr/src/linux/arch/i386/boot/bzImage
K4=/boot/vmlinuz
# First, try to find a readable copy:
if [ -r $K1 ]; then
  KERNEL=$K1
elif [ -r $K2 ]; then
  KERNEL=$K2
elif [ -r $K3 ]; then
  KERNEL=$K3
elif [ -r $K4 ]; then
  KERNEL=$K4
fi
# Then, try to find the newest copy:
if [ $K1 -nt $KERNEL ]; then
  KERNEL=$K1
fi
if [ $K2 -nt $KERNEL ]; then
  KERNEL=$K2
fi
if [ $K3 -nt $KERNEL ]; then
  KERNEL=$K3
fi
if [ $K4 -nt $KERNEL ]; then
  KERNEL=$K4
fi
# If there's a kernel specified on the command line, default to that:
if [ ! "$1" = "" ]; then
  KERNEL=$1
  if [ ! -r $1 ]; then
    echo "No such kernel: $1"
  fi
fi
# At this point, if none of $K1 - $K4 matched a file, and no $1 was specified,
# it's possible that $KERNEL doesn't point to an existing file:
if [ ! -r $KERNEL ]; then
  echo "usage:  makebootdisk <kernel filename to use>"
  echo "No valid kernel was specified, and a Linux kernel could not be found"
  echo "at any of the following default locations:"
  echo "   /vmlinuz"
  echo "   /usr/src/linux/arch/i386/boot/zImage"
  echo "   /usr/src/linux/arch/i386/boot/bzImage"
  echo "   /boot/vmlinuz"
  exit 1
fi
if mount | fgrep ' on / ' | fgrep umsdos 1> /dev/null 2> /dev/null ; then
 MOUNT="read-write"
else
 MOUNT="read-only"
fi
while [ 0 ]; do # the bootdisk menu loop
 dialog --title "MAKE BOOTDISK FROM $KERNEL" --menu "This menu allows you to make a \
bootdisk from a newly compiled kernel. If you don't supply a kernel filename \
on the command line, then it looks at /vmlinuz, \
/usr/src/linux/arch/i386/boot/bzImage, \
/usr/src/linux/arch/i386/boot/zImage, and /boot/vmlinuz, and uses whichever \
one is newest. \
There are two types of \
bootdisks that you can make:  a simple bootdisk (which is just a kernel image \
written directly to disk) or a LILO bootdisk (which is more flexible, but \
takes a little longer to load).  Which option would you like?" 18 74 4 \
"format" "format floppy disk in /dev/fd0" \
"simple" "make simple vmlinuz > /dev/fd0 bootdisk" \
"lilo" "make lilo bootdisk" \
"exit" "exit this program" 2> $TMP/return
 if [ ! $? = 0 ]; then
  break;
 fi
 REPLY=`cat $TMP/return`
 rm -f $TMP/return
 if [ "$REPLY" = "format" ]; then
  dialog --title "SPECIFY FLOPPY SIZE" --menu "What size is your /dev/fd0 (a:)\
 drive?" 9 60 2 \
"1.44" "1.44 megabytes" \
"1.2" "1.2 megabytes" 2> $TMP/return
  if [ "`cat $TMP/return`" = "1.44" ]; then
   dialog --title "Formatting /dev/fd0u1440" --infobox "Formatting /dev/fd0, \
1.44 megabytes." 3 50 
   fdformat -n /dev/fd0u1440 1> /dev/null 2> /dev/null
  elif [ "`cat $TMP/return`" = "1.2" ]; then
   dialog --title "Formatting /dev/fd0h1200" --infobox "Formatting /dev/fd0, \
1.2 megabytes." 3 50
   fdformat -n /dev/fd0h1200 1> /dev/null 2> /dev/null
  fi
  rm -f $TMP/return
 elif [ "$REPLY" = "simple" ]; then # make simple bootdisk
  dialog --title "BOOT DISK CREATION" --yesno \
"\n\
Now put a formatted floppy in your boot drive. \n\
This will be made into your Linux boot disk. Use this to\n\
boot Linux until LILO has been configured to boot from\n\
the hard drive.\n\n\
Any data on the target disk will be destroyed.\n\n\
YES creates the disk, NO aborts.\n" 14 62
  if [ $? = 0 ]; then
   dialog --title "CREATING DISK" --infobox "Creating boot disk from $KERNEL..." 5 72
   dd if=$KERNEL of=/dev/fd0 2> /dev/null
   rdev /dev/fd0 $ROOT_DEVICE
   rdev -v /dev/fd0 -1
   if [ "$MOUNT" = "read-only" ]; then
    rdev -R /dev/fd0 1
   else
    rdev -R /dev/fd0 0
   fi
  fi
 elif [ "$REPLY" = "lilo" ]; then # make lilo bootdisk
  dialog --title "CREATING LILO BOOTDISK IN /dev/fd0" --yesno "Now put a \
formatted floppy in your boot drive.  This will be made into a LILO \
bootdisk that you can use to start your Linux system.  Any data on the \
target disk will be destroyed.  YES creates the disk, NO aborts." 8 62
  if [ $? = 0 ]; then # make the disk
#   dialog --title "SPECIFY FLOPPY SIZE" --menu "What size is your /dev/fd0 (a:)\
# drive?" 9 60 2 \
#"1.44" "1.44 megabytes" \
#"1.2" "1.2 megabytes" 2> $TMP/return
#   if [ "`cat $TMP/return`" = "1.44" ]; then
#    DEV=/dev/fd0u1440
#   else
#    DEV=/dev/fd0h1200
#   fi
DEV=/dev/fd0
   dialog --infobox "Creating LILO bootdisk from $KERNEL for $ROOT_DEVICE..." 4 60
   mke2fs -i 1024 $DEV 1> /dev/null 2> /dev/null
   if [ ! -d $TMP/lilo ]; then
    mkdir $TMP/lilo
   fi
   mount -t ext2 /dev/fd0 $TMP/lilo 1> /dev/null 2> /dev/null
   cp $KERNEL $TMP/lilo/vmlinuz
   mkdir $TMP/lilo/dev
   cp -a /dev/ed* $TMP/lilo/dev
   cp -a /dev/fd* $TMP/lilo/dev
   cp -a /dev/hd* $TMP/lilo/dev
   cp -a /dev/sd* $TMP/lilo/dev
   mkdir $TMP/lilo/etc
   cat << EOF > $TMP/lilo/etc/lilo.conf
boot = /dev/fd0
message=/boot/message
prompt
image = /vmlinuz
        label = mount
        ramdisk = 0
        root = $ROOT_DEVICE
        vga = normal
        $MOUNT
EOF
   mkdir $TMP/lilo/boot
   cp -a /boot/*.b $TMP/lilo/boot
   cat << EOF > $TMP/lilo/boot/message

Welcome to the Slackware Linux custom LILO bootdisk!  

By default, this disk boots a root Linux partition on $ROOT_DEVICE when
you hit ENTER.  If you'd like to boot some other partition, use a command
like this on the LILO prompt below:

    mount root=/dev/sda1 ro

Where "/dev/sda1" is the partition you want to boot, and "ro" specifies that
the partition should be initially mounted as read-only.  If you which to mount
the partition read-write, use "rw" instead.  You may also add any other kernel
parameters you might need depending on your hardware, and which drivers are
included in your kernel.

EOF
   lilo -r $TMP/lilo
   umount $TMP/lilo
   rm -rf $TMP/lilo
  fi
 elif [ "$REPLY" = "exit" ]; then
  break;
 fi
done
