#! /bin/bash

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

# get video modes array and save/load paths
. /tbu/utility/video-modes.inc
. /tbu/utility/video-modes-uefi.inc
# load support functions
. /tbu/utility/video-functions.inc

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

# check command line
eficadir=
for param in $1;
do
	if [ "$param" = "/msca2011" ]; then 
		eficadir=MicrosoftCA2011
	elif [ "$param" = "/msca2023" ]; then 
		eficadir=MicrosoftCA2023
	elif [ "$param" = "/tbca2026" ]; then 
		eficadir=TeraByte
	elif [ "$param" != "" ]; then
		echo "Usage:  ./makeISO [msca2011|msca2023|tbca2026]"
		echo "  /msca2011 - UEFI boot using MS 2011 CA"
		echo "  /msca2023 - UEFI boot using MS 2023 CA"
		echo "  /tbca2026 - UEFI boot using TeraByte 2026 CA"
		echo
		echo "Invalid Parameter: $param"
		echo
		exit 1
	fi
done

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
		read -r cpioversion < <($cpiopath --version)
		echo "Using cpio version $cpioversion"
		break
	fi
done

if [ ! `which chroot 2> /dev/null` ]; then
	# add sbin paths to PATH and recheck
	PATH=$PATH:/usr/sbin:/sbin
	if [ ! `which chroot 2> /dev/null` ]; then
		m=1
		echo "Required program chroot not found on system. This can usually be"
		echo "corrected by making sure /usr/sbin and /sbin are included in the"
		echo "environment PATH variable for the root user. Alternatives include"
		echo "running script with 'sudo' (e.g. sudo ./makeISO) or using 'su -' to"
		echo "open a login shell, then changing to the script's folder and"
		echo "running it."
	fi
fi

# 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
	enlist=( "ifl_en*.zip" )
	encount=${#enlist[@]}
	zipcheck=1
	if [ "$nlangfiles" = "1" ] && [ "$encount" != "0" ]; 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

# check if x64
uname -a | grep -i "x86_64" > /dev/null
if [ "$?" = "0" ]; then
	isx64=1
else
	isx64=0
fi

if [ "$isx64" = "1" ] && [ -f "./x64/tbosdt64" ]; then
	tbosdt="./x64/tbosdt64"
else
	tbosdt="./util/tbosdt"
fi

# copy files from iflnet.iso to cdroot
echo "mount 0: iflnet.iso" > getiflnetfiles.run
echo "copy 0: cdroot -s -q" >> getiflnetfiles.run
${tbosdt} getiflnetfiles.run 2> /dev/null
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
	${tbosdt} getiflfiles.run 2> /dev/null
	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_v4.zip"; else langfilefinish="_gui_trial.zip"; fi
elif [ "$gui" = "0" ]; then
	if [ "$reg" = "1" ]; then langfilefinish="_v4.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

# get video modes array and save/load paths
. initrmfs/tbu/utility/video-modes.inc
. initrmfs/tbu/utility/video-modes-uefi.inc
# load support functions
. initrmfs/tbu/utility/video-functions.inc

# video
	# setup for use of functions
	videomode_functions_init "0" "${settingspath}/vmode"
	# load in current config 
	videomode_loadconfig "custom"
	# get video mode selection
	videomode_extractselection
	defaultitem="${vid_vmodesel}"
	
# UEFI video

	# setup for use of functions
	videomode_functions_init "1" "${settingspath}/vmodeuefi"
	# load in current config 
	videomode_loadconfig "custom"
	# get video mode selection
	videomode_extractselection
	vmodeuefi="${vid_vmodesel}"

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

# for non-UEFI mode
if [ "$netsupport" = "1" ]; then
	cat cdroot/isolinux.cfg-default | sed -e "s/kbdmap qwerty.ktl/kbdmap $ktl.ktl/" | sed -e "s/iflkmap=us ktl=qwerty/iflkmap=$kbd ktl=$ktl/" | sed -e "s/cdboot=yes/cdboot=$cdboot/" | sed -e "s/iflbflag=0/iflbflag=0 $bootparms/" | sed -e "s/default .*$/default $defaultitem/" > cdroot/isolinux.cfg
	cat cdroot/menu.txt-default | sed -e "s/default <.*> in/default <$defaultitem> in/" > cdroot/menu.txt
else
	cat cdroot/isolinux.cfg-default | sed -e "s/kbdmap qwerty.ktl/kbdmap $ktl.ktl/" | sed -e "s/iflkmap=us ktl=qwerty/iflkmap=$kbd ktl=$ktl/" | sed -e "s/cdboot=yes/cdboot=$cdboot/" | sed -e "s/iflbflag=0/iflbflag=0 $bootparms/" | sed -e "s/default .*$/default $defaultitem/" > cdroot/isolinux.cfg
	cat cdroot/menu.txt-default | sed -e "s/default <.*> in/default <$defaultitem> in/" | sed -e "s/Network Boot/Boot/" > cdroot/menu.txt
fi


# 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/cdboot=yes/cdboot=$cdboot/" | sed -e "s/iflbflag=0/iflbflag=0 $bootparms/" | sed -e "s/default=[0-9]\+/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/cdboot=yes/cdboot=$cdboot/" | sed -e "s/iflbflag=0/iflbflag=0 $bootparms/" | sed -e "s/default=[0-9]\+/default=$vmodeuefi/" > cdroot/boot/grub/grub.cfg
fi

if [ ! "$SERIAL_PORTS" = "" ]; then
	if [ "$SERIAL_CONSOLE" = "Y" ]; then
		sed -i "/^\s*append/ s/\(.*\s\)\(\S\+\)\(\s*\)$/\1console=ttyS0,${SERIAL_BAUD} \2\3/" cdroot/isolinux.cfg
		sed -i "/^\s*linux/ s/\(.*\s\)\(\S\+\)\(\s*\)$/\1console=ttyS0,${SERIAL_BAUD} \2\3/" 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

if [ "$eficadir" = "" ]; then
	while [ 0 ]; do
		echo "Select the Certificate Type for UEFI Secure Boot."
		echo " 0) Use current default"
		echo " 1) Microsoft CA 2011"
		echo " 2) Microsoft CA 2023"
		echo " 3) TeraByte"
		read -r -p "Select 0-3: " choice
		case "$choice" in
			0)
				break
				;;
			1)
				eficadir=MicrosoftCA2011
				break
				;;
			2)
				eficadir=MicrosoftCA2023
				break
				;;
			3)
				eficadir=TeraByte
				break
				;;
			*)
				echo "Invalid selection. Enter 0, 1, 2, or 3."
				echo
				;;
		esac
	done
	echo
