#!/bin/sh
#
# This is mkusb.sh. (3-24-2006)
#
# sh mkusb.sh -h "For Help!"
#
PATH="/bin:/sbin:/usr/bin:/usr/sbin:$PATH"

help() {
    msg="Press enter to scroll, q to Quit!"
if [ -x "`type -path less`" ]; then
    pager=less
elif [ -x "`type -path more`" ]; then
    pager=more
else
    pager=cat
    msg_cat="
    Press [Shift]-[Page-Up] to scroll back!"
fi

$pager << EOF

$msg

========================================================================
          If you want to boot RIP from a USB flash drive.
========================================================================

   The USB device should be at least 32 MB for RIP-15.1.iso.
   
   If you include the FreeBSD image it should be at least 44 MB.

================== Install RIP to the USB device =================

   It will find your USB drive. Only have one USB drive plugged in!
   
   # sh mkusb.sh RIP-15.1.iso   "You'll loose anything on the
                                 USB drive!"
   
   If you want to specify the USB drive, do this.
   
   # sh mkusb.sh RIP-15.1.iso /dev/sda
   
   The above example assumes that's the name of the RIP ISO image, and
   the USB drive is on /dev/sda!

   Instead of specifying an ISO image you can specify a source directory
   that contains the contents of the ISO image, laid out the same way
   as the ISO image.
    
   # sh mkusb.sh source_directory /dev/sda

   If you had RIP on CD mounted under /mnt/cdrom, you could do this.

   sh mkusb.sh /mnt/cdrom /dev/sda 

   If you want to change or add anything to the Linux system, do this.

   # mkdir /rip1 /rip2
   # mount -t vfat /dev/sda /rip1
   # cd /rip2
   /rip2# gzip -dc /rip1/boot/rootfs.cgz | cpio -iumdv

   Make the changes in the /rip2 directory, then cpio/gzip the system.

   # rm /rip1/boot/rootfs.cgz
   /rip2# find . | bin/cpio -v -o -H newc | gzip -9 >/rip1/boot/rootfs.cgz
                     ^^^
           "Use the RIP cpio 'bin/cpio' here!"

   # rm -r /rip2
   # umount /rip1
   # rmdir /rip1

   To boot the RIP system, the BIOS must support booting from a USB device.
   $msg_cat
EOF
   exit
}

if [ "$1" = "-h" ]; then
   help
fi

error() {
   umount /tmp/rip1 2>/dev/null
   rmdir /tmp/rip1 2>/dev/null
   rm -f /tmp/rip1 2>/dev/null
   rm -f /tmp/makeboot.bin /tmp/syslinux.cfg /tmp/mkboot
   rmdir /tmp/empty_dir 2>/dev/null
   #rm -rf /tmp/doc_dir
   exit
}

if [ -n "$1" ]; then

if [ -n "$2" ]; then
if [ ! -b "$2" ]; then
   echo "ERROR: Can't find USB device \`$2'!"
   exit
fi
   usb_dev=$2
else
   usb_dev=usb
fi

if [ ! -f "$1" -a ! -d "$1" ]; then
   echo "ERROR: Can't find ISO image, or source directory \`$1'!"
   exit
fi  

umount /tmp/rip1 2>/dev/null
rmdir /tmp/rip1 2>/dev/null
rm -f /tmp/rip1 2>/dev/null
umount $2 2>/dev/null

if [ -d "$1" ]; then
   ln -s $1 /tmp/rip1 || error
else
if ! grep -q "loop" /proc/devices ; then
   echo "ERROR: I need loop support in the kernel, or loaded as a module!"
   echo "modprobe loop"
   exit
elif ! grep -q "iso9660" /proc/filesystems ; then
   echo "ERROR: I need iso9660 support in the kernel, or loaded as a module!"
   echo "modprobe iso9660"
   exit
fi
   mkdir -p /tmp/rip1
   mount -r -t iso9660 -o loop $1 /tmp/rip1 || error
fi
else
echo "
----------------------------------------------------------------------
Usage: sh mkusb.sh RIP-15.1.iso /dev/sda

The above example assumes that's the name of the RIP ISO image, and
the USB device is on /dev/sda!

Instead of specifying an ISO image you can specify a source directory
that contains the contents of the ISO image, laid out the same way
as the ISO image.

sh mkusb.sh source_directory /dev/sda

If you have RIP on CD mounted under /mnt/cdrom, you could do this.

sh mkusb.sh /mnt/cdrom /dev/sda

