#!/bin/bash

#syntax : sh create_util_usb.sh <device> <scu iso> <huu iso> <driver.iso>
#example: sh create_util_usb.sh /dev/sdb scu.iso huu.iso ucs-cxxx-1.4.3-driver.iso

if [ $# -lt 2 ];then
	echo "Missing argurments"
	echo "syntax: sh create_util_usb.sh <device> <huu-iso-image>"
	echo "example:"
	echo "	sh create_util_usb.sh /dev/sdb ucs-c220m4-huu-2.0.12.31.iso" 
	exit 1;
fi

USB_DEV=$1
SCU_ISO=$2

if [ "$3" == "debug" ]; then
	set -x
fi

#check for syslinux

rpm -qa | grep syslinux 
if [ $? -ne 0 ]; then
	echo "Error:  syslinux not installed"
	exit 1;
fi

#Script input validation

if [ -z $USB_DEV ]; then
	echo "Error: USB Device not provided"
	exit 1;
fi
if [ -z $SCU_ISO ]; then
	echo "Error: SCU ISO not provided"
	exit 1;
fi

losetup -f
if [ $? -ne 0 ]; then
	echo "Error: No free loop device found. "
	exit 1
fi

#HUU ISO name validation
basename $SCU_ISO | grep -i "huu"
if [ $? -ne 0 ]; then
	echo "The second parameter does not look HUU iso. Do you want to proceed (y/n)?"
	read ans
	ans=`echo $ans | tr [:upper:] [:lower:]`
	if [ $ans = "y" ]; then
		echo "Proceeding..."
	else
		exit 1;
	fi
fi

TMP_TEST_DIR=/tmp/scu_test.$$
mkdir -p $TMP_TEST_DIR
#SCU ISO validation
losetup -f
if [ $? -ne 0 ]; then
	echo "Error: No free loop device found. "
	exit 1
fi

mount -o loop $SCU_ISO $TMP_TEST_DIR
if [ $? -ne 0 ]; then
	echo "Error: Unable to mount SCU iso for validation"
	rm -rf $TMP_TEST_DIR
	exit 1;
fi
umount $TMP_TEST_DIR


echo " "

rm -rf $TMP_TEST_DIR


TMP_PART_SC=/tmp/partscr.sh

PART1_SIZE=8192
PART1_DEV=$USB_DEV"1"
USB_DIR=/tmp/usb2.$$
MBR=/usr/share/syslinux/mbr.bin

SYSLINUX_FILE=syslinux.cfg
MENU_FILE=menu.txt
TMP_SCU=/tmp/SCU

SCU_LCD_SUM=0


create_part ()
{
	echo "## Creating partitions... Please wait (Will take a few minutes)"
	
	umount $PART1_DEV
	dd if=/dev/zero of=$USB_DEV bs=4096 count=10
	echo "Zeroing of USB done....."
echo "d
n
p
1

+1024M
w" | fdisk $USB_DEV
	
	if [ $? -ne 0 ]; then
		echo "Error: Partition creation failed."
		exit 1;
	fi
	
	#Mark Partition as active
echo "a
1
w
" | fdisk $USB_DEV
	
	echo "####### creating partition Done ######"
}

format_part () 
{
	echo "###### Formating the partitions ######"
	mkdosfs -F 32 $PART1_DEV
	if [ $? -ne 0 ]; then
		echo "Error: Formatting of first USB partition failed"
		exit 1;
	fi
	echo "##### Formating partition Done ######"
}


add_syslinux ()
{
	echo "##### add_syslinux #####"
	dd if=$MBR of=$USB_DEV
	if [ $? -ne 0 ]; then
		echo "Error: dd of mbr.bin failed ";
		#rm -rf $USB_DIR
		exit 1;
	fi
	syslinux $PART1_DEV 
	if [ $? -ne 0 ]; then
		echo "Error: syslinux failed [ $PART1_DEV ] Failed";
		#rm -rf $USB_DIR
		exit 1;
	fi
	
	echo "##### add_syslinux DONE  ######"
}


copy_files ()
{
	echo "##### copy_files  ######"
	mkdir -p $USB_DIR
	mount $PART1_DEV $USB_DIR
	if [ $? -ne 0 ]; then
		echo "Error: Mounting of partition [ $PART1_DEV ] on Dir [ $USB_DIR ] Failed";
		rm -rf $USB_DIR
		exit 1;
	fi

	
	mkdir -p $TMP_SCU

	losetup -f
	if [ $? -ne 0 ]; then
		echo "Error: No Free loop device found. Exiting";
		umount $USB_DIR
		rm -rf $USB_DIR
		rm -rf $TMP_SCU
		exit 1;
		
	fi
	
	mount -o loop $SCU_ISO $TMP_SCU
	if [ $? -ne 0 ]; then
		echo "Error: Mounting of SCU ISO [ $SCU_ISO ] on Dir [ $TMP_SCU ] Failed";
		umount $USB_DIR
		rm -rf $USB_DIR
		rm -rf $TMP_SCU
		exit 1;
	fi

	cp -rvpf $TMP_SCU/* $USB_DIR/.
	if [ $? -ne 0 ]; then
		echo "Error: Copying of SCU Files to USB failedFailed";
		umount $USB_DIR
		rm -rf $USB_DIR
		rm -rf $TMP_SCU
		exit 1;
	fi
 	
	mv $USB_DIR/isolinux/ $USB_DIR/syslinux
    	mv $USB_DIR/syslinux/isolinux.cfg $USB_DIR/syslinux/syslinux.cfg

    	# Set the UUID of SCU SD device as part of syslinuf.cfg
    	SCU_UUID=`blkid $PART1_DEV | cut -d" " -f2 | sed 's/\"//g'`
    	sed -ir "s/root=live:CDLABEL=.*-huu-[0-9]*/root=live:${SCU_UUID}/g" $USB_DIR/syslinux/syslinux.cfg	
		sed -ir "s/root=live:CDLABEL=.*-huu-[0-9]*/root=live:${SCU_UUID}/g" $USB_DIR/EFI/BOOT/grub.cfg
	
	umount $TMP_SCU
	umount $USB_DIR
	
	rm -rf $TMP_SCU $USB_DIR	
	echo "##### copy_files Done  ######"
}

#delete_part
create_part
format_part
add_syslinux
copy_files 

fdisk -l $USB_DEV


exit 0
