#! /bin/bash

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 [ "$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
	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
    # 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
    	    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
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
    ./util/tbosdt getiflpxefiles.run
    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
    ./util/tbosdt getiflnetpxefiles.run
    rm getiflnetpxefiles.run
fi

# get the Product Key from isolinux.cfg before deleting it below
ProductKeySearch=`grep -o -m1 ProductKey=.............. pxe/isolinux.cfg`
CurrentProductKey=`echo ${ProductKeySearch:11}`
ProductKeyText="ProductKey=$CurrentProductKey"

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.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 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
# video
if [ -f $settingspath/vmode ]; then
    defaultitem=`cat $settingspath/vmode`
elif [ -f initrmfs/tbu/utility/x64 ]; then
    defaultitem=2
else
    defaultitem=1
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 
    echo "timeout 50" > pxe/pxelinux.cfg/default
    echo "kbdmap $ktl.ktl" >> pxe/pxelinux.cfg/default
    echo "prompt 1" >> pxe/pxelinux.cfg/default
    echo "display menu.txt" >> pxe/pxelinux.cfg/default
    echo "default $defaultitem" >> pxe/pxelinux.cfg/default
    echo "label 1" >> pxe/pxelinux.cfg/default
    echo " kernel vmlinuz" >> pxe/pxelinux.cfg/default
    echo " append vga=0 iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0 $ProductKeyText $bootparms" >> pxe/pxelinux.cfg/default
    echo "label 2" >> pxe/pxelinux.cfg/default
    echo " kernel vmlinuz" >> pxe/pxelinux.cfg/default
    echo " append vga=773 iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0 $ProductKeyText $bootparms" >> pxe/pxelinux.cfg/default
    echo "label 3" >> pxe/pxelinux.cfg/default
    echo " kernel vmlinuz" >> pxe/pxelinux.cfg/default
    echo " append vga=771 iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0 $ProductKeyText $bootparms" >> pxe/pxelinux.cfg/default
    echo "label 4" >> pxe/pxelinux.cfg/default
    echo " kernel vmlinuz" >> pxe/pxelinux.cfg/default
    echo " append vga=769 iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0 $ProductKeyText $bootparms" >> pxe/pxelinux.cfg/default
    echo "label 5" >> pxe/pxelinux.cfg/default
    echo " kernel vmlinuz" >> pxe/pxelinux.cfg/default
    echo " append vga=ask iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0 $ProductKeyText $bootparms" >> pxe/pxelinux.cfg/default
    echo "label 4gh" >> pxe/pxelinux.cfg/default
    echo " kernel vmlinuz" >> pxe/pxelinux.cfg/default
    echo " append vga=0 mem=2G iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0 $ProductKeyText $bootparms" >> pxe/pxelinux.cfg/default

    # 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 "" > pxe/menu.txt
    echo $boottitle >> pxe/menu.txt
    echo "" >> pxe/menu.txt
    echo "Boot Menu:" >> pxe/menu.txt
    echo "<1>  80x25 text mode" >> pxe/menu.txt
    echo "<2>  1024x768 frame buffer mode" >> pxe/menu.txt
    echo "<3>  800x600 frame buffer mode" >> pxe/menu.txt
    echo "<4>  640x480 frame buffer mode" >> pxe/menu.txt
    echo "<5>  Select alternate video mode during boot" >> pxe/menu.txt
    echo "" >> pxe/menu.txt
    echo "Booting default <$defaultitem> in 5 seconds. Press <Enter> to boot now." >> pxe/menu.txt
    echo "" >> pxe/menu.txt
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" > pxe/pxelinux.cfg/default
    echo "kbdmap $ktl.ktl" >> pxe/pxelinux.cfg/default
    echo "prompt 1" >> pxe/pxelinux.cfg/default
    echo "display menu.txt" >> pxe/pxelinux.cfg/default
    echo "default $defaultitem" >> pxe/pxelinux.cfg/default
    echo "label 1" >> pxe/pxelinux.cfg/default
    echo " kernel vmlinuz" >> pxe/pxelinux.cfg/default
    echo " append vga=794 switchx=yes iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0 $ProductKeyText $bootparms" >> pxe/pxelinux.cfg/default
    echo "label 2" >> pxe/pxelinux.cfg/default
    echo " kernel vmlinuz" >> pxe/pxelinux.cfg/default
    echo " append vga=791 switchx=yes iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0 $ProductKeyText $bootparms" >> pxe/pxelinux.cfg/default
    echo "label 3" >> pxe/pxelinux.cfg/default
    echo " kernel vmlinuz" >> pxe/pxelinux.cfg/default
    echo " append vga=788 switchx=yes iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0 $ProductKeyText $bootparms" >> pxe/pxelinux.cfg/default
    echo "label 4" >> pxe/pxelinux.cfg/default
    echo " kernel vmlinuz" >> pxe/pxelinux.cfg/default
    echo " append vga=785 switchx=yes iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0 $ProductKeyText $bootparms" >> pxe/pxelinux.cfg/default
    echo "label 4gh" >> pxe/pxelinux.cfg/default
    echo " kernel vmlinuz" >> pxe/pxelinux.cfg/default
    echo " append vga=791 switchx=yes mem=2G iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0 $ProductKeyText $bootparms" >> pxe/pxelinux.cfg/default

    # 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 "" > pxe/menu.txt
    echo $boottitle >> pxe/menu.txt
    echo "" >> pxe/menu.txt
    echo "Boot Menu:" >> pxe/menu.txt
    echo "<1>  1280x1024 frame buffer mode" >> pxe/menu.txt
    echo "<2>  1024x768 frame buffer mode" >> pxe/menu.txt
    echo "<3>  800x600 frame buffer mode" >> pxe/menu.txt
    echo "<4>  640x480 frame buffer mode" >> pxe/menu.txt
    echo "" >> pxe/menu.txt
    echo "For systems that fail to start the GUI after boot, please try" >> pxe/menu.txt
    echo "another video mode from the list above (e.g. boot: 3)." >> pxe/menu.txt
    echo "" >> pxe/menu.txt
    echo "Booting default <$defaultitem> in 5 seconds. Press <Enter> to boot now." >> pxe/menu.txt
    echo "" >> pxe/menu.txt
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" > pxe/pxelinux.cfg/default
    echo "kbdmap $ktl.ktl" >> pxe/pxelinux.cfg/default
    echo "prompt 1" >> pxe/pxelinux.cfg/default
    echo "display menu.txt" >> pxe/pxelinux.cfg/default
    echo "default $defaultitem" >> pxe/pxelinux.cfg/default
    echo "label 1" >> pxe/pxelinux.cfg/default
    echo " kernel vmlinuz" >> pxe/pxelinux.cfg/default
    echo " append vga=773 iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0 $ProductKeyText $bootparms" >> pxe/pxelinux.cfg/default
    echo "label 2" >> pxe/pxelinux.cfg/default
    echo " kernel vmlinuz" >> pxe/pxelinux.cfg/default
    echo " append vga=773 xres=1024 iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0 $ProductKeyText $bootparms" >> pxe/pxelinux.cfg/default
    echo "label 3" >> pxe/pxelinux.cfg/default
    echo " kernel vmlinuz" >> pxe/pxelinux.cfg/default
    echo " append vga=773 xres=800 iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0 $ProductKeyText $bootparms" >> pxe/pxelinux.cfg/default
    echo "label 4" >> pxe/pxelinux.cfg/default
    echo " kernel vmlinuz" >> pxe/pxelinux.cfg/default
    echo " append vga=773 xres=640 iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0 $ProductKeyText $bootparms" >> pxe/pxelinux.cfg/default
    echo "label 1fb" >> pxe/pxelinux.cfg/default
    echo " kernel vmlinuz" >> pxe/pxelinux.cfg/default
    echo " append vga=794 switchx=yes iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0 $ProductKeyText $bootparms" >> pxe/pxelinux.cfg/default
    echo "label 2fb" >> pxe/pxelinux.cfg/default
    echo " kernel vmlinuz" >> pxe/pxelinux.cfg/default
    echo " append vga=791 switchx=yes iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0 $ProductKeyText $bootparms" >> pxe/pxelinux.cfg/default
    echo "label 3fb" >> pxe/pxelinux.cfg/default
    echo " kernel vmlinuz" >> pxe/pxelinux.cfg/default
    echo " append vga=788 switchx=yes iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0 $ProductKeyText $bootparms" >> pxe/pxelinux.cfg/default
    echo "label 4fb" >> pxe/pxelinux.cfg/default
    echo " kernel vmlinuz" >> pxe/pxelinux.cfg/default
    echo " append vga=785 switchx=yes iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0 $ProductKeyText $bootparms" >> pxe/pxelinux.cfg/default
    echo "label 4gh" >> pxe/pxelinux.cfg/default
    echo " kernel vmlinuz" >> pxe/pxelinux.cfg/default
    echo " append vga=773 mem=2G iflkmap=$kbd ktl=$ktl initrd=initrmfs.gz iflbflag=0 $ProductKeyText $bootparms" >> pxe/pxelinux.cfg/default
    # 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 "" > pxe/menu.txt
    echo $boottitle >> pxe/menu.txt
    echo "" >> pxe/menu.txt
    echo "Boot Menu:" >> pxe/menu.txt
    echo "<1>  Best available graphics mode" >> pxe/menu.txt
    echo "<2>  1024x768 graphics mode" >> pxe/menu.txt
    echo "<3>  800x600 graphics mode" >> pxe/menu.txt
    echo "<4>  640x480 graphics mode" >> pxe/menu.txt
    echo "" >> pxe/menu.txt
    echo "For systems that fail to start the GUI after boot, please use" >> pxe/menu.txt
    echo "frame buffer option 1fb, 2fb, 3fb, or 4fb (e.g. boot: 2fb)." >> pxe/menu.txt
    echo "" >> pxe/menu.txt
    echo "Booting default <$defaultitem> in 5 seconds. Press <Enter> to boot now." >> pxe/menu.txt
    echo "" >> pxe/menu.txt
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
    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
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

# 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