For complete Help! type \`sh mkusb.sh -h'.
----------------------------------------------------------------------
"
exit
fi

sys_cfg() {
cat << EOF
DEFAULT menu.c32
PROMPT 0
MENU TITLE RIPLinux

LABEL Boot Linux rescue system! (initramfs method)
KERNEL kernel
APPEND vga=normal initrd=rootfs.cgz root=/dev/ram0 rw

LABEL Boot Linux rescue system! (skip keymap prompt)
KERNEL kernel
APPEND vga=normal nokeymap initrd=rootfs.cgz root=/dev/ram0 rw

LABEL Boot Linux rescue system! (non-initramfs method)
KERNEL kernel
APPEND vga=normal rip_dev=/dev/usb,/rootfs.cgz initrd=initrd.gz init=/linuxrc root=/dev/ram0 rw

LABEL Boot Linux rescue system! (skip keymap prompt)
KERNEL kernel
APPEND vga=normal nokeymap rip_dev=/dev/usb,/rootfs.cgz initrd=initrd.gz init=/linuxrc root=/dev/ram0 rw
$freebsd
LABEL Edit and put 'root=/dev/XXXX' Linux partition to boot!
KERNEL kernel
APPEND vga=normal ro

LABEL Boot memory tester!
KERNEL memtest
APPEND -

LABEL Boot GRUB bootloader!
KERNEL grub.exe
APPEND -

LABEL Boot MBR on first hard drive
KERNEL chain.c32
APPEND hd0 0

LABEL Boot partition #1 on first hard drive
KERNEL chain.c32
APPEND hd0 1

LABEL Boot partition #2 on first hard drive
KERNEL chain.c32
APPEND hd0 2

LABEL Boot partition #3 on first hard drive
KERNEL chain.c32
APPEND hd0 3

LABEL Boot partition #4 on first hard drive
KERNEL chain.c32
APPEND hd0 4
EOF
}

for f in kernel rootfs.cgz initrd.gz makeboot/makeboot.bin ; do
if [ ! -f /tmp/rip1/boot/$f ]; then
    umount /tmp/rip1 2>/dev/null
    rmdir /tmp/rip1 2>/dev/null
    rm -f /tmp/rip1 2>/dev/null
    echo "ERROR: Can't find \`/boot/$f' on \`$1'!"
    exit
fi
done

echo "********** Working, please wait... **********"

if [ -x /tmp/rip1/boot/makeboot/makeboot.bin ]; then
    echo "/tmp/rip1/boot/makeboot/makeboot.bin \\" > /tmp/mkboot
else
    cp /tmp/rip1/boot/makeboot/makeboot.bin /tmp 2>/dev/null
    chmod 755 /tmp/makeboot.bin 2>/dev/null
    echo "/tmp/makeboot.bin \\" > /tmp/mkboot
fi

echo "-o $usb_dev -v -F -Y -Z -D -L RIPLinux -b /tmp/rip1/boot/makeboot/ldlinux.bss \\
-m /tmp/rip1/boot/makeboot/mbrfat.bin \\
-c /tmp/rip1/boot/makeboot/ldlinux.sys \\
-c /tmp/rip1/boot/rootfs.cgz \\
-c /tmp/rip1/boot/kernel \\
-c /tmp/rip1/boot/initrd.gz \\
-c /tmp/rip1/boot/memtest \\
-c /tmp/rip1/boot/isolinux/menu.c32 \\
-c /tmp/rip1/boot/isolinux/chain.c32 \\
-c /tmp/rip1/boot/grub4dos/grub.exe \\
-c /tmp/rip1/boot/makeboot/menu.lst \\
-c /tmp/rip1/boot/loadlin.exe \\
-c /tmp/rip1/boot/rip.bat \\
-c /tmp/rip1/boot/doc/grub.txt \\" >> /tmp/mkboot

if [ -f /tmp/rip1/boot/freebsd.img ]; then
  echo "-c /tmp/rip1/boot/freebsd.img \\" >> /tmp/mkboot
if [ -f /tmp/rip1/boot/memdisk ]; then
  echo "-c /tmp/rip1/boot/memdisk \\" >> /tmp/mkboot
freebsd='
LABEL Boot FreeBSD rescue system!
KERNEL memdisk
APPEND initrd=freebsd.img

LABEL Boot FreeBSD rescue system! (method #2)
KERNEL grub.exe
APPEND --config-file="map --mem --heads=255 --sectors-per-track=63 /freebsd.img (hd0);map --hook;chainloader (hd0)+1;rootnoverify (hd0);map --harddrives=1"
'
else
freebsd='
LABEL Boot FreeBSD rescue system!
KERNEL grub.exe
APPEND --config-file="map --mem --heads=255 --sectors-per-track=63 /freebsd.img (hd0);map --hook;chainloader (hd0)+1;rootnoverify (hd0);map --harddrives=1"
'
fi
fi

sys_cfg > /tmp/syslinux.cfg
echo "-c /tmp/syslinux.cfg \\" >> /tmp/mkboot

if [ -d /tmp/rip1/boot/doc.off ]; then
    mkdir -p /tmp/doc_dir
    cp -R /tmp/rip1/boot/doc /tmp/doc_dir
    echo "/tmp/doc_dir" >> /tmp/mkboot
else
    mkdir -p /tmp/empty_dir
    echo "/tmp/empty_dir" >> /tmp/mkboot
fi

sh /tmp/mkboot || error
rmdir /tmp/empty_dir 2>/dev/null
#rm -rf /tmp/doc_dir
rm -f /tmp/makeboot.bin /tmp/syslinux.cfg /tmp/mkboot

umount /tmp/rip1 2>/dev/null
rmdir /tmp/rip1 2>/dev/null
rm -f /tmp/rip1 2>/dev/null

echo "
The USB drive should be ready to boot!
Give it no more than 15 seconds to boot!
Done!"
exit 0
