#!/bin/bash

# This script 'writes' a Juntura ISO image onto a USB 
# storage device & makes it bootable.
#
# jjjohn, 23/Aug/06.
# 
# $Id: usbrecord.sh,v 1.3 2007/02/23 18:01:04 jjjohn Exp $
############ Some functions ###########################

execute () {
	echo -n "[$*] .....";
	$* || {
		ret=$?; echo "FAILED!";
		exit -1;
	}
	echo "OK";
	return 0;
}

display_help() {
cat <<EOF
Usage: $0 [options] <ISO Image> [USB partition]
EOF
	exit
}

########### Main ##############
REV="$Revision: 1.3 $"
REV=${REV/: /}
REV=${REV/ $/}
DATE="$Date: 2007/02/23 18:01:04 $"
DATE=${DATE/: /}
DATE=${DATE/ $/}
cat <<EOF

USB Record script, version $REV, $DATE
jaiber.j.john@intel.com

EOF

if [ $# -lt 1 ];then
	display_help
fi

if [ $UID -ne 0 ];then
	echo "****** This script requires root permission"
	exit 1
fi

# Parse the command line opts
set -- `getopt "ahn" "$@"` || {
	display_help
}

opt_flag=0
FMT_PROMPT=1
NO_FMT=0
while :
do
	case "$1" in
	-a) FMT_PROMPT=0;;
	-h) display_help;;
	-n) NO_FMT=1;;
	*) break;;
	esac
	shift
	opt_flag=1
done

#set -x
ISO=$2
DEV=$3
# Check if partition is given in non-automatic mode
if [ "$ISO" = "" ];then
	echo "****** ISO filename missing."
	display_help
fi

# Check if partition is given in non-automatic mode
if [ "$DEV" = "" ];then
	echo "****** Partition argument missing."
	display_help
fi

#echo "ISO=$ISO, DEV=$DEV"

if ! echo $ISO|grep '.iso' >/dev/null;then
	echo "****** Is $ISO really an ISO image?"
	exit 1
fi
part=""
if echo $DEV|grep ".*[0-9]$" >/dev/null;then
	part=$DEV
	DEV=`echo $DEV|sed 's/\(.*\)[0-9]$/\1/'`
fi

#read x
FDLST=/tmp/.fdlst
dsk_fnd=0
max=0
if [ "x$part" = "x" ];then

	fdisk -l >$FDLST
	exec 5<$FDLST
	while read -u 5 ln;do
		if echo $ln|grep "^Disk $DEV";then
			echo "Found $DEV!"
			dsk_fnd=1
			echo "Listing the partitions on $DEV: "
		fi
		if [ $dsk_fnd -eq 1 ];then
			if echo $ln|grep "^$DEV" >/dev/null;then
				echo -n $ln|sed "s,\($DEV\)\(.\).*,\t\2) \1,"
				let max=$max+1
				echo $max
			fi
		fi	
	done

	if [ $dsk_fnd -eq 0 ];then
		echo "******* Cant find USB device $DEV!"
		exec 5<&-
		exit 1
	fi
	exec 5<&-
	choice=0
	if [ $max -eq 0 ];then
		echo "No partitions found. Will create one(280MB) now.."
		# Create an ext3 one with 250MB using fdisk
		choice=1
	fdisk $DEV << EOF
n
p
1

+280M
w

EOF
	else
		while [ $choice -lt 1 -o $choice -gt $max ];do
			read -p "Which partition to install Juntura? Enter your choice[1-$max]: " choice
			#echo "Entered choice = $choice"
		done
		part=$DEV$choice
	fi
fi

echo
read -p "!!!! All files in $part will be lost!!!! Press ENTER to continue.." x

# Set partition type
#set -x
max=0
fdisk -l $DEV 2>/dev/null |grep "^/" > $FDLST
exec 5<$FDLST
while read -u 5 ln;do
	let max=$max+1
done
exec 5<&-
echo -n "Setting partition type.."
pnum=`echo $part|sed 's/.*\([0-9]\)$/\1/'`
if [ $max -eq 1 ];then
	fdisk $DEV 1>/dev/null <<EOF
