#!/bin/bash
#
# Make a bootable usb stick with Neptune: Configure the stick, copy a DVD image
# and install the bootloader.
# See also http://www.knoppix.net/wiki/Bootable_USB_Key , espacially for removing 
# the badware U3 if you have a stick with U3.
# 
# Tested with some 512 MB, 1 GB, 2 GB, 4 GB, 8 GB and 16 GiB 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.1, 2010-05-20, Dr. Rolf Freitag.


# For debugging and first use: Be verbose and stop in case of an error like usb disconnect or dead device.
#set -xe
# Be verbose and stop when a variable isn't set.
set -xu

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

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

# Neptune image.
imagefile="neptune19-kde-rc3.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 Neptune 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 of stability.
# Comment the following line for fast installation!
#/usr/bin/time badblocks -wsv -o badblocks_0_"$device".txt /dev/"$device"

# 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 -t random -wsv -o badblocks_1_"$device".txt /dev/"$device"

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

###############################################

# Partitioning and formatting section: Uncomment only the right section!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

# For sticks of size < 1 GiB/GB
# If the stick does not with this geometry, try 0 64 32 for 1 GiB/GB or 0 255 63
#mkdiskimage -1 /dev/"$device" 0 32 32

# For sticks of size = 1 GiB/GB: use zipdisk geometry
#mkdiskimage -1 /dev/"$device" 0 64 32

# For sticks of size > 1 GiB/GB AND <= 2 GiB/GB
#mkdiskimage -F -1 /dev/"$device" 0 128 32

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

sync
sleep 1
#mkdosfs /dev/"$device"1

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

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

# Get the 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 
#wget -c http://content.wuala.com/contents/Neptune%20Share/"$imagefile"?dl=1

# todo: get the checksum to check the checksum of the image file and the files copied
# to the USB stick

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

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

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

# unmount the image
umount /mnt/iso

####### boot loader part ##########

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

# move boot files
mv /mnt/"$device"1/isolinux /mnt/"$device"1/syslinux
mv syslinux.cfg syslinux/
sync

# copy modified syslinux.cfg with the options noswap noeject noprompt dma at the end of line 2
# and korrect freedos entry.
#sync
cp -Lr syslinux.cfg /mnt/"$device"1/
sync

# copy wifi stuff
cp -a freewifi_neptune.sh /mnt/"$device"1/tmp/
sync
cp -a diag.sh /mnt/"$device"1/tmp/
sync

### finish; this can also take some minutes!
#sync &
umount /mnt/"$device"1

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

# beep finish
/usr/bin/beep -f 500 -l 100 -n -f 1000 -l 100 -n -f 500 -l 100

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

exit 0

