#!/bin/bash
#
# Make a bootable usb stick with Kubuntu: Configure the stick, copy a kubuntu CD/DVD
# and install a new bootloader.
#
# See also http://www.knoppix.net/wiki/Bootable_USB_Key , espacially for removing 
# the badware U3 if you have a stick with U3.
# Is is recommended to use the script with appended " 2>&1 | tee logfile.txt" and to check the
# output (logfile.txt).
#
# Tested with some sticks.
#

# ----------------------------------------------------------------------------
# "THE BEER-WARE LICENSE" (Revision 43):
# Dr. Rolf Freitag (rolf dot freitag at email dot de) wrote this file.
# As long as you retain this notice you can do whatever
# the LGPL (Lesser GNU public License) allows with this stuff.
# If you think this stuff is worth it, you can send me money via
# paypal or if we met some day you can buy me a beer in return.
# ----------------------------------------------------------------------------


# Version 1.0, 2011-05-21, Dr. Rolf Freitag.


# Stop the script when a variable isn't set
set -u

# For debugging: +be verbose
#set -x -u 

if [ $# -ne 1 ]
then
 echo "Error: Not exact one parameter (the device); exiting."
 exit 1
fi

# Device (without path): First and only parameter (see dmesg and/or fdisk -l and correct if necessary or you may lose your data on a HDD/SSD)!!!
# Example: sde
device="$1"

# image file
imagefile="kubuntu-11.04-dvd-i386.iso"

# clear the display
clear

# Check if the device is availible.
fdisk -l /dev/"$device" | grep "$device" 2>&1 >/dev/null
if [ $? -ne 0 ]; then
  echo "Error: Device $device not found; exiting."
  exit 1
fi

# Check if the device is mounted, e. g. if it is the system drive
mount | grep "$device" 2>&1 >/dev/null
if [ $? -eq 0 ]; then
  echo "Error: Device $device seems to be mounted; exiting."
  exit 1
fi

echo -n "This script will install a bootable Kubuntu on device "
echo -n "$device"
echo "."
echo "Starting in 15 s (press <Ctrl>-C if you want to abort)." 
echo "-"
echo "output of fdisk -l:"
fdisk -l
sleep 15

# Test for speed and badblocks or missing blocks; override previous data.
# The average speed is size/time *0.5 (0.5 because of read and write).
# Comment the following line for fast installation!
#/usr/bin/time badblocks -wsv -c 65536 -t random -o badblocks_1_"$device".txt /dev/"$device"

#clear MBR
dd if=/dev/zero of=/dev/"$device" bs=512 count=1

# For sticks > 2 GiB/GB: The common HDD geometry.
mkdiskimage -F -1 /dev/"$device" 0 255 63

sync
sleep 1

# prepare syslinux; use -sf instead of -f only for some very buggy BIOSes on which syslinux would otherwise fail.
syslinux -f /dev/"$device"1

# prepare mountpoint directories
mkdir -p /mnt/iso
mkdir -p /mnt/"$device"1
mkdir -p /mnt/tmp

# mount the stick
mount -o async,rw,noatime,nodev /dev/"$device"1 /mnt/"$device"1

# show ldlinux.sys for checking
#ls -ilaF /mnt/"$device"1

# Get the CD or DVD image (if not downloded already).
# If the file is already downloaded, you should link it to the acutal 
# directory, e. g. by
# ln -s /home/ftp/images/KNOPPIX.iso KNOPPIX.iso
#rsync -avv rsync://ftp.gwdg.de/pub/linux/knoppix/dvd/"$imagefile"

# mount the image
mount -o loop "$imagefile" /mnt/iso 

# copy the image; this takes some minutes!
cp -Lr /mnt/iso/. /mnt/"$device"1/
sync

# copy boot files: For Ubuntu you must use the source /mnt/iso/isolinux/.
cp -Lr /mnt/iso/isolinux/. /mnt/"$device"1/
sync

# rename for syslinux
mv /mnt/"$device"1/isolinux.cfg /mnt/"$device"1/syslinux.cfg

# make tmp directory for other files; e. g. for transfering drivers or pictures
mkdir /mnt/"$device"1/tmp

# unmount the image file
umount /mnt/iso

# finish 
umount /mnt/"$device"1

echo "finished kubuntu usb-stick on device"
echo "$device"
echo "."
echo "Waiting (for Ctrl-C)."

# beep finish
/usr/bin/beep -f 500 -l 1000 -n -f 1000 -l 1000 -n -f 500 -l 1000
dd if=/dev/urandom of=/dev/dsp bs=1 count=123456

# wait (for Ctrl-C), usefull for terminals with a timeout (autologout)
sleep 123456789

exit 0

