#! /bin/bash

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

clear
echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
echo "         The makePXE script to create PXE boot files - 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


# parse command line
custom=0;nn=0;synerror=0
if [ "$1" = "/c" ]; then let custom=custom+1
elif [ "$1" = "/nn" ]; then let nn=nn+1
elif [ "$1" != "" ]; then synerror=1
fi
if [ "$2" = "/c" ]; then let custom=custom+1
elif [ "$2" = "/nn" ]; then let nn=nn+1
elif [ "$2" != "" ]; then synerror=1
fi
if [ "$nn" -gt "1" ]; then synerror=1; fi
if [ "$custom" -gt "1" ]; then synerror=1; fi
if [ "$3" != "" ]; then synerror=1; fi

# show usage if command line error
if [ "$synerror" = "1" ]; then
	echo "Command line entered: $0 $1 $2 $3 $4"
	echo
	echo "Usage:  ./makePXE [/c] [/nn]"
	echo "  /c  - create customized version"
	echo "  /nn - create smaller version with no network support"
	echo
	echo "Usage examples:"
	echo "./makePXE         -> create default version"
	echo "./makePXE /c      -> create custom version"
	echo "./makePXE /nn     -> create default version with no network support"
	echo "./makePXE /c /nn  -> create custom version with no network support"
	echo
	echo "Note that without the /c option, makePXE will create the PXE boot files"
	echo "based on the default configuration. This will be the same as iflnet.iso"
	echo "if /nn is not used, or the same as ifl.iso if /nn is used"
	echo
	exit 1
fi

if [ "$nn" = "1" ]; then 
	netsupport=0
	isofilename="ifl.iso"
else
	netsupport=1
	isofilename="iflnet.iso"
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 the IFL directory"
fi
if [ ! -e ifl.iso ]; then
	m=1
	echo "File ifl.iso not found in the IFL directory"
fi

if [ ! -d util ]; then
	m=1
	echo "Directory util not found in the IFL directory"
fi

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 ./makePXE) or using 'su -' to"
		echo "open a login shell, then changing to the script's folder and"
		echo "running it."
	fi
fi

if [ "$custom" = "1" ]; then
	# 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
	# check if cpio will execute (64 bit systems)
	# if not use distro version if it's there
	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 [ ! -e config.txt ]; then
		m=1
		echo "Required file config.txt not found in the IFL directory"
	fi
	# check for zcat
	zcatpath=`which zcat 2> /dev/null`
	if [ ! "$?" = "0" ]; then
		m=1
		echo "Required program zcat (typically /bin/zcat) not found on system."
	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 / create working directories
echo "Cleaning up any previous contents of working directories"
for workingdir in initrmfs pxe
do
	rm -r $workingdir 2> /dev/null
done
mkdir pxe

# copy files from util directory
cp util/pxelinux.0 pxe
mkdir -p pxe/efi32 && cp -r util/efi32/* pxe/efi32
mkdir -p pxe/efi64 && cp -r util/efi64/* pxe/efi64

# 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

cp -r util/pxelinux.cfg pxe
if [ "$nn" = "1" ]; then
	echo "mount 0: ifl.iso" > getiflpxefiles.run
	echo "copy 0: pxe -s -q" >> getiflpxefiles.run
	echo "copy pxe/isolinux.cfg pxe/pxelinux.cfg/default -q" >> getiflpxefiles.run
	${tbosdt} getiflpxefiles.run 2> /dev/null
	rm getiflpxefiles.run
else
	echo "mount 0: iflnet.iso" > getiflnetpxefiles.run
	echo "copy 0: pxe -s -q" >> getiflnetpxefiles.run
	echo "copy pxe/isolinux.cfg pxe/pxelinux.cfg/default -q" >> getiflnetpxefiles.run
	${tbosdt} getiflnetpxefiles.run 2> /dev/null
	rm getiflnetpxefiles.run
fi

for filename in isolinux.cfg isolinux.bin boot.cat
do
	rm pxe/$filename 2> /dev/null
done
for directoryname in boot disk efi
do
	rm -r pxe/$directoryname 2> /dev/null
done

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

	# decompress and extract root file system so that changes can be made
	echo "Decompressing and extracting the root filesystem"
	mkdir initrmfs
	cd initrmfs
	$zcatpath ../pxe/initrmfs.gz | $cpiopath -i -m -d
	cd ..
	rm pxe/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

	# set up settings directory if needed
	if [ "$netsupport" = "1" ]; then
		if [ ! -d util/settings ]; then mkdir util/settings; fi
		if [ ! -d util/settings/network ]; then mkdir util/settings/network; fi
		settingspath="util/settings"
	else
		if [ ! -d util/settings ]; then mkdir util/settings; fi
		settingspath="util/settings"
	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 boot iflscript lang lvm md rdiflkmap rdktl rdlang rdvga rdxres 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 pxe/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/" > pxe/pxelinux.cfg/default
		cat pxe/menu.txt-default | sed -e "s/default <.*> in/default <$defaultitem> in/" > pxe/menu.txt
	else
		cat pxe/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/" > pxe/pxelinux.cfg/default
		cat pxe/menu.txt-default | sed -e "s/default <.*> in/default <$defaultitem> in/" | sed -e "s/Network Boot/Boot/" > pxe/menu.txt
	fi


	# For UEFI mode
	if [ "$netsupport" = "1" ]; then
		cat pxe/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/" > pxe/boot/grub/grub.cfg
	else
		cat pxe/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/" > pxe/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/" pxe/pxelinux.cfg/default
			sed -i "/^\s*linux/ s/\(.*\s\)\(\S\+\)\(\s*\)$/\1console=ttyS0,${SERIAL_BAUD} \2\3/" pxe/boot/grub/grub.cfg
		fi
	fi

	clear
	echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
	echo "         The makePXE script to create PXE boot files - 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 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

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

# chown all files and directories to owner:group
# as precaution make sure running from IFL dir, otherwise don't do it
if [ "$0" = "./makePXE" ]; 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

# ensure correct permissions on pxe contents
chmod -R 644 pxe/*
chmod 755 pxe/pxelinux.cfg

echo
echo "The build of the PXE boot files is now complete"
echo "The files required for PXE boot can be found in the pxe directory"
echo
