#! /bin/bash

. "${BASH_SOURCE%/*}/inisup"

clear
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "      The makeISO script to create customized ISO file - TeraByte, Inc"
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo

# check running as root
userid=`id -u`
if [ ! "$userid" = "0" ]; then
	echo "This script must be run as root...exiting"
	echo
	exit 1
fi

# get directory owner and group
currentdir=`pwd`
ownerid=`stat -c %U $currentdir`
groupid=`stat -c %G $currentdir`
if [ "$ownerid" = "root" ]; then ownerid=0; fi
if [ "$groupid" = "root" ]; then groupid=0; fi
if [ "$currentdir" = "/" ]; then
	echo "This script cannot be run from the / directory...exiting"
	echo
	exit 1
fi

# check for read/write access and running on Linux fs
tf1=0;tf2=0
for i in `seq 1 5000`
do
	if [ "$tf1" = "0" ]; then
		if [ ! -f t$i ] && [ ! -d t$i ]; then tf1=t$i; fi
	elif [ "$tf2" = "0" ]; then
		if [ ! -f t$i ] && [ ! -d t$i ]; then tf2=t$i; fi
	else
		break
	fi
done
if [ ! "$tf1" = "0" ] && [ ! "$tf2" = "0" ]; then
	touch $tf1 2> /dev/null
	if [ ! -e $tf1 ]; then
		echo "Unable to create a test file. This indicates that this partition"
		echo "is mounted read only. To work with the IFL disk from Linux, the"
		echo "zip archive must be extracted on a *Linux* partition, and that"
		echo "partition must be mounted with read/write privileges...exiting"
		echo
		exit 1
	fi
	chmod +x $tf1
	chmod -x $tf1
	cp $tf1 $tf2
	if [ -x $tf2 ]; then
		rm $tf1; rm $tf2
		echo "This script must be run on a Linux file system. It appears to be"
		echo "either FAT/FAT32 or NTFS. Please extract the IFL zip archive on"
		echo "a Linux partition (ext2/3 or reiserfs)...exiting"
		echo
		exit 1
	fi
	rm $tf1; rm $tf2
fi

echo "Checking for required files, directories"
m=0
if [ -x /sbin/modprobe ]; then
	modprobepath=/sbin/modprobe
elif [ -x /usr/sbin/modprobe ]; then
	modprobepath=/usr/sbin/modprobe
else
	m=1
	echo "Required file modprobe (typically /sbin/modprobe) not found on the system"
fi
if [ ! -e iflnet.iso ]; then
	m=1
	echo "File iflnet.iso not found in IFL directory"
fi
if [ ! -e ifl.iso ]; then
	m=1
	echo "File ifl.iso not found in IFL directory"
fi
if [ ! -d util ]; then
	m=1
	echo "Directory util was not found in IFL directory"
fi
# check if util/cpio will execute (won't on 64 bit systems)
# use /bin/cpio if it exists, otherwise exit
while [ 0 ]; do
	if [ -x util/cpio ]; then
		./util/cpio --help > /dev/null 2>&1
		if [ "$?" = "0" ]; then
			cpiopath="../util/cpio"
			echo "Using included version of cpio"
			break
		fi
	fi
	cpiopath=`which cpio 2> /dev/null`
	if [ "$?" != "0" ]; then
		m=1
		echo "Required file cpio (tpyically /bin/cpio) not found on system"
		break
	else
		cpioversion=`$cpiopath --version`
		echo "Using cpio version $cpioversion"
		break
	fi
done

# determine which genisoimage / mkisofs to use -> makeisopath variable
# NOTE: iso files are now **built under chroot**, so genisoimage version and 32/64 bitness of distro doesn't matter

if [ ! -e config.txt ]; then
	m=1
	echo "Required file config.txt not found in IFL directory"
fi

zcatpath=`which zcat 2> /dev/null`
if [ ! "$?" = "0" ]; then
	m=1
	echo "Required program zcat (typically /bin/zcat) not found on system."
fi

# check for unzip if any language files present (other than ifl_en.zip)
nlangfiles=`ls ifl_*.zip 2> /dev/null | grep -c ifl`
if [ "$nlangfiles" != "0" ]; then
	zipcheck=1
	if [ "$nlangfiles" = "1" ] && [ -f ifl_en.zip ]; then zipcheck=0; fi
	if [ "$nlangfiles" = "1" ] && [ -f ifl_en_trial.zip ]; then zipcheck=0; fi
	if [ "$nlangfiles" = "1" ] && [ -f ifl_en_gui.zip ]; then zipcheck=0; fi
	if [ "$nlangfiles" = "1" ] && [ -f ifl_en_gui_trial.zip ]; then zipcheck=0; fi
	if [ "$zipcheck" = "1" ]; then
		unzippath=`which unzip 2> /dev/null`
		if [ ! "$?" = "0" ]; then
			m=1
			echo "Required program unzip not found on system. This can usually be corrected"
			echo "by installing the unzip package."
		fi
	fi
