#!/bin/sh
# 
#define Q+D
#
# This makes a bootkernel disk in /dev/fd0 from a kernel image.
#
# Run this in a directory containing the kernels you're gonna use, and a
# subdirectory with a master image of the bootkernel disk.
#
# This is the command to use:
#
# makedisk kernel_image disk_size
#          ^^           ^^^^^^^^^ This is 1440 or 1200
#          ^^^^^ This is the name (and maybe path to) the kernel you're going
#                to use, such as scsinet/scsinet.
#
#
KERNEL=$1
DISKSIZE=$2
FSSIZE=`filesize $1`
FSSIZE=`expr $FSSIZE + 180112`
FSSIZE=`expr $FSSIZE / 1024`
TARGET=/dev/fd0
MASTER_DIR=/tmp/master-$$
CWD=`pwd`
MOUNT=/tmp/mnt-$$
if [ ! -d $MASTER_DIR ]; then
  mkdir -p $MASTER_DIR
  ( cd $MASTER_DIR ; tar xzvf $CWD/master.tar.gz )
  DELETE_MASTER=true
fi
if [ ! -d $MOUNT ]; then
  mkdir -p $MOUNT
  DELETE_MOUNT=true
fi
if [ $DISKSIZE = 1200 ]; then
 cp lilo.conf.1200 $MASTER_DIR/etc/lilo.conf
 RAMSIZE=1200
elif [ $DISKSIZE = 1440 ]; then
 cp lilo.conf.1440 $MASTER_DIR/etc/lilo.conf
 RAMSIZE=1440
else
 echo
 echo "Usage: makedisk kernel_image disk_size"
 echo
 echo "Disk is made in /dev/fd0. Target disk must be formatted."
 echo "Legal disk sizes are 1200 and 1440."
 echo
 exit
fi
if [ -r $1 ]; then
 echo "Making a bootkernel disk from kernel image $1..."
else
 echo "Image $1 not readable."
 exit
fi
cp $1 $MASTER_DIR/vmlinuz 
cat `dirname $1`/config | gzip -9c > $MASTER_DIR/config.gz
cat `dirname $1`/System.map | gzip -9c > $MASTER_DIR/System.map.gz
RESULT=$?
if [ ! $RESULT = 0 ]; then
  echo "Fatal error on $MASTER_DIR..."
  read junk_input;
fi
LABEL=`dirname $1`
cat << EOF > $MASTER_DIR/boot/message

Welcome to the Slackware Linux (v. 7.0.0) $LABEL bootdisk!

If you need to pass extra parameters to the kernel, enter them at the prompt
below after one of the valid configuration names:  ramdisk (to load a rootdisk
into memory and boot it), and mount (to boot an existing Linux partition).
NOTE:  Most hardware is auto-detected without parameters.  So, before assuming
       your system requires parameters, try a few different bootdisks. :^)

Here are some examples (and more can be found in the BOOTING file):
   ramdisk hdx=cyls,heads,sects,wpcom,irq  (needed only if probing fails)
   ramdisk hdx=cdrom (force detection of an IDE/ATAPI CD-ROM drive)
   where hdx can be any of hda through hdh. Examples: hdc=1050,32,64  hdd=cdrom

If you would rather load the root/install disk from your second floppy drive:
   ramdisk root=/dev/fd1

In a pinch, you can boot your Linux system with a command like:
   mount root=/dev/hda1

DON'T SWITCH ANY DISKS YET! This prompt is just for entering extra parameters.
If you don't need to enter any parameters, hit ENTER to continue.

EOF
echo "Rdeving -R $MASTER_DIR/vmlinuz 0"
rdev -R $MASTER_DIR/vmlinuz 0
#echo "Rdeving -r $MASTER_DIR/vmlinuz $RAMSIZE"
#rdev -r $MASTER_DIR/vmlinuz $RAMSIZE
echo "Rdeving rdev -r $MASTER_DIR/vmlinuz 49152"
rdev -r $MASTER_DIR/vmlinuz 49152
echo "Rdeving -v $MASTER_DIR/vmlinuz -v"
rdev -v $MASTER_DIR/vmlinuz -1
# It's possible some people might want to use /dev/fd0h1200 or /dev/fd0H1440
# on the line below...
echo "Rdeving $MASTER_DIR/vmlinuz /dev/fd0"
rdev $MASTER_DIR/vmlinuz /dev/fd0
#echo "Formatting the target disk..."
#fdformat /dev/fd0H1200
echo "Blanking the target disk..."
dd if=/dev/zero of=$TARGET bs=1024 count=$FSSIZE
echo "Making the MINIX/Linux filesystem..."
# Let's use a 600K size, since that's good enough for the kernel + utilities
# needed to boot... then we can reduce the size of the uncompressed disks.
mkfs.minix -i 400 $TARGET $FSSIZE
echo "Mounting the disk on $MOUNT..."
mount $TARGET $MOUNT
cd $MASTER_DIR
echo "Copying the files over..."
cp -a * $MOUNT
sync
echo "Installing LILO..."
lilo -r $MOUNT
sync
umount $MOUNT
echo "Bootkernel disk created."
if [ "$DELETE_MASTER" = "true" ]; then
  rm -rf $MASTER_DIR
fi
if [ "$DELETE_MOUNT" = "true" ]; then
  rm -rf $MOUNT
fi
NAME=`dirname $1`
NAME=`basename $NAME`
dd if=/dev/fd0 of=/tmp/$NAME bs=1024 count=$FSSIZE