fi

if [ "$eficadir" != "" ]; then
	if [ ! -f "cdroot/sbca/$eficadir/bootx64.efi" ]; then
		echo "NOTE: Source appears old; it uses the Microsoft Corporation UEFI CA 2011."
	else
		cp cdroot/sbca/$eficadir/bootx64.efi cdroot/efi/boot
		cp cdroot/sbca/$eficadir/efi.img cdroot/boot/grub
		cp cdroot/boot/grub/efi.img initrmfs/tbu/cdboot/boot/grub
	fi
fi

# 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 network/firmware ] && [ "$netsupport" = "1" ]; then
	echo "Copying network/firmware directory to the root filesystem"
	cp -a network/firmware/* initrmfs/lib/firmware 2> /dev/null
	chown -R 0:0 initrmfs/lib/firmware/
fi

if [ -d firmware ]; then
	echo "Copying firmware directory to the root filesystem"
	cp -a 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
	if [ "$BUILDCDBOOT" = "N" ]; then
		echo "Restore Disc is diabled"
	else
		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
	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"
	if [ "$BUILDCDBOOT" = "N" ]; then
		echo "Restore Disc is diabled"
	else
		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
	fi
else
	echo "Boot Media language version is English (default)"
	if [ "$BUILDCDBOOT" = "N" ]; then
		echo "Restore Disc is diabled"
	else
		echo "Restore Disc language version is English (default)"
	fi
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 $SERIAL_BAUD vt100" >> initrmfs/etc/inittab
elif [ "$SERIAL_PORTS" = "2" ]; then
	echo "ttyS1::respawn:-/sbin/getty -L ttyS1 $SERIAL_BAUD vt100" >> initrmfs/etc/inittab
elif [ "$SERIAL_PORTS" = "B" ]; then
	echo "ttyS0::respawn:-/sbin/getty -L ttyS0 $SERIAL_BAUD vt100" >> initrmfs/etc/inittab
	echo "ttyS1::respawn:-/sbin/getty -L ttyS1 $SERIAL_BAUD 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
