#!/bin/sh
#
# This is mkusb.sh. (5-8-2008)
#
# 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 RIPLinux from a USB flash drive.
========================================================================

   The USB device should be at least 75 MB for RIPLinuX-5.3.iso.
   The USB device should be at least 40 MB for RIPLinux-5.3-non-X.iso.
   
================== Install RIPLinux to a USB device =================

   # sh mkusb.sh RIPLinuX-5.3.iso /dev/sdb
   
   The above example assumes that's the name of the ISO image, and
   the USB drive is on /dev/sdb!

   # fdisk -l  "To find the device the USB drive is on!"
               "For example, if it says /dev/sdb4, just put '/dev/sdb' etc. "

   If the USB drive is not fat16 or FAT32 formatted, use the '-f' option,
   because it must be fat16 or FAT32 formatted. You'll loose anything
   on the USB drive!
   
   # sh mkusb.sh -f RIPLinuX-5.3.iso /dev/sdb
   
   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/sdb

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

   sh mkusb.sh /mnt/cdrom /dev/sdb 

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

   # mkdir /rip1 /rip2
   # mount -t vfat /dev/sdb /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 RIPLinux 'bin/cpio' here!"

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

   To boot the RIPLinux 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
   umount /tmp/usb 2>/dev/null
   rmdir /tmp/usb 2>/dev/null
   rm -f /tmp/mkdosfs.bin /tmp/syslinux.bin
   exit
}

if [ -n "$1" ]; then

if [ "$1" = "-f" ]; then
  format=yes
  shift
fi

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

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

if ! grep -q -E "msdos|vfat" /proc/filesystems ; then
   echo "ERROR: I need msdos or vfat support in the kernel, or loaded as a module!"
   echo "modprobe msdos"
   exit
fi

umount /tmp/rip1 2>/dev/null
rmdir /tmp/rip1 2>/dev/null
rm -f /tmp/rip1 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 RIPLinuX-5.3.iso /dev/sdb

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

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/sdb

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

sh mkusb.sh /mnt/cdrom /dev/sdb

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

for f in kernel rootfs.cgz syslinux/syslinux.bin syslinux/mkdosfs.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... ***"
umount $2 2>/dev/null

   mkdir -p /tmp/usb

  if [ "$format" = "yes" ]; then
    echo "*** Fat16/32 formatting $2..."
if [ -x /tmp/rip1/boot/syslinux/mkdosfs.bin ]; then
    /tmp/rip1/boot/syslinux/mkdosfs.bin -I $2 1>/dev/null || error
else
    cp /tmp/rip1/boot/syslinux/mkdosfs.bin /tmp || error
    chmod 755 /tmp/mkdosfs.bin || error
    /tmp/mkdosfs.bin -I $2 1>/dev/null || error
    rm -f /tmp/mkdosfs.bin
fi
  fi 

   mount $2 /tmp/usb || error

   echo "*** Copying rootfs.cgz and kernel to $2..."
   mkdir -p /tmp/usb/boot/syslinux || error
   rm -f /tmp/usb/boot/syslinux/kernel /tmp/usb/boot/syslinux/rootfs.cgz
   cp /tmp/rip1/boot/kernel /tmp/usb/boot/syslinux || error
   cp /tmp/rip1/boot/rootfs.cgz /tmp/usb/boot/syslinux || error
   cp /tmp/rip1/boot/syslinux/syslinux.cfg /tmp/usb/boot/syslinux

for f in /tmp/rip1/boot/memtest /tmp/rip1/boot/grub4dos/grub.exe \
/tmp/rip1/boot/syslinux/menu.lst /tmp/rip1/boot/doc/grub.txt \
/tmp/rip1/boot/isolinux/menu.c32 /tmp/rip1/boot/isolinux/chain.c32
do
    rm -f /tmp/usb/boot/syslinux/`basename $f`
    cp $f /tmp/usb/boot/syslinux
done

   mkdir -p /tmp/usb/boot/syslinux/doc
   cp /tmp/rip1/boot/doc/* /tmp/usb/boot/syslinux/doc

if [ -x /tmp/rip1/boot/syslinux/syslinux.bin ]; then
    /tmp/rip1/boot/syslinux/syslinux.bin $2 || error
else
    cp /tmp/rip1/boot/syslinux/syslinux.bin /tmp || error
    chmod 755 /tmp/syslinux.bin || error
    /tmp/syslinux.bin $2 || error
    rm -f /tmp/syslinux.bin
fi
    mv /tmp/usb/ldlinux.sys /tmp/usb/boot/syslinux

umount /tmp/usb 2>/dev/null && rmdir /tmp/usb
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
