#!/bin/sh

MSG() {
cat <<EOF
============================================================================
Format a floppy disk to 1722 and put it in drive A. 

It's usually /dev/fd0H1722 or fd0u1722.  Use fdformat or superformat.

# fdformat /dev/fd0u1722
# superformat /dev/fd0u1722

If you can't format a floppy disk to 1722 without errors, there's no
point trying to use zdisk with the floppy.

Tell zdisk where the chosen kernel is, the kernel can't be more than 
720 Kb in size.

zdisk -k path_to_kernel

Some possible paths to the kernel.

zdisk -k /vmlinuz
zdisk -k /boot/vmlinuz
zdisk -k /usr/src/linux/arch/i386/boot/zImage
zdisk -k /usr/src/linux/arch/i386/boot/bzImage
============================================================================
EOF
}

if [ "$1" = "" ]; then
MSG
exit
fi

if [ ! "$UID" = "0" ]; then
echo
echo "[$LOGNAME] You need to be [root] to run this script."
echo
exit 1
fi

if [ "$1" = "-k" ]; then
if [ -s "$2" ]; then

if [ -b /dev/fd0u1722 ]; then
FD=/dev/fd0u1722
elif [ -b /dev/fd0H1722 ]; then
FD=/dev/fd0H1722
else
echo "Cat't find /dev/fd0u1722 or /dev/fd0H1722 ..."
echo "You can create /dev/fd0u1722 like this."
echo "# mknod -m 660 /dev/fd0u1722 b 2 60"
exit 1
fi

if [ ! -s ./ramflop.gz ]; then
echo "Can't find 'ramflop.gz' in this directory."
echo "You should have 'ramflop.gz' in the same directory as this script."
exit 1
fi

if [ ! `cat /proc/filesystems | grep -v umsdos | grep msdos` ]; then
if [ -s /lib/modules/`uname -r`/fs/msdos.o ]; then
echo "You don't have msdos filesystem support, i need it."
echo "You have msdos filesystem support as a module, load it."
echo "# depmod -a \`uname -r\`"
echo "# modprobe msdos"
exit 1
else
echo "You don't have msdos filesystem support, i need it."
exit 1
fi
fi

echo "Press [Ctrl] C to Quit!"
echo -n "Put a 1722 formatted floppy in drive A and press: [Enter] "
read PAUSE

if [ ! -d /tmp ]; then
mkdir /tmp
chmod 1777 /tmp 2>/dev/null
fi

dd bs=1k if=$2 of=/dev/null 2>/tmp/zimage || du -k $2 2>/dev/null 1>/tmp/zimage

SIZE=`cat /tmp/zimage | sed -n "1 p" | cut -b-3`

if [ "$SIZE" -gt "720" ]; then
echo
echo "The kernel is to big: $SIZE Kb (No more than 720 Kb)"
echo
exit 1
fi

error() {
umount /tmp/floppy 2>/dev/null
echo "ERROR! Maybe the floppy disk or drive is no good."
echo "Can you format the floppy disk to 1722 without errors?"
echo "It could be something else, i just know there was a ERROR!"
echo
exit 1
}

echo
echo "This will take about 90 seconds ..."
echo

cat ./ramflop.gz | gzip -dc > $FD || error
mkdir -p /tmp/floppy
mount -t msdos $FD /tmp/floppy || error
cp $2 /tmp/floppy/kernel || error

umount /tmp/floppy 2>/dev/null && rmdir /tmp/floppy 2>/dev/null
rm -f /tmp/zimage

echo "If it got this far everything should be alright."
echo "The kernel & rescue system should be on the floppy disk."
echo "Done ..."

else
echo "Can't find: $2"
exit 1
fi
fi