fi

if [ "$m" = "0" ]; then
	echo "All required files and directories found"
else
	echo
	echo "One or more required files or directories were not found"
	echo "See above messages for details...exiting script"
	echo
	exit 1
fi

# clean up any previous contents of working directories
echo "Cleaning up any previous contents of working directories"
for workingdir in cdroot initrmfs
do
	rm -r $workingdir 2> /dev/null
	mkdir $workingdir
done

# copy files from iflnet.iso to cdroot
echo "mount 0: iflnet.iso" > getiflnetfiles.run
echo "copy 0: cdroot -s -q" >> getiflnetfiles.run
./util/tbosdt getiflnetfiles.run
rm getiflnetfiles.run
# decompress and extract root file system so that changes can be made
echo "Decompressing and extracting the root filesystem"
cd initrmfs
$zcatpath ../cdroot/initrmfs.gz | $cpiopath -i -m -d
cd ..
rm cdroot/initrmfs.gz 2> /dev/null

# check for unreg flag at /tbu/utility/ur to determine if reg or unreg version
if [ -f initrmfs/tbu/utility/ur ]; then reg=0; else reg=1; fi
# check for gui flag at /tbu/utility/gui to determine if gui version or not
if [ -f initrmfs/tbu/utility/gui ]; then gui=1; else gui=0; fi

# source config.txt to get options
. config.txt

# get disk version to build
chroot initrmfs /usr/bin/setdiskversion
Mselect=`cat initrmfs/tmp/Mselect`
rm initrmfs/tmp/Mselect 2> /dev/null
if [ "$Mselect" = "Full Version with network support" ]; then
	netsupport=1
	if [ ! -d util/settings ]; then mkdir util/settings; fi
	if [ ! -d util/settings/network ]; then mkdir util/settings/network; fi
	settingspath="util/settings"
else
	# set up to create the non-network version (ifl.iso)
	netsupport=0
	chroot initrmfs /usr/bin/dialog --no-shadow --infobox "  Please wait..." 3 21 2> /dev/null
	if [ ! -d util/settings ]; then mkdir util/settings; fi
	settingspath="util/settings"
	for workingdir in cdroot initrmfs
	do
		rm -r $workingdir 2> /dev/null
		mkdir $workingdir
	done
	echo "mount 0: ifl.iso" > getiflfiles.run
	echo "copy 0: cdroot -s -q" >> getiflfiles.run
	./util/tbosdt getiflfiles.run
	rm getiflfiles.run
	cd initrmfs
	$zcatpath ../cdroot/initrmfs.gz | $cpiopath -i -m -d --quiet
	cd ..
	rm cdroot/initrmfs.gz 2> /dev/null
fi

