#!/bin/bash
# make bootable USB flash drive. Adapted from
# batch file to create bootable SLAX disk
# usage: make_usb.sh /dev/sd? # (no partition)
# WARNING! boot sector of the device will be OVERWRITTEN!
# make_disk.sh author: Tomas M. <http://www.linux-live.org>

if [ "$1" = "" ]; then
  echo "usage: $0 USB Flash drive"
  echo "example: $0 /dev/sdh"
  echo "Where /dev/sdh is your USB flash drive"
  echo "WARNING! this command will overwrite the boot sector of whatever device you give it!"
  exit 1
fi

PART="$1"

if [ -f /proc/mounts ]; then
  M=`grep -qw $PART /proc/mounts`
  if [ $M = 0 ]; then
    echo device $PART appears to be mounted
    exit 1
  fi
fi

TMPDIR=/mnt/makedisk_mount$$
echo "making VFAT filesystem on $PART"
/sbin/mkfs.vfat -I $PART
if [ $? -ne 0 ]; then 
echo "/sbin/mkfs.vfat failed!!";exit 1; fi

echo "Mounting $PART to $TMPDIR..."
mkdir -p $TMPDIR
mount "$PART" $TMPDIR
if [ $? -ne 0 ]; then exit 1; fi

echo "Copying files..."
cp -a ./* "$TMPDIR"
if [ $? -ne 0 ]; then exit 1; fi
sync

echo "Setting up syslinux on $BOOT..."
echo "prompt 1
timeout 40
default linux

label linux
kernel boot/vmlinuz
append max_loop=255 initrd=boot/initrd.gz init=linuxrc load_ramdisk=1 prompt_ramdisk=0 ramdisk_size=4444 root=/dev/ram0 rw enforcing=0
" >$TMPDIR/syslinux.cfg
syslinux $PART
if [ $? -ne 0 ] 
then 
echo "syslinux $PART"
exit 1
fi

umount $TMPDIR
rmdir $TMPDIR