t
83
w

EOF
else
	fdisk $DEV 1>/dev/null <<EOF
t
$pnum
83
w

EOF
fi
echo "..OK"
if [ $NO_FMT -eq 0 ];then
	echo -n "Formatting $part.."
	umount $part
	mkfs.ext3 -q $part || {
		echo -e "\n****** Formatting error. Exiting.."
		exit 2
	}
	echo " OK"
fi

cd_mnt_dir=/tmp/cdmnt$$
if [ -d $cd_mnt_dir ];then
	echo "Deleting $cd_mnt_dir"
	umount $cd_mnt_dir 2>/dev/null
	rm -rf $cd_mnt_dir || {
		echo "****** Cant delete temp dir!"
		exit 3
	}
fi

usb_mnt_dir=/tmp/usbmnt$$
if [ -d $usb_mnt_dir ];then
	echo "Deleting $usb_mnt_dir"
	umount $usb_mnt_dir 2>/dev/null
	rm -rf $usb_mnt_dir || {
		echo "****** Cant delete temp dir!"
		exit 3
	}
fi

lp_mnt_dir=/tmp/lpmnt$$
if [ -d $lp_mnt_dir ];then
	echo "Deleting $lp_mnt_dir"
	umount $lp_mnt_dir 2>/dev/null
	rm -rf $lp_mnt_dir || {
		echo "****** Cant delete temp dir!"
		exit 3
	}
fi

mkdir -p $cd_mnt_dir $usb_mnt_dir $lp_mnt_dir

# Mount ISO key
mount -o loop $ISO $cd_mnt_dir || {
	echo "****** Unable to mount $ISO!"
	exit 3
}

# Mount USB key
mount $part $usb_mnt_dir || {
	echo "****** Unable to mount $part!"
	umount $cd_mnt_dir
	exit 3
}

echo -n "Copying files to USB drive. It may take a few minutes, please wait.."
cp -a $cd_mnt_dir/* $usb_mnt_dir || {
	echo "***** Unable to copy all files to USB.."
	umount $cd_mnt_dir
	umount $usb_mnt_dir
	exit 3
}
sync
echo "..OK"
# Copy LILO files..
rm -f initrd
gunzip <$cd_mnt_dir/boot/isolinux/initrd.gz >initrd 2>/dev/null
mount -o loop initrd $lp_mnt_dir || {
	echo "****** Unable to mount initrd"
	umount $cd_mnt_dir
	umount $usb_mnt_dir
	exit 3
}

cd $lp_mnt_dir
cp -f sbin/bootsect.b $usb_mnt_dir/boot
cp -f etc/lilo.conf $usb_mnt_dir/boot
cp -f sbin/lilo /tmp

# Get the CD Label
LABEL=`grep "LABEL=" etc/platform|sed 's/LABEL=//'`
#pwd
#cat etc/isolinux.cfg
PLAT=`grep -m 1 platform $usb_mnt_dir/boot/isolinux/isolinux.cfg|sed 's/.*platform=\(.*\)/\1/'`
#echo "PLAT=$PLAT"
#read a
echo "LABEL = [$LABEL]"
# Modify LILO config
#set -x
sed -i -e "s,/dev/sda,$DEV," -e "s,/mnt/usb,$usb_mnt_dir," -e "s,PLAT,$PLAT," \
	$usb_mnt_dir/boot/lilo.conf
cp $usb_mnt_dir/boot/lilo.conf /tmp

echo -n "Installing bootloader, please wait.."
# Make USB bootable
/tmp/lilo -C $usb_mnt_dir/boot/lilo.conf 2>/dev/null
echo "..OK"
cd -
echo -n "Sync files to disk.."
umount $lp_mnt_dir
echo ".. OK"
# Cleanup
rm -rf initrd
cd /
umount $cd_mnt_dir
umount $usb_mnt_dir
rm -rf $mnt_dir $lp_mnt_dir $usb_mnt_dir

# Setup LABEL, finally!
e2label $part "$LABEL"


echo "############# DONE ###############"