# get settings from saved values, and copy ndiswrapper directory to root fs
cp $settingspath/* initrmfs/tbu/utility 2> /dev/null
if [ "$netsupport" = "1" ]; then
	rulesfile="70-persistent-net.rules"
	cp $settingspath/network/* initrmfs/etc/network 2> /dev/null
	mv initrmfs/etc/network/$rulesfile initrmfs/etc/udev/rules.d/$rulesfile 2> /dev/null
	if [ -d wireless/ndiswrapper ]; then
		cp -a wireless/ndiswrapper initrmfs/tbu/ndiswrapper
		chown -R 0:0 initrmfs/tbu/ndiswrapper/
	fi
fi

# copy scripts directory to root fs, ensure files are owned by root
if [ -d scripts ]; then
	# echo "Copying scripts directory to the root filesystem"
	cp -a scripts initrmfs/tbu/scripts
	chown -R 0:0 initrmfs/tbu/scripts/
fi

# take care of language settings in root fs
if [ "$gui" = "1" ]; then
	if [ "$reg" = "1" ]; then langfilefinish="_gui.zip"; else langfilefinish="_gui_trial.zip"; fi
elif [ "$gui" = "0" ]; then
	if [ "$reg" = "1" ]; then langfilefinish=".zip"; else langfilefinish="_trial.zip"; fi
fi
if [ -e initrmfs/tbu/utility/lang ]; then lang=`cat initrmfs/tbu/utility/lang`; else lang=""; fi
if [ -e initrmfs/tbu/utility/rdlang ]; then rdlang=`cat initrmfs/tbu/utility/rdlang`; else rdlang=""; fi
rm initrmfs/tbu/utility/lang-avail 2> /dev/null
while read line
do
	if [ -f ifl_${line:0:2}$langfilefinish ]; then
		echo $line >> initrmfs/tbu/utility/lang-avail
	else
		if [ "$lang" = "${line:0:2}" ]; then rm initrmfs/tbu/utility/lang 2> /dev/null; fi
		if [ "$rdlang" = "${line:0:2}" ]; then rm initrmfs/tbu/utility/rdlang 2> /dev/null; fi
	fi
done < "initrmfs/tbu/utility/lang-versions"

# call settings menu
chroot initrmfs /usr/bin/custom-menu

# save settings and clean up root fs
for i in iflkmap ktl vga xres vmode vmodeuefi boot iflscript lang lvm md rdiflkmap rdktl rdlang rdvga rdxres rdvmodeuefi rdvmode rdboot rdcmd rdlvm rdmenu rdmenuconfig
do
	if [ -e initrmfs/tbu/utility/$i ]; then
		cp -a initrmfs/tbu/utility/$i $settingspath/$i
	else
		rm $settingspath/$i 2> /dev/null
	fi
done
for i in iflkmap ktl vga xres lang rdlang lang-avail
do
	if [ -e initrmfs/tbu/utility/$i ]; then
		rm initrmfs/tbu/utility/$i 2> /dev/null
	fi
done

# if net version, save network config files
if [ "$netsupport" = "1" ]; then
	for i in interfaces ifacelist ndisinstall
	do
		if [ -f initrmfs/etc/network/$i ]; then
			cp -a initrmfs/etc/network/$i $settingspath/network/$i
		else
			rm $settingspath/network/$i 2> /dev/null
		fi
	done
	cmp -s initrmfs/etc/network/interfaces initrmfs/etc/network/interfaces-default 2> /dev/null
	if [ "$?" = "0" ]; then rm $settingspath/network/interfaces 2> /dev/null; fi 
	rm $settingspath/network/settings-* 2> /dev/null
	cp -a initrmfs/etc/network/settings-* $settingspath/network 2> /dev/null
	rm $settingspath/network/settings-default 2> /dev/null
	if [ -f initrmfs/etc/udev/rules.d/$rulesfile ]; then
		cp -a initrmfs/etc/udev/rules.d/$rulesfile $settingspath/network/$rulesfile
	else
		rm $settingspath/network/$rulesfile 2> /dev/null
	fi
fi

# get variables for config files
# keyboard
if [ -e $settingspath/iflkmap ]; then
	kbd=`cat $settingspath/iflkmap`
	ktl=`cat $settingspath/ktl`
else
	kbd=us
	ktl=qwerty
fi
# video
if [ -f $settingspath/vmode ]; then
	defaultitem=`cat $settingspath/vmode`
elif [ -f initrmfs/tbu/utility/x64 ]; then
	defaultitem=2
else
	defaultitem=1
fi
# UEFI video
if [ -e $settingspath/vmodeuefi ]; then
	vmodeuefi=`cat $settingspath/vmodeuefi`
else
	vmodeuefi=0
fi

# boot parameters
if [ -e $settingspath/boot ]; then
	bootparms=`cat $settingspath/boot`
else
	bootparms=""
fi

if [ ! -f initrmfs/tbu/utility/gui ]; then
	# build boot loader config file (isolinux.cfg)
	echo "timeout 50" > cdroot/isolinux.cfg
	echo "kbdmap $ktl.ktl" >> cdroot/isolinux.cfg
	echo "prompt 1" >> cdroot/isolinux.cfg
	echo "display menu.txt" >> cdroot/isolinux.cfg
	echo "default $defaultitem" >> cdroot/isolinux.cfg
	echo "label 1" >> cdroot/isolinux.cfg
	echo " kernel vmlinuz" >> cdroot/isolinux.cfg
	echo " append vga=0 iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0  $bootparms" >> cdroot/isolinux.cfg
	echo "label 2" >> cdroot/isolinux.cfg
	echo " kernel vmlinuz" >> cdroot/isolinux.cfg
	echo " append vga=773 iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0  $bootparms" >> cdroot/isolinux.cfg
	echo "label 3" >> cdroot/isolinux.cfg
	echo " kernel vmlinuz" >> cdroot/isolinux.cfg
	echo " append vga=771 iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0  $bootparms" >> cdroot/isolinux.cfg
	echo "label 4" >> cdroot/isolinux.cfg
	echo " kernel vmlinuz" >> cdroot/isolinux.cfg
	echo " append vga=769 iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0  $bootparms" >> cdroot/isolinux.cfg
	echo "label 5" >> cdroot/isolinux.cfg
	echo " kernel vmlinuz" >> cdroot/isolinux.cfg
	echo " append vga=ask iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0  $bootparms" >> cdroot/isolinux.cfg
	echo "label 4gh" >> cdroot/isolinux.cfg
	echo " kernel vmlinuz" >> cdroot/isolinux.cfg
	echo " append vga=0 mem=2G iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0  $bootparms" >> cdroot/isolinux.cfg

	# build menu.txt file 
	if [ "$netsupport" = "1" ]; then
		boottitle="Image For Linux (CUI) Network Boot Disk - TeraByte, Inc."
	else
		boottitle="Image For Linux (CUI) Boot Disk - TeraByte, Inc."
	fi
	echo "" > cdroot/menu.txt
	echo $boottitle >> cdroot/menu.txt
	echo "" >> cdroot/menu.txt
	echo "Boot Menu:" >> cdroot/menu.txt
	echo "<1>  80x25 text mode" >> cdroot/menu.txt
	echo "<2>  1024x768 frame buffer mode" >> cdroot/menu.txt
	echo "<3>  800x600 frame buffer mode" >> cdroot/menu.txt
	echo "<4>  640x480 frame buffer mode" >> cdroot/menu.txt
	echo "<5>  Select alternate video mode during boot" >> cdroot/menu.txt
	echo "" >> cdroot/menu.txt
	echo "Booting default <$defaultitem> in 5 seconds. Press <Enter> to boot now." >> cdroot/menu.txt
	echo "" >> cdroot/menu.txt

	# For UEFI mode
	if [ "$netsupport" = "1" ]; then
		cat cdroot/boot/grub/grub.cfg-default | sed -e "s/iflkmap=us ktl=qwerty/iflkmap=$kbd ktl=$ktl/" | sed -e "s/iflbflag=0/iflbflag=0 $bootparms/" | sed -e "s/default=0/default=$vmodeuefi/" > cdroot/boot/grub/grub.cfg
	else
		cat cdroot/boot/grub/grub.cfg-default | sed -e "s/Network Boot/Boot/" | sed -e "s/iflkmap=us ktl=qwerty/iflkmap=$kbd ktl=$ktl/" | sed -e "s/iflbflag=0/iflbflag=0 $bootparms/" | sed -e "s/default=0/default=$vmodeuefi/" > cdroot/boot/grub/grub.cfg
	fi
	
elif [ -f initrmfs/tbu/utility/gui ] && [ -f initrmfs/tbu/utility/x64 ]; then
	# build boot loader config file for GUI version (isolinux.cfg)
	echo "timeout 50" > cdroot/isolinux.cfg
	echo "kbdmap $ktl.ktl" >> cdroot/isolinux.cfg
	echo "prompt 1" >> cdroot/isolinux.cfg
	echo "display menu.txt" >> cdroot/isolinux.cfg
	echo "default $defaultitem" >> cdroot/isolinux.cfg
	echo "label 1" >> cdroot/isolinux.cfg
	echo " kernel vmlinuz" >> cdroot/isolinux.cfg
	echo " append vga=794 switchx=yes iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0  $bootparms" >> cdroot/isolinux.cfg
	echo "label 2" >> cdroot/isolinux.cfg
	echo " kernel vmlinuz" >> cdroot/isolinux.cfg
	echo " append vga=791 switchx=yes iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0  $bootparms" >> cdroot/isolinux.cfg
	echo "label 3" >> cdroot/isolinux.cfg
	echo " kernel vmlinuz" >> cdroot/isolinux.cfg
	echo " append vga=788 switchx=yes iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0  $bootparms" >> cdroot/isolinux.cfg
	echo "label 4" >> cdroot/isolinux.cfg
	echo " kernel vmlinuz" >> cdroot/isolinux.cfg
	echo " append vga=785 switchx=yes iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0  $bootparms" >> cdroot/isolinux.cfg
	echo "label 4gh" >> cdroot/isolinux.cfg
	echo " kernel vmlinuz" >> cdroot/isolinux.cfg
	echo " append vga=791 switchx=yes mem=2G iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0  $bootparms" >> cdroot/isolinux.cfg

	# build menu.txt file 
	if [ "$netsupport" = "1" ]; then
		boottitle="Image For Linux (GUI) Network Boot Disk - TeraByte, Inc."
	else
		boottitle="Image For Linux (GUI) Boot Disk - TeraByte, Inc."
	fi

	echo "" > cdroot/menu.txt
	echo $boottitle >> cdroot/menu.txt
	echo "" >> cdroot/menu.txt
	echo "Boot Menu:" >> cdroot/menu.txt
	echo "<1>  1280x1024 frame buffer mode" >> cdroot/menu.txt
	echo "<2>  1024x768 frame buffer mode" >> cdroot/menu.txt
	echo "<3>  800x600 frame buffer mode" >> cdroot/menu.txt
	echo "<4>  640x480 frame buffer mode" >> cdroot/menu.txt
	echo "" >> cdroot/menu.txt
	echo "For systems that fail to start the GUI after boot, please try" >> cdroot/menu.txt
	echo "another video mode from the list above (e.g. boot: 3)." >> cdroot/menu.txt
	echo "" >> cdroot/menu.txt
	echo "Booting default <$defaultitem> in 5 seconds. Press <Enter> to boot now." >> cdroot/menu.txt
	echo "" >> cdroot/menu.txt

	# For UEFI mode
	if [ "$netsupport" = "1" ]; then
		cat cdroot/boot/grub/grub.cfg-default | sed -e "s/iflkmap=us ktl=qwerty/iflkmap=$kbd ktl=$ktl/" | sed -e "s/iflbflag=0/iflbflag=0 $bootparms/" | sed -e "s/default=0/default=$vmodeuefi/" > cdroot/boot/grub/grub.cfg
	else
		cat cdroot/boot/grub/grub.cfg-default | sed -e "s/Network Boot/Boot/" | sed -e "s/iflkmap=us ktl=qwerty/iflkmap=$kbd ktl=$ktl/" | sed -e "s/iflbflag=0/iflbflag=0 $bootparms/" | sed -e "s/default=0/default=$vmodeuefi/" > cdroot/boot/grub/grub.cfg
	fi
elif [ -f initrmfs/tbu/utility/gui ] && [ ! -f initrmfs/tbu/utility/x64 ]; then
	# build boot loader config file for GUI version (isolinux.cfg)
	echo "timeout 50" > cdroot/isolinux.cfg
	echo "kbdmap $ktl.ktl" >> cdroot/isolinux.cfg
	echo "prompt 1" >> cdroot/isolinux.cfg
	echo "display menu.txt" >> cdroot/isolinux.cfg
	echo "default $defaultitem" >> cdroot/isolinux.cfg
	echo "label 1" >> cdroot/isolinux.cfg
	echo " kernel vmlinuz" >> cdroot/isolinux.cfg
	echo " append vga=773 iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0  $bootparms" >> cdroot/isolinux.cfg
	echo "label 2" >> cdroot/isolinux.cfg
	echo " kernel vmlinuz" >> cdroot/isolinux.cfg
	echo " append vga=773 xres=1024 iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0  $bootparms" >> cdroot/isolinux.cfg
	echo "label 3" >> cdroot/isolinux.cfg
	echo " kernel vmlinuz" >> cdroot/isolinux.cfg
	echo " append vga=773 xres=800 iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0  $bootparms" >> cdroot/isolinux.cfg
	echo "label 4" >> cdroot/isolinux.cfg
	echo " kernel vmlinuz" >> cdroot/isolinux.cfg
	echo " append vga=773 xres=640 iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0  $bootparms" >> cdroot/isolinux.cfg
	echo "label 1fb" >> cdroot/isolinux.cfg
	echo " kernel vmlinuz" >> cdroot/isolinux.cfg
	echo " append vga=794 switchx=yes iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0  $bootparms" >> cdroot/isolinux.cfg
	echo "label 2fb" >> cdroot/isolinux.cfg
	echo " kernel vmlinuz" >> cdroot/isolinux.cfg
	echo " append vga=791 switchx=yes iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0  $bootparms" >> cdroot/isolinux.cfg
	echo "label 3fb" >> cdroot/isolinux.cfg
	echo " kernel vmlinuz" >> cdroot/isolinux.cfg
	echo " append vga=788 switchx=yes iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0  $bootparms" >> cdroot/isolinux.cfg
	echo "label 4fb" >> cdroot/isolinux.cfg
	echo " kernel vmlinuz" >> cdroot/isolinux.cfg
	echo " append vga=785 switchx=yes iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0  $bootparms" >> cdroot/isolinux.cfg
	echo "label 4gh" >> cdroot/isolinux.cfg
	echo " kernel vmlinuz" >> cdroot/isolinux.cfg
	echo " append vga=773 mem=2G iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0  $bootparms" >> cdroot/isolinux.cfg

	# build menu.txt file 
	if [ "$netsupport" = "1" ]; then
		boottitle="Image For Linux (GUI) Network Boot Disk - TeraByte, Inc."
	else
		boottitle="Image For Linux (GUI) Boot Disk - TeraByte, Inc."
	fi

	echo "" > cdroot/menu.txt
	echo $boottitle >> cdroot/menu.txt
	echo "" >> cdroot/menu.txt
	echo "Boot Menu:" >> cdroot/menu.txt
	echo "<1>  Best available graphics mode" >> cdroot/menu.txt
	echo "<2>  1024x768 graphics mode" >> cdroot/menu.txt
	echo "<3>  800x600 graphics mode" >> cdroot/menu.txt
	echo "<4>  640x480 graphics mode" >> cdroot/menu.txt
	echo "" >> cdroot/menu.txt
	echo "For systems that fail to start the GUI after boot, please use" >> cdroot/menu.txt
	echo "frame buffer option 1fb, 2fb, 3fb, or 4fb (e.g. boot: 2fb)." >> cdroot/menu.txt
	echo "" >> cdroot/menu.txt
	echo "Booting default <$defaultitem> in 5 seconds. Press <Enter> to boot now." >> cdroot/menu.txt
	echo "" >> cdroot/menu.txt

	# For UEFI mode
	if [ "$netsupport" = "1" ]; then
		cat cdroot/boot/grub/grub.cfg-default | sed -e "s/iflkmap=us ktl=qwerty/iflkmap=$kbd ktl=$ktl/" | sed -e "s/iflbflag=0/iflbflag=0 $bootparms/" | sed -e "s/default=0/default=$vmodeuefi/" > cdroot/boot/grub/grub.cfg
	else
		cat cdroot/boot/grub/grub.cfg-default | sed -e "s/Network Boot/Boot/" | sed -e "s/iflkmap=us ktl=qwerty/iflkmap=$kbd ktl=$ktl/" | sed -e "s/iflbflag=0/iflbflag=0 $bootparms/" | sed -e "s/default=0/default=$vmodeuefi/" > cdroot/boot/grub/grub.cfg
	fi
fi


clear
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "      The makeISO script to create customized ISO file - TeraByte, Inc"
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo
echo "OPTION 1: Settings menu"
echo

# prompt for console login option
echo "OPTION 2: Do you want a console login to be required (default is No)?"
echo "Enter y and press <Enter> for Yes"
echo -n "Just press <Enter> for No  "
yn=0
read yn
if [ "$yn" = "y" ] || [ "$yn" = "Y" ]; then
	echo "Configuring root filesystem to require console login"
	echo "Note: The default root password is 'ifl'"
	cp -a initrmfs/etc/inittab-getty initrmfs/etc/inittab
else
	echo "Console login will not be required (default)"
fi

# prompt for password change option
echo
echo "OPTION 3: Change root password from the default of 'ifl'?"
echo "Enter y and press <Enter> for Yes"
echo -n "Just press <Enter> for No  "
yn=0
read yn
if [ "$yn" = "y" ] || [ "$yn" = "Y" ]; then
	echo "The root password will be changed"
	echo "You will now be prompted to enter a new root password"
	echo "Password can be ANY string of 1 to 8 characters"
	ps=0
	while [ "$ps" = "0" ]
	do
		chroot initrmfs /usr/bin/passwd
		if [ "$?" = "0" ]; then
			ps=1
		else
			echo "Error while changing password...please try again"
		fi
	done
else
	echo "The default root password will be used"
fi
echo

if [ "$netsupport" = "1" ]; then
	echo "Building custom full version with network support"    
else
	echo "Building custom smaller version with no network support"    
fi

# copy files and directories to root file system
if [ -e ifl-custom.ini ]; then
	keyini="initrmfs/tbu/ifl.ini license productkey"
	userini="initrmfs/tbu/ifl.ini license user"
	productkey=$(ReadIniValue $keyini)
	prodkeyname=$(ReadIniValue $userini)
	echo "Copying ifl-custom.ini to the root filesystem as ifl.ini"
	cp -a ifl-custom.ini initrmfs/tbu/ifl.ini
	chown 0:0 initrmfs/tbu/ifl.ini
	if [ "$(ReadIniValue $keyini)" = "" ]; then
		WriteIniValue $userini "$prodkeyname"
		WriteIniValue $keyini "$productkey"
	fi
fi

if [ -e config.txt ]; then
	echo "Copying config.txt to the root filesystem"
	cp -a config.txt initrmfs/etc/config.txt
	chown 0:0 initrmfs/etc/config.txt
elif [ -e CONFIG.TXT ]; then
	echo "Copying CONFIG.TXT to the root filesystem"
	cp -a CONFIG.TXT initrmfs/etc/config.txt
	chown 0:0 initrmfs/etc/config.txt
fi

#if [ -d scripts ]; then
	#echo "Copying scripts directory to the root filesystem"
	#cp -a scripts initrmfs/tbu/scripts
	#chown -R 0:0 initrmfs/tbu/scripts/
#fi

if [ -d wireless/firmware ] && [ "$netsupport" = "1" ]; then
	echo "Copying wireless/firmware directory to the root filesystem"
	cp -a wireless/firmware/* initrmfs/lib/firmware 2> /dev/null
	chown -R 0:0 initrmfs/lib/firmware/
fi

if [ -d iscsi ] && [ "$netsupport" = "1" ]; then
	echo "Copying iscsi directory to the root filesystem"
	rm -r initrmfs/etc/iscsi
	cp -a iscsi initrmfs/etc/iscsi
	cp -a help/iscsi.txt initrmfs/etc/iscsi/iscsi.txt
	chown -R 0:0 initrmfs/etc/iscsi/
fi

if [ -d tbosdtfiles ]; then
	echo "Copying tbosdtfiles directory to the root filesystem"
	cp -a tbosdtfiles initrmfs/tbu
	chown -R 0:0 initrmfs/tbu/tbosdtfiles/
fi

if [ -f BOOTITBM-CUSTOM.INI ]; then
	echo "Copying BOOTITBM-CUSTOM.INI to the root filesystem as BOOTITBM.INI"
	cp -a BOOTITBM-CUSTOM.INI initrmfs/tbu/BOOTITBM.INI
	chown 0:0 initrmfs/tbu/BOOTITBM.INI
fi

# process language versions selected in menus
# lang is boot media setting, rdlang is restore disc setting
# if lang setting doesn't exist, it's English
if [ -e $settingspath/lang ]; then
	lang=`cat $settingspath/lang`
	while read line
	do
		if [ "${line:0:2}" = "$lang" ]; then
		langtext=${line:3}
		break
		fi
	done < "initrmfs/tbu/utility/lang-versions"
	# if rdlang doesn't exist, it's the same as lang
	if [ -e $settingspath/rdlang ]; then 
		rdlang=`cat $settingspath/rdlang`
	else
		rdlang=$lang
	fi
	rdlangtext="English (default)"
	while read line
	do
		if [ "${line:0:2}" = "$rdlang" ]; then
		rdlangtext=${line:3}
		break
		fi
	done < "initrmfs/tbu/utility/lang-versions"
	rm -r lang-tmp 2> /dev/null
	mkdir lang-tmp
	unzip -q -d lang-tmp ifl_$lang$langfilefinish
	echo "Boot Media language version is $langtext"
	if [ -e lang-tmp/imagel ]; then
		echo "Copying $langtext version of imagel from ifl_$lang$langfilefinish to root filesystem"
		# if unregistered version, save English version for restore disc in case that uses English
		if [ "$reg" = "0" ]; then
			cp initrmfs/tbu/imagel initrmfs/tbu/imagelrd
		fi
		cp lang-tmp/imagel initrmfs/tbu/imagel
		chown 0:0 initrmfs/tbu/imagel
		chmod +x initrmfs/tbu/imagel
	else
		echo "$langtext version of imagel not found in ifl_$lang$langfilefinish"
		echo "The English (default) version of imagel will be used"
	fi
	echo "Restore Disc language version is $rdlangtext"
	if [ "$rdlang" != "en" ]; then
		rm -r lang-tmp 2> /dev/null
		mkdir lang-tmp
		unzip -q -d lang-tmp ifl_$rdlang$langfilefinish
		if [ -e lang-tmp/tbirest ]; then
			echo "Copying $rdlangtext version of tbirest from ifl_$rdlang$langfilefinish to root filesystem"
			cp lang-tmp/tbirest initrmfs/tbu/tbirest
			chown 0:0 initrmfs/tbu/tbirest
			chmod +x initrmfs/tbu/tbirest
		elif [ "$reg" = "1" ]; then
			echo "$rdlangtext version of tbirest not found in ifl_$rdlang$langfilefinish"
			echo "The English (default) version of tbirest will be used"
		elif [ -e lang-tmp/imagel ]; then
			echo "Copying $rdlangtext version of imagel from ifl_$rdlang$langfilefinish to root filesystem"
			cp lang-tmp/imagel initrmfs/tbu/imagelrd
			chown 0:0 initrmfs/tbu/imagelrd
			chmod +x initrmfs/tbu/imagelrd
		else
			echo "$rdlangtext version of imagel not found in ifl_$rdlang$langfilefinish"
			echo "The English (default) version of imagel will be used"
		fi
	fi
elif [ -e $settingspath/rdlang ]; then
	echo "Boot Media language version is English (default)"
	rdlang=`cat $settingspath/rdlang`
	rdlangtext="English (default)"
	while read line
	do
		if [ "${line:0:2}" = "$rdlang" ]; then
		rdlangtext=${line:3}
		break
		fi
	done < "initrmfs/tbu/utility/lang-versions"
	echo "Restore Disc language version is $rdlangtext"
	if [ "$rdlang" != "en" ]; then
		rm -r lang-tmp 2> /dev/null
		mkdir lang-tmp
		unzip -q -d lang-tmp ifl_$rdlang$langfilefinish
		if [ -e lang-tmp/tbirest ]; then
			echo "Copying $rdlangtext version of tbirest from ifl_$rdlang$langfilefinish to root filesystem"
		cp lang-tmp/tbirest initrmfs/tbu/tbirest
		chown 0:0 initrmfs/tbu/tbirest
		chmod +x initrmfs/tbu/tbirest
		elif [ "$reg" = "1" ]; then
		echo "$rdlangtext version of tbirest not found in ifl_$rdlang$langfilefinish"
		echo "The English (default) version of tbirest will be used"
		elif [ -e lang-tmp/imagel ]; then
		echo "Copying $rdlangtext version of imagel from ifl_$rdlang$langfilefinish to root filesystem"
		cp lang-tmp/imagel initrmfs/tbu/imagelrd
		chown 0:0 initrmfs/tbu/imagelrd
		chmod +x initrmfs/tbu/imagelrd
		else
		echo "$rdlangtext version of imagel not found in ifl_$rdlang$langfilefinish"
		echo "The English (default) version of imagel will be used"
		fi
	fi
else
	echo "Boot Media language version is English (default)"
	echo "Restore Disc language version is English (default)"
fi
rm -r lang-tmp 2> /dev/null

# run getty on one or both serial ports, based on config.txt
if [ "$SERIAL_PORTS" = "1" ]; then
	echo "ttyS0::respawn:-/sbin/getty -L ttyS0 9600 vt100" >> initrmfs/etc/inittab
elif [ "$SERIAL_PORTS" = "2" ]; then
	echo "ttyS1::respawn:-/sbin/getty -L ttyS1 9600 vt100" >> initrmfs/etc/inittab
elif [ "$SERIAL_PORTS" = "B" ]; then
	echo "ttyS0::respawn:-/sbin/getty -L ttyS0 9600 vt100" >> initrmfs/etc/inittab
	echo "ttyS1::respawn:-/sbin/getty -L ttyS1 9600 vt100" >> initrmfs/etc/inittab
fi


if [ "$netsupport" = "1" ];then
	isofilename=iflnet-custom.iso
	isolabel=iflnet
else
	isofilename=ifl-custom.iso
	isolabel=ifl
fi
# delete existing file now to free some RAM (done mostly for building from running disk)
rm $isofilename 2> /dev/null


# recompress the root file system
rm initrmfs/tmp/* 2> /dev/null
cd initrmfs
chown -R 0:0 tbu/*
echo "Recompressing the root filesystem...please wait"
find . -print | $cpiopath -o -a -H newc > ../cdroot/initrmfs
cd ..
gzip -9 cdroot/initrmfs
#rm -r initrmfs 2> /dev/null

echo  "Creating $isofilename"
mv cdroot initrmfs
chroot initrmfs /usr/bin/genisoimage -quiet -V $isolabel -l -R -r -J -o $isofilename -b isolinux.bin -c boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table -eltorito-alt-boot -efi-boot boot/grub/efi.img -no-emul-boot cdroot/
mv initrmfs/$isofilename $isofilename
rm -r cdroot 2> /dev/null
rm -r initrmfs 2> /dev/null

# chown all files and directories to owner:group
# as precaution make sure running from IFL dir, otherwise don't do it
if [ "$0" = "./makeISO" ]; then
	chown -R $ownerid:$groupid util 2> /dev/null
	chown -R $ownerid:$groupid help 2> /dev/null
	chown -R $ownerid:$groupid wireless 2> /dev/null
	chown -R $ownerid:$groupid scripts 2> /dev/null
	chown -R $ownerid:$groupid iscsi 2> /dev/null
	chown -R $ownerid:$groupid cdboot 2> /dev/null
	chown -R $ownerid:$groupid pxe 2> /dev/null
	chown -R $ownerid:$groupid grub 2> /dev/null
	chown -R $ownerid:$groupid tbosdtfiles 2> /dev/null
	chown $ownerid:$groupid *
fi

echo
echo "Build of $isofilename is complete"
echo "The file $isofilename can now be burned to CD/DVD"
echo
