#!/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
cp lilo.conf.zipslack $MASTER_DIR/etc/lilo.conf
RAMSIZE=1440
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
echo > $MASTER_DIR/config.gz
echo > $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. 8.0.0) ZipSlack bootdisk!

By default, this disk will boot a Linux system on /dev/sda4.  In an otherwise
IDE based Linux system with a Zip drive connected to a SCSI card or parallel
port, this is probably the device to use.

To boot the default device (/dev/sda4) just hit ENTER.  To boot other devices,
you'll have to enter some information on the 'boot:' prompt below.  Here are
two examples of how to boot a SCSI or parallel Zip drive:
  mount root=/dev/sda4 rw
  mount root=/dev/sdb4 rw

These examples work with IDE devices such as DOS partitions on your hard drive,
or IDE Zip drives:
  mount root=/dev/hda1 rw
  mount root=/dev/hda4 rw
  mount root=/dev/hda5 rw
  mount root=/dev/hdb4 rw


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
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 -s /dev/null -r $MOUNT
echo "Adding extra files (hope they fit :)"
cat $CWD/`dirname $1`/config | gzip -9c > $MOUNT/config.gz
cat $CWD/`dirname $1`/System.map.gz > $MOUNT/System.map.gz
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