#! /bin/bash

startup_initialize() {
    clear
    echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
    echo "         The makeGRUB script to create GRUB 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

    # set up initial values of variables
    netsupport=1
    netsupporttext="Network"
    isofilename="iflnet.iso"
    grubmenutitledefault="IFL Boot Disk"
    grubmenutitletextdefault="IFL Boot Disk (default)"
    if [ -f util/settings/grub/grubmenutitle ]; then
	grubmenutitle=`cat util/settings/grub/grubmenutitle`
	grubmenutitletext=$grubmenutitle
    else
	grubmenutitle=$grubmenutitledefault
	grubmenutitletext=$grubmenutitletextdefault
    fi
    get_grubversion
    if [ "$grubversion" = "2" ]; then
	grubversiontext="Grub2"
	echo "grubcfgpath = $grubcfgpath"
	get_biosuefi
	echo "biosuefi = $biosuefi, efipart = $efipart"
	get_grub_partition
	if [ "$?" = "0" ]; then
	    echo "grubpart = $grubpart"
	    echo "iflgrubpath = $iflgrubpath, iflgrubnormalpath = $iflgrubnormalpath"
	else
	    $dialogpath --no-shadow --title "Grub Block Device Not Supported" --msgbox "\nThe grub directory (i.e. /boot/grub or /boot/grub2) must be located on a standard partition such as /dev/sda1, /dev/sda2, etc, and be formatted as ext2/3/4. Other block device types (such as RAID or LVM drives), and other file systems are not supported at this time.\n\n" 11 75 2> /dev/null
	    clear
	    exit 1
	fi
    elif [ "$grubversion" = "1" ]; then
	grubversiontext="Grub Legacy"
	echo "grubcfgpath = $grubcfgpath"
	get_biosuefi
	echo "biosuefi = $biosuefi, efipart = $efipart"
	get_grub_partition
	if [ "$?" = "0" ]; then
	    echo "grubpart = $grubpart"
	    echo "iflgrubpath = $iflgrubpath, iflgrubnormalpath = $iflgrubnormalpath"
	else
	    $dialogpath --no-shadow --title "Grub Block Device Not Supported" --msgbox "\nThe grub directory (i.e. /boot/grub) must be located on a standard partition such as /dev/sda1, /dev/sda2, etc, and be formatted as ext2/3/4. Other block device types (such as RAID or LVM drives), and other file systems are not supported at this time.\n\n" 10 75 2> /dev/null
	    clear
	    exit 1
	fi
	for drive in a b c d e f g h i j k l m n o p q r s t u v w x y z
	do
	    if [ "$drive" = "a" ]; then x=0; else letx=x+1;fi
	    if [ "${grubpart:0:8}" = "/dev/sd$drive" ]; then
		grub1root="root      (hd$x,"
		break
	    fi
	done
	for y in `seq 1 128`
	do
	    if [ "$grubpart" = "/dev/sd$drive$y" ]; then
		let y=y-1
		grub1root="$grub1root$y)"
		break
	    fi
	done
	echo "grub1root = $grub1root"
    elif [ "$grubversion" = "fedorauefi" ]; then
	$dialogpath --no-shadow --title "Fedora UEFI Not Supported" --msgbox "\nFedora UEFI and its' derivatives are currently not supported by makeGRUB. The Fedora distribution is supported on BIOS systems.\n\n" 8 70 2> /dev/null
	clear
	exit 1
    elif [ "$grubversion" = "0" ]; then
	$dialogpath --no-shadow --title "Grub Version/Configuration Not Identified" --msgbox "\nThe Grub bootloader is either not installed, or its' configuration could not be identified. The makeGRUB script does not support this distribution as currently configured.\n\n" 9 70 2> /dev/null
	clear
	exit 1
    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

    # 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 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
    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

    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
}

main_menu_grub2() {
    $dialogpath --no-shadow --title  "makeGRUB - Main Menu Grub2" --menu "\nSelect 'Create Grub2 boot files for IFL' to create the required files, then select 'Install Grub2 boot files for IFL' to add IFL to the Grub menu. Repeat as needed to change any IFL settings. Once Grub files are installed, the 'Schedule IFL cron jobs' item can be used to create cron jobs to automatically reboot into IFL.\n\nSelect 'Uninstall Grub2 boot files for IFL' at any time to remove IFL from the Grub menu, and to delete all of the installed files.\n\nMore info: http://www.terabyteunlimited.com/kb/article.php?id=586\n \n" 24 75 5\
    "Exit Menu" "" \
    "Create Grub2 boot files for IFL" "" \
    "Install Grub2 boot files for IFL" "" \
    "Schedule IFL cron jobs" "" \
    "Uninstall Grub2 boot files for IFL" "" 2> /tmp/GRUBmselect
    if [ ! "$?" = "0" ]; then
        rm -f /tmp/GRUBmselect 2> /dev/null
	GRUBmselect=""
    else
	GRUBmselect=`cat /tmp/GRUBmselect`
	rm /tmp/GRUBmselect 2> /dev/null
    fi
}

main_menu_grub1() {
    $dialogpath --no-shadow --title  "makeGRUB - Main Menu Grub Legacy" --menu "\nSelect 'Create Grub Legacy boot files for IFL' to create the required files, then select 'Install Grub Legacy boot files for IFL' to add IFL to the Grub menu. Repeat as needed to change any IFL settings. Once Grub files are installed, the 'Schedule IFL cron jobs' item can be used to create cron jobs to automatically reboot into IFL.\n\nSelect 'Uninstall Grub Legacy boot files for IFL' at any time to remove IFL from the Grub menu, and to delete all of the installed files.\n\nMore info: http://www.terabyteunlimited.com/kb/article.php?id=586\n \n" 24 75 5\
    "Exit Menu" "" \
    "Create Grub Legacy boot files for IFL" "" \
    "Install Grub Legacy boot files for IFL" "" \
    "Schedule IFL cron jobs" "" \
    "Uninstall Grub Legacy boot files for IFL" "" 2> /tmp/GRUBmselect
    if [ ! "$?" = "0" ]; then
        rm -f /tmp/GRUBmselect 2> /dev/null
	GRUBmselect=""
    else
	GRUBmselect=`cat /tmp/GRUBmselect`
	rm /tmp/GRUBmselect 2> /dev/null
    fi
}

boot_file_config_menu() {
    while [ 0 ]; do
    if [ "$grubversion" = "2" ]; then
	$dialogpath --no-shadow --title  "Grub Boot Files Configuration Items" --menu "\nGrub Version:        $grubversiontext\nIFL Grub Menu Title: $grubmenutitletext\nNetwork/Non-network: $netsupporttext\nBIOS/UEFI:           $biosuefi\n" 15 75 3\
	"Continue To IFL Settings Menu" "" \
	"Modify IFL Grub menu title" "" \
	"Network or non-network (toggle)" "" 2> /tmp/GRUBmselect
	if [ ! "$?" = "0" ]; then
    	    rm -f /tmp/GRUBmselect 2> /dev/null
	    GRUBmselect=""
	    return 1
	else
	    GRUBmselect=`cat /tmp/GRUBmselect`
	    rm /tmp/GRUBmselect 2> /dev/null
	fi
    else
	$dialogpath --no-shadow --title  "Grub Boot Files Configuration Item" --menu "\nGrub Version:        $grubversiontext\nIFL Grub Menu Title: $grubmenutitletext\nNetwork/Non-network: $netsupporttext\nBIOS/UEFI:           $biosuefi\n" 15 75 3\
	"Continue To IFL Settings Menu" "" \
	"Modify IFL Grub menu title" "" \
	"Network or non-network (toggle)" "" 2> /tmp/GRUBmselect
	if [ ! "$?" = "0" ]; then
    	    rm -f /tmp/GRUBmselect 2> /dev/null
	    GRUBmselect=""
	    return 1
	else
	    GRUBmselect=`cat /tmp/GRUBmselect`
	    rm /tmp/GRUBmselect 2> /dev/null
	fi
    fi
    if [ "$GRUBmselect" = "Continue To IFL Settings Menu" ]; then
	if [ "$grubmenutitle" = "$grubmenutitledefault" ]; then
	    rm util/settings/grub/grubmenutitle 2> /dev/null
	else
	    echo "$grubmenutitle" > util/settings/grub/grubmenutitle
	fi
	return 0
    elif [ "$GRUBmselect" = "Network or non-network (toggle)" ]; then
	if [ "$netsupport" = "1" ]; then
	    netsupport=0
	    netsupporttext="Non-network"
	    isofilename=ifl.iso
	else
	    netsupport=1
	    netsupporttext="Network"
	    isofilename=iflnet.iso
	fi
    elif [ "$GRUBmselect" = "Modify IFL Grub menu title" ]; then
	while [ 0 ]; do
    	    $dialogpath --no-shadow --max-input 80 --title "Modify IFL Grub Menu Title" --inputbox "\nModify the IFL Grub menu title below as needed, then press <Enter>. This will be the title of the IFL item on the Grub menu." 11 75  "$grubmenutitle" 2> /tmp/TITLEselect
	    if [ "$?" = "0" ]; then
		TITLEselect=`cat /tmp/TITLEselect`
		rm /tmp/TITLEselect 2> /dev/null
		if [ "$TITLEselect" != "" ]; then
		    grubmenutitle=$TITLEselect
		    if [ "$grubmenutitle" = "$grubmenutitledefault" ]; then
			grubmenutitletext=$grubmenutitletextdefault
		    else
			grubmenutitletext=$grubmenutitle
		    fi
		    break
		fi
	    else
		break
	    fi
	done
    fi
    done
}

create_boot_files() {
    boot_file_config_menu
    if [ "$?" != "0" ]; then return 1; fi

    clear
    echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
    echo "         The makeGRUB script to create GRUB boot files - TeraByte, Inc"
    echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
    echo

    # clean up any existing grub and cdroot directories, create new ones
    # copy files from util directory
    echo "Deleting any previous contents of grub and cdroot directories, recreating"
    rm -r grub 2> /dev/null
    mkdir grub
    rm -r cdroot 2> /dev/null
    mkdir cdroot

    # copy files from IFL ISO file to cdroot directory using tbosdt script
    echo "mount 0: $isofilename" > getiflfiles.run
    echo "copy 0: cdroot -s -q" >> getiflfiles.run
    ./util/tbosdt getiflfiles.run
    rm getiflfiles.run

    # clean up and recreate initrmfs directory
    # decompress and extract root file system so that changes can be made
    rm -r initrmfs 2> /dev/null
    mkdir initrmfs
    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
	# grubmenutitle="IFL (GUI) Boot Disk"
    else
	# grubmenutitle="IFL (CUI) Boot Disk"
	gui=0
    fi
    # check for x64 flag at /tbu/utility/x64 to determine if x64 version or not
    if [ -f initrmfs/tbu/utility/x64 ]; then 
	x64=1
    else
	x64=0
    fi

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

    # 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
	#if [ ! -d util/settings/ifl ]; then mkdir util/settings/ifl; 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 vmodeuefi boot iflscript lang lvm md rdiflkmap rdktl rdlang rdvga rdxres rdboot rdcmd rdlvm rdmenu rdmenuconfig rdvmode rdvmodeuefi
    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`
    else
	defaultitem=1
    fi
    # boot parameters
    if [ -e $settingspath/boot ]; then
	bootparms=`cat $settingspath/boot`
    else
	bootparms=""
    fi

    clear
    echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
    echo "         The makeGRUB script to create GRUB 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

    # this is now done earlier so list of scripts is available when chrooting into root fs
    #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

    # create /tbu/utility/restore_grubcfg_normal script to restore normal grub.cfg file
    # this script will be executed from /etc/rc.sysinit, if it exists
    if [ "$grubversion" = "2" ] && [ "$grubpart" != "" ]; then
	echo "#!/bin/bash" > initrmfs/tbu/utility/restore_grubcfg_normal
	echo "mount $grubpart /mnt 2> /dev/null" >> initrmfs/tbu/utility/restore_grubcfg_normal
	echo "if [ -f /mnt$iflgrubnormalpath ] && [ -f /mnt$iflgrubrestoreflag ]; then" >> initrmfs/tbu/utility/restore_grubcfg_normal
	echo "    rm /mnt$iflgrubrestoreflag 2> /dev/null" >> initrmfs/tbu/utility/restore_grubcfg_normal
	echo "    cp /mnt$iflgrubnormalpath /mnt$iflgrubpath 2> /dev/null" >> initrmfs/tbu/utility/restore_grubcfg_normal
	echo "fi" >> initrmfs/tbu/utility/restore_grubcfg_normal
	echo "umount /mnt 2> /dev/null"  >> initrmfs/tbu/utility/restore_grubcfg_normal
	chown 0:0 initrmfs/tbu/utility/restore_grubcfg_normal
	chmod +x initrmfs/tbu/utility/restore_grubcfg_normal
    elif [ "$grubversion" = "1" ] && [ "$grubpart" != "" ]; then
	echo "#!/bin/bash" > initrmfs/tbu/utility/restore_grubcfg_normal
	echo "mount $grubpart /mnt 2> /dev/null" >> initrmfs/tbu/utility/restore_grubcfg_normal
	echo "if [ -f /mnt$iflgrubnormalpath ] && [ -f /mnt$iflgrubrestoreflag ]; then" >> initrmfs/tbu/utility/restore_grubcfg_normal
	echo "    rm /mnt$iflgrubrestoreflag 2> /dev/null" >> initrmfs/tbu/utility/restore_grubcfg_normal
	if [ "$grubcfgpath" = "/boot/efi/EFI/redhat/grub.conf" ] && [ "$efipart" != "" ]; then
	echo "    mount $efipart /cdrom 2> /dev/null" >> initrmfs/tbu/utility/restore_grubcfg_normal
	echo "    cp /mnt$iflgrubnormalpath /cdrom$iflgrubpath 2> /dev/null" >> initrmfs/tbu/utility/restore_grubcfg_normal
	echo "    umount /cdrom 2> /dev/null" >> initrmfs/tbu/utility/restore_grubcfg_normal
	else
	echo "    cp /mnt$iflgrubnormalpath /mnt$iflgrubpath 2> /dev/null" >> initrmfs/tbu/utility/restore_grubcfg_normal
	fi
	echo "fi" >> initrmfs/tbu/utility/restore_grubcfg_normal
	echo "umount /mnt 2> /dev/null"  >> initrmfs/tbu/utility/restore_grubcfg_normal
	chown 0:0 initrmfs/tbu/utility/restore_grubcfg_normal
	chmod +x initrmfs/tbu/utility/restore_grubcfg_normal
    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 > ../cdroot/initrmfs
    cd ..
    gzip -9 cdroot/initrmfs

    # copy kernel, initrmfs.gz, and ktl files to the hdd directory
    # menu.txt and boot loader config file are already there
    echo "Copying required files to the grub directory"
    cp cdroot/vmlinuz grub/vmlinuz
    cp cdroot/initrmfs.gz grub/initrmfs.gz

    # clean up temporary directories no longer needed"
    rm -r cdroot 2> /dev/null
    rm -r initrmfs 2> /dev/null

    mountpoint /boot > /dev/null 2>&1
    if [ "$?" = "0" ]; then bootpath=/ifl; else bootpath=/boot/ifl; fi

    if [ -f $settingspath/vmode ]; then
	vmode=`cat $settingspath/vmode`
    else
	vmode=1
    fi
    if [ -f $settingspath/vmodeuefi ]; then
	vmodeuefi=`cat $settingspath/vmodeuefi`
    else
	vmodeuefi=0
    fi

    if [ "$gui" = "1" ] && [ "$x64" = "0" ]; then
	if [ "$biosuefi" = "BIOS" ]; then
	    if [ "$vmode" = "1" ]; then
		vstring="vga=773"
	    elif [ "$vmode" = "2" ]; then
		vstring="vga=773 xres=1024"
	    elif [ "$vmode" = "3" ]; then
		vstring="vga=773 xres=800"
	    elif [ "$vmode" = "4" ]; then
		vstring="vga=773 xres=640"
	    elif [ "$vmode" = "1fb" ]; then
		vstring="vga=794 switchx=yes"
	    elif [ "$vmode" = "2fb" ]; then
		vstring="vga=791 switchx=yes"
	    elif [ "$vmode" = "3fb" ]; then
		vstring="vga=788 switchx=yes"
	    elif [ "$vmode" = "4fb" ]; then
		vstring="vga=785 switchx=yes"
	    elif [ "$vmode" = "4gh" ]; then
		vstring="vga=773 mem=2G"
	    else
		vstring="vga=773"
	    fi
	else
	    if [ "$vmodeuefi" = "0" ]; then
		gfxpayload="1024x768,800x600,640x480"
		vstring="uefimode=yes"
	    elif [ "$vmodeuefi" = "1" ]; then
		gfxpayload="1024x768,800x600,640x480"
		vstring="uefimode=yes xres=1024"
	    elif [ "$vmodeuefi" = "2" ]; then
		gfxpayload="800x600,640x480"
		vstring="uefimode=yes xres=800"
	    elif [ "$vmodeuefi" = "3" ]; then
		gfxpayload="640x480"
		vstring="uefimode=yes xres=640"
	    elif [ "$vmodeuefi" = "4" ]; then
		gfxpayload="1024x768,800x600,640x480"
		vstring="uefimode=yes switchx=yes"
	    else
		gfxpayload="1024x768,800x600,640x480"
		vstring="uefimode=yes"
	    fi
	fi
    elif [ "$gui" = "1" ] && [ "$x64" = "1" ]; then
	if [ "$biosuefi" = "BIOS" ]; then
	    if [ "$vmode" = "1" ]; then
		vstring="vga=794 switchx=yes"
	    elif [ "$vmode" = "2" ]; then
		vstring="vga=791 switchx=yes"
	    elif [ "$vmode" = "3" ]; then
		vstring="vga=788 switchx=yes"
	    elif [ "$vmode" = "4" ]; then
		vstring="vga=785 switchx=yes"
	    elif [ "$vmode" = "4gh" ]; then
		vstring="vga=791 switchx=yes mem=2G"
	    else
		vstring="vga=791 switchx=yes"
	    fi
	else
	    if [ "$vmodeuefi" = "0" ]; then
		gfxpayload="1024x768,800x600,640x480"
		vstring="uefimode=yes switchx=yes"
	    elif [ "$vmodeuefi" = "1" ]; then
		gfxpayload="800x600,640x480"
		vstring="uefimode=yes switchx=yes"
	    elif [ "$vmodeuefi" = "2" ]; then
		gfxpayload="640x480"
		vstring="uefimode=yes switchx=yes"
	    else
		gfxpayload="1024x768,800x600,640x480"
		vstring="uefimode=yes switchx=yes"
	    fi
	fi
    else
	if [ "$biosuefi" = "BIOS" ]; then
	    if [ "$vmode" = "1" ]; then
		vstring="vga=0"
	    elif [ "$vmode" = "2" ]; then
		vstring="vga=773"
	    elif [ "$vmode" = "3" ]; then
		vstring="vga=771"
	    elif [ "$vmode" = "4" ]; then
		vstring="vga=769"
	    elif [ "$vmode" = "4gh" ]; then
		vstring="vga=0"
	    else
		vstring="vga=0"
	    fi
	else
	    if [ "$vmodeuefi" = "0" ]; then
		gfxpayload="1024x768,800x600,640x480"
		vstring="uefimode=yes"
	    elif [ "$vmodeuefi" = "1" ]; then
		gfxpayload="800x600,640x480"
		vstring="uefimode=yes"
	    elif [ "$vmodeuefi" = "2" ]; then
		gfxpayload="640x480"
		vstring="uefimode=yes"
	    else
		gfxpayload="1024x768,800x600,640x480"
		vstring="uefimode=yes"
	    fi
	fi
    fi

    if [ "$grubversion" = "1" ]; then
	# create menu.lst boot stanza file for GRUB
	# echo "" > grub/ifl-menu.lst
	echo "#MARKER: IFL Boot Disk Stanza (do not remove this line)" >> grub/ifl-menu.lst
	echo "title     $grubmenutitle" >> grub/ifl-menu.lst
	echo "$grub1root" >> grub/ifl-menu.lst
	echo "kernel    $bootpath/vmlinuz $vstring iflkmap=$kbd ktl=$ktl iflbflag=0 $ProductKeyText $bootparms" >> grub/ifl-menu.lst
	echo "initrd    $bootpath/initrmfs.gz" >> grub/ifl-menu.lst
    elif [ "$grubversion" = "2" ]; then
	if [ -f /boot/ifl/orig_files/40_custom ]; then
	    # if IFL menu item is installed, use orig file backed up as the starting
	    # point in case other menu items added by user are already in 40_custom
	    cp /boot/ifl/orig_files/40_custom grub/40_custom
	elif [ -f /etc/grub.d/40_custom ]; then
	    # if IFL menu item not installed, use file curently in /etc/grub.d
	    # user may have added other menu items to 40_custom
	    cp /etc/grub.d/40_custom grub/40_custom
	else
	    # otherwise use included default copy of the file, which contains no menu items
	    cp util/40_custom grub/40_custom
	fi
	# added IFLmenu item to working copy of 40_custom
	echo "" >> grub/40_custom
	echo "menuentry '$grubmenutitle' {" >> grub/40_custom
	echo "  insmod gzio" >> grub/40_custom
	if [ "$biosuefi" = "UEFI" ]; then
	    echo "  gfxpayload='$gfxpayload'" >> grub/40_custom
	fi
	echo "  linux $bootpath/vmlinuz $vstring iflkmap=$kbd ktl=$ktl iflbflag=0 $ProductKeyText $bootparms" >> grub/40_custom
	echo "  initrd $bootpath/initrmfs.gz" >> grub/40_custom
	echo "}" >> grub/40_custom
	echo "$grubmenutitle" > grub/grubmenutitle
    fi

    # chown all files and directories to owner:group
    # as precaution make sure running from IFL dir, otherwise don't do it
    if [ "$0" = "./makeGRUB" ]; 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
    chmod 644 grub/vmlinuz 2> /dev/null
    chmod 644 grub/initrmfs.gz 2> /dev/null
    chmod 644 grub/ifl-menu.lst 2> /dev/null
    chmod 755 grub/40_custom 2> /dev/null

    echo
    echo "The build of the Grub boot files is now complete. The files created by"
    echo "the makeGRUB script can be found in the grub directory."
    echo
    if [ "$grubversion" = "2" ]; then
	echo "Note: The files in the grub directory will not be in use until they are"
	echo "INSTALLED by selecting 'Install Grub2 boot files for IFL' from the menu."
	echo "Doing that will add the IFL Boot Disk to the Grub menu."
	echo
    fi
}

install_grub1_boot_files() {
    clear
    if [ -f grub/vmlinuz ] && [ -f grub/initrmfs.gz ] && [ -f grub/ifl-menu.lst ]; then
	$dialogpath --no-shadow --yes-label "OK" --no-label "Cancel"  --title "Confirm Installing GRUB Files" --yesno "\nThe Grub Legacy boot files for IFL will now be installed to add IFL to the Grub boot menu. To do that, the following actions will be taken:\n\n* create the /boot/ifl directory, if not already there\n* install the IFL boot files in /boot/ifl\n* update $grubcfgpath to add IFL to the Grub boot menu\n* install /usr/local/bin/iflreboot and /usr/local/bin/iflupdate\n\nOnce the install completes, 'iflreboot' can be used to reboot the system into IFL, either manually, or through a cron job. These actions can be reversed at a later time by choosing the 'Uninstall Grub Legacy boot files for IFL' item from the main menu.\n\nPress <Enter> to continue, or select <Cancel> to cancel the install. \n\n" 21 80 2> /dev/null
	if [ ! "$?" = "0" ]; then return 1; fi
	clear
	echo "Attempting to install Grub boot files for IFL menu item"
	echo
	if [ "$grubcfgpath" = "" ]; then
	    echo "ERROR: Path to menu.lst file not found"
	    echo "Cancelling  install of IFL files"
	    echo
	    return 1
	else
	    echo "Active copy of grub config file found at $grubcfgpath"
	fi
	echo "Creating /boot/ifl directory (if not already there)"
	mkdir -p /boot/ifl/orig_files
	rm /boot/ifl/restore 2> /dev/null
	# back up original grub menu file if no file already there - this file not is used again by IFL scripts
	# it is backed up  in case user wants to manually restore it
	if [ ! -f /boot/ifl/orig_files/menu.lst ] && [ ! -f /boot/ifl/orig_files/grub.conf ]; then
	    echo "Backing up original grub config file to /boot/ifl/orig_files directory"
	    cp $grubcfgpath /boot/ifl/orig_files
	    cp $grubcfgpath $grubcfgpath-save-ifl
	fi
	#  see if main file matches menu.lst-reboot first - copy menu.lst-normal to it if it does
	if [ -f /boot/ifl/menu.lst-reboot ] && [ -f /boot/ifl/menu.lst-normal ]; then
	    cmp -s /boot/ifl/menu.lst-reboot $grubcfgpath
	    if [ "$?" != "0" ]; then
		cp /boot/ifl/menu.lst-normal $grubcfgpath
	    fi
	fi
	# run function to remove current IFL lines (5), if any
	remove_grub1_ifl_lines
	echo "Adding IFL to Grub menu by updating $grubcfgpath"
	cat grub/ifl-menu.lst >> $grubcfgpath
	echo "Copying IFL files from grub directory to /boot/ifl directory"
	cp grub/vmlinuz /boot/ifl/vmlinuz
	cp grub/initrmfs.gz /boot/ifl/initrmfs.gz
	cp grub/ifl-menu.lst /boot/ifl/ifl-menu.lst
	echo "Setting up /boot/ifl directory to process reboots to IFL"
	cp $grubcfgpath /boot/ifl/menu.lst-normal
	cp $grubcfgpath /boot/ifl/menu.lst-reboot
	# count lines in menu.lst that contain "title " and don't start with "#"
	grep "title" $grubcfgpath > titlelines.tmp 2> /dev/null
	titlelines=0
	while read line
	do
	    if [ "${line:0:1}" != "#" ]; then let titlelines=titlelines+1; fi
	done < "titlelines.tmp"
	rm titlelines.tmp 2> /dev/null
	if [ "$titlelines" -gt "0" ]; then let titlelines=titlelines-1; else titlelines=0; fi

	# grep all lines containing "default" to tmp file
	# replace all lines not starting with "#" with default=$titlelines
	grep "default" $grubcfgpath > defaultlines.tmp 2> /dev/null
	while read line
	do
	    if [ "${line:0:1}" != "#" ]; then
		sed -i -e "s/$line/default=$titlelines/" /boot/ifl/menu.lst-reboot
	    fi
	done < "defaultlines.tmp"
	rm defaultlines.tmp 2> /dev/null

	# create /usr/local/bin/iflreboot script
	echo "Creating iflreboot reboot script at /usr/local/bin/iflreboot"
	create_grub1_reboot_script
	# create /usr/local/bin/iflupdate script
	echo "Creating iflupdate reboot script at /usr/local/bin/iflupdate"
	create_grub1_update_script

	echo "Grub menu title for IFL boot item is '$grubmenutitle'"
	echo
	echo "Press <Enter> to continue"
	read xyz
    else
	$dialogpath --no-shadow --title "Grub Files Missing" --msgbox "\nSome or all of the required Grub files for IFL are missing from the grub directory. Please select 'Create Grub Legacy boot files for IFL' from the menu to create a full set of Grub2 files.\n\n" 9 75 2> /dev/null
    fi
}

remove_grub1_ifl_lines() {
    nocopy=0
    rm /boot/ifl/newgrubconf 2> /dev/null
    while read line
    do
	if [ "$nocopy" = "0" ]; then
	    if [ "$line" = "#MARKER: IFL Boot Disk Stanza (do not remove this line)" ]; then
		nocopy=1
	    else
		echo "$line" >> /boot/ifl/newgrubconf
	    fi
	elif [ "$nocopy" -gt "0" ] && [ "$nocopy" -lt "5" ]; then
	    let ncopy=ncopy+1
	else
	    echo "$line" >> /boot/ifl/newgrubconf
	fi
    done < "$grubcfgpath"
    cp /boot/ifl/newgrubconf $grubcfgpath
    rm /boot/ifl/newgrubconf 2> /dev/null
}

create_grub1_reboot_script() {
    # create /usr/local/bin/iflreboot script
    echo "#! /bin/bash" > /usr/local/bin/iflreboot
    echo "date >> /usr/local/bin/iflreboot.log"  >> /usr/local/bin/iflreboot
    echo "for iflfile in menu.lst-reboot menu.lst-normal vmlinuz initrmfs.gz" >> /usr/local/bin/iflreboot
    echo "do" >> /usr/local/bin/iflreboot
    echo "    if [ ! -f /boot/ifl/\$iflfile ]; then" >> /usr/local/bin/iflreboot
    echo "        echo \"One or more IFL files not found in /boot/ifl. Reboot to IFL cancelled.\" >> /usr/local/bin/iflreboot.log" >> /usr/local/bin/iflreboot
    echo "        echo \"\" >>  /usr/local/bin/iflreboot.log" >> /usr/local/bin/iflreboot
    echo "        exit 1" >> /usr/local/bin/iflreboot
    echo "    fi" >> /usr/local/bin/iflreboot
    echo "done" >> /usr/local/bin/iflreboot
    echo "cmp -s /boot/ifl/menu.lst-normal $grubcfgpath" >> /usr/local/bin/iflreboot
    echo "if [ \"\$?\" != \"0\" ]; then" >> /usr/local/bin/iflreboot
    echo "    cmp -s /boot/ifl/menu.lst-reboot $grubcfgpath" >> /usr/local/bin/iflreboot
    echo "    if [ \"\$?\" != \"0\" ]; then" >> /usr/local/bin/iflreboot
    echo "        echo \"Updating /boot/ifl/menu.lst-* files due to mismatch between active and saved copies.\" >> /usr/local/bin/iflreboot.log" >> /usr/local/bin/iflreboot
    echo "        cp $grubcfgpath /boot/ifl/menu.lst-normal" >> /usr/local/bin/iflreboot
    echo "        cp $grubcfgpath /boot/ifl/menu.lst-reboot" >> /usr/local/bin/iflreboot
    echo "        grep \"title\" $grubcfgpath > /usr/local/bin/titlelines.tmp 2> /dev/null" >> /usr/local/bin/iflreboot
    echo "        titlelines=0" >> /usr/local/bin/iflreboot
    echo "        while read line" >> /usr/local/bin/iflreboot
    echo "        do" >> /usr/local/bin/iflreboot
    echo "            if [ \"\${line:0:1}\" != \"#\" ]; then let titlelines=titlelines+1; fi" >> /usr/local/bin/iflreboot
    echo "        done < /usr/local/bin/titlelines.tmp" >> /usr/local/bin/iflreboot
    echo "        rm /usr/local/bin/titlelines.tmp 2> /dev/null" >> /usr/local/bin/iflreboot
    echo "        if [ \"\$titlelines\" -gt \"0\" ]; then let titlelines=titlelines-1; else titlelines=0; fi" >> /usr/local/bin/iflreboot
    echo "        grep \"default\" $grubcfgpath > /usr/local/bin/defaultlines.tmp 2> /dev/null" >> /usr/local/bin/iflreboot
    echo "        while read line" >> /usr/local/bin/iflreboot
    echo "        do" >> /usr/local/bin/iflreboot
    echo "            if [ \"\${line:0:1}\" != \"#\" ]; then" >> /usr/local/bin/iflreboot
    echo "                sed -i -e \"s/\$line/default=\$titlelines/\" /boot/ifl/menu.lst-reboot" >> /usr/local/bin/iflreboot
    echo "            fi" >> /usr/local/bin/iflreboot
    echo "        done < /usr/local/bin/defaultlines.tmp" >> /usr/local/bin/iflreboot
    echo "        rm /usr/local/bin/defaultlines.tmp 2> /dev/null" >> /usr/local/bin/iflreboot
    echo "    else" >> /usr/local/bin/iflreboot
    echo "        cp /boot/ifl/menu.lst-normal $grubcfgpath" >> /usr/local/bin/iflreboot
    echo "    fi" >> /usr/local/bin/iflreboot
    echo "fi" >> /usr/local/bin/iflreboot
    echo "cp /boot/ifl/menu.lst-reboot $grubcfgpath" >> /usr/local/bin/iflreboot
    echo "if [ \"\$1\" != \"\" ]; then" >> /usr/local/bin/iflreboot
    if [ "$iflgrubnormalpath" = "/boot/ifl/menu.lst-normal" ]; then
	echo "    sed -i -e \"s/boot\/ifl\/vmlinuz/boot\/ifl\/vmlinuz iflscript=\$1/\" $grubcfgpath" >> /usr/local/bin/iflreboot
    else
	echo "    sed -i -e \"s/ifl\/vmlinuz/ifl\/vmlinuz iflscript=\$1/\" $grubcfgpath" >> /usr/local/bin/iflreboot
    fi
    echo "    echo \"Rebooting to IFL with iflscript=\$1.\" >> /usr/local/bin/iflreboot.log" >> /usr/local/bin/iflreboot
    echo "else" >> /usr/local/bin/iflreboot
    echo "    echo \"Rebooting to IFL with no iflscript parameter.\" >> /usr/local/bin/iflreboot.log" >> /usr/local/bin/iflreboot
    echo "fi" >> /usr/local/bin/iflreboot
    echo "echo \"\" >>  /usr/local/bin/iflreboot.log" >> /usr/local/bin/iflreboot
    echo "touch /boot/ifl/restore" >> /usr/local/bin/iflreboot
    echo "/sbin/shutdown -r now" >> /usr/local/bin/iflreboot
    chown 0:0 /usr/local/bin/iflreboot
    chmod +x /usr/local/bin/iflreboot
}

create_grub1_update_script() {
    # create /usr/local/bin/iflreboot script
    echo "#! /bin/bash" > /usr/local/bin/iflupdate
    echo "date >> /usr/local/bin/iflreboot.log"  >> /usr/local/bin/iflupdate
    echo "echo \"Running /usr/local/bin/iflupdate script.\" >> /usr/local/bin/iflreboot.log"  >> /usr/local/bin/iflupdate
    echo "rm /boot/ifl/restore 2> /dev/null" >> /usr/local/bin/iflupdate
    echo "for iflfile in menu.lst-reboot menu.lst-normal" >> /usr/local/bin/iflupdate
    echo "do" >> /usr/local/bin/iflupdate
    echo "    if [ ! -f /boot/ifl/\$iflfile ]; then" >> /usr/local/bin/iflupdate
    echo "        echo \"One or more IFL files not found in /boot/ifl. Update to IFL files cancelled.\" >> /usr/local/bin/iflreboot.log" >> /usr/local/bin/iflupdate
    echo "        echo \"\" >>  /usr/local/bin/iflreboot.log" >> /usr/local/bin/iflupdate
    echo "        exit 1" >> /usr/local/bin/iflupdate
    echo "    fi" >> /usr/local/bin/iflupdate
    echo "done" >> /usr/local/bin/iflupdate
    echo "cmp -s /boot/ifl/menu.lst-normal $grubcfgpath" >> /usr/local/bin/iflupdate
    echo "if [ \"\$?\" != \"0\" ]; then" >> /usr/local/bin/iflupdate
    echo "    cmp -s /boot/ifl/menu.lst-reboot $grubcfgpath" >> /usr/local/bin/iflupdate
    echo "    if [ \"\$?\" != \"0\" ]; then" >> /usr/local/bin/iflupdate
    echo "        echo \"Updating /boot/ifl/menu.lst-* files due to mismatch between active and saved copies.\" >> /usr/local/bin/iflreboot.log" >> /usr/local/bin/iflupdate
    echo "        cp $grubcfgpath /boot/ifl/menu.lst-normal" >> /usr/local/bin/iflupdate
    echo "        cp $grubcfgpath /boot/ifl/menu.lst-reboot" >> /usr/local/bin/iflupdate
    echo "        grep \"title\" $grubcfgpath > /usr/local/bin/titlelines.tmp 2> /dev/null" >> /usr/local/bin/iflupdate
    echo "        titlelines=0" >> /usr/local/bin/iflupdate
    echo "        while read line" >> /usr/local/bin/iflupdate
    echo "        do" >> /usr/local/bin/iflupdate
    echo "            if [ \"\${line:0:1}\" != \"#\" ]; then let titlelines=titlelines+1; fi" >> /usr/local/bin/iflupdate
    echo "        done < /usr/local/bin/titlelines.tmp" >> /usr/local/bin/iflupdate
    echo "        rm /usr/local/bin/titlelines.tmp 2> /dev/null" >> /usr/local/bin/iflupdate
    echo "        if [ \"\$titlelines\" -gt \"0\" ]; then let titlelines=titlelines-1; else titlelines=0; fi" >> /usr/local/bin/iflupdate
    echo "        grep \"default\" $grubcfgpath > /usr/local/bin/defaultlines.tmp 2> /dev/null" >> /usr/local/bin/iflupdate
    echo "        while read line" >> /usr/local/bin/iflupdate
    echo "        do" >> /usr/local/bin/iflupdate
    echo "            if [ \"\${line:0:1}\" != \"#\" ]; then" >> /usr/local/bin/iflupdate
    echo "                sed -i -e \"s/\$line/default=\$titlelines/\" /boot/ifl/menu.lst-reboot" >> /usr/local/bin/iflupdate
    echo "            fi" >> /usr/local/bin/iflupdate
    echo "        done < /usr/local/bin/defaultlines.tmp" >> /usr/local/bin/iflupdate
    echo "        rm /usr/local/bin/defaultlines.tmp 2> /dev/null" >> /usr/local/bin/iflupdate
    echo "    else" >> /usr/local/bin/iflupdate
    echo "        echo \"Restoring /boot/ifl/grub.cfg-normal to $grubcfgpath\" >> /usr/local/bin/iflreboot.log" >> /usr/local/bin/iflupdate
    echo "        cp /boot/ifl/menu.lst-normal $grubcfgpath" >> /usr/local/bin/iflupdate
    echo "    fi" >> /usr/local/bin/iflupdate
    echo "fi" >> /usr/local/bin/iflupdate
    echo "echo \"\" >>  /usr/local/bin/iflreboot.log" >> /usr/local/bin/iflupdate
    chown 0:0 /usr/local/bin/iflupdate
    chmod +x /usr/local/bin/iflupdate
}

get_grubversion() {
    for grubfile in /etc/grub.d /etc/grub.d/40_custom
    do
	if [ -e $grubfile ]; then
	    grubversion=2
	else
	    grubversion=0
	     break
	fi
    done
    if [ "$grubversion" = "2" ]; then
	if [ -f /boot/grub/grub.cfg ]; then
	    grubcfgpath="/boot/grub/grub.cfg"
	    return
	elif [ -f /boot/grub2/grub.cfg ]; then
	    grubcfgpath="/boot/grub2/grub.cfg"
	    return
	elif [ -f /boot/efi/EFI/fedora/grub.cfg ]; then
	    # grubcfgpath="/boot/efi/EFI/fedora/grub.cfg"
	    grubversion=fedorauefi
	    grubcfgpath=""
	    return
	else
	    grubversion=0
	    grubcfgpath=""
	fi
    elif [ "$grubversion" = "0" ]; then
	if [ -f /boot/grub/menu.lst ]; then
	    if [ -f /boot/grub/grub.conf ]; then
		grubcfgpath="/boot/grub/grub.conf"
		grubversion=1
		return
	    else
		grubcfgpath="/boot/grub/menu.lst"
		grubversion=1
		return
	    fi
	elif [ -f /boot/efi/EFI/redhat/grub.conf ]; then
	    grubcfgpath="/boot/efi/EFI/redhat/grub.conf"
	    grubversion=1
	    return
	else
	    grubversion=0
	    grubcfgpath=""
	fi
    fi
}

get_biosuefi() {
    efipart=""
    efiarray=( `mount | grep -i "/boot/efi "` ) > /dev/null
    if [ "$?" = "0" ]; then
	biosuefi="UEFI"
	efipart=${efiarray[0]}
    else
	biosuefi="BIOS"
    fi
}

get_grub_partition() {
    grubpart=""
    grubarray=( `mount | grep -i "/boot "` ) > /dev/null
    if [ "$?" = "0" ]; then
	grubpart=${grubarray[0]}
	iflgrubrestoreflag="/ifl/restore"
	if [ "$grubversion" = "2" ]; then
	    iflgrubnormalpath="/ifl/grub.cfg-normal"
	else
	    iflgrubnormalpath="/ifl/menu.lst-normal"
	fi
	if [ "$grubcfgpath" = "/boot/grub/grub.cfg" ]; then
	    iflgrubpath="/grub/grub.cfg"
	elif [ "$grubcfgpath" = "/boot/grub2/grub.cfg" ]; then
	    iflgrubpath="/grub2/grub.cfg"
	elif [ "$grubcfgpath" = "/boot/efi/EFI/fedora/grub.cfg" ]; then
	    iflgrubpath="/EFI/fedora/grub.cfg"
	elif [ "$grubcfgpath" = "/boot/grub/grub.conf" ]; then
	    iflgrubpath="/grub/grub.conf"
	elif [ "$grubcfgpath" = "/boot/grub/menu.lst" ]; then
	    iflgrubpath="/grub/menu.lst"
	elif [ "$grubcfgpath" = "/boot/efi/EFI/redhat/grub.conf" ]; then
	    iflgrubpath="/EFI/redhat/grub.conf"
	fi
    else
	grubarray=( `mount | grep -i "/ "` ) > /dev/null
	if [ "$?" = "0" ]; then
	    grubpart=${grubarray[0]}
	    iflgrubrestoreflag="/boot/ifl/restore"
	    if [ "$grubversion" = "2" ]; then
		iflgrubnormalpath="/boot/ifl/grub.cfg-normal"
	    else
		iflgrubnormalpath="/boot/ifl/menu.lst-normal"
	    fi
	    if [ "$grubcfgpath" = "/boot/grub/grub.cfg" ]; then
		iflgrubpath="/boot/grub/grub.cfg"
	    elif [ "$grubcfgpath" = "/boot/grub2/grub.cfg" ]; then
		iflgrubpath="/boot/grub2/grub.cfg"
	    elif [ "$grubcfgpath" = "/boot/grub/grub.conf" ]; then
		iflgrubpath="/boot/grub/grub.conf"
	    elif [ "$grubcfgpath" = "/boot/grub/menu.lst" ]; then
		iflgrubpath="/boot/grub/menu.lst"
	    elif [ "$grubcfgpath" = "/boot/efi/EFI/redhat/grub.conf" ]; then
		iflgrubpath="/EFI/redhat/grub.conf"
	    fi
	fi
    fi
    # rule out that grub partition is on anything other than standard partition with ext2/3/4 fs
    # needs to be mounted from boot disk to switch grub.cfg file
    if [ "$grubpart" = "" ]; then
	return 1
    elif [ "${grubpart:0:7}" = "/dev/sd" ]; then
	for fs in ext2 ext3 ext4
	do
	    blkid $grubpart | grep " TYPE=\"$fs\"" > /dev/null
	    if [ "$?" = "0" ]; then return 0; fi
	done
	return 1
    elif [ "${grubpart:0:18}" = "/dev/disk/by-uuid/" ] && [ -e /etc/debian_version ]; then
	for fs in ext2 ext3 ext4
	do
	    blkid $grubpart | grep " TYPE=\"$fs\"" > /dev/null
	    if [ "$?" = "0" ]; then return 0; fi
	done
	return 1
    else
	return 1
    fi
}

install_grub2_boot_files() {
    clear
    if [ -f grub/vmlinuz ] && [ -f grub/initrmfs.gz ] && [ -f grub/40_custom ] && [ -f grub/grubmenutitle ]; then
	$dialogpath --no-shadow --yes-label "OK" --no-label "Cancel"  --title "Confirm Installing GRUB Files" --yesno "\nThe Grub2 boot files for IFL will now be installed to add IFL to the Grub boot menu. To do that, the following actions will be taken:\n\n* create the /boot/ifl directory, if not already there\n* install the IFL boot files in /boot/ifl\n* install the IFL version of /etc/grub.d/40_custom\n* update $grubcfgpath to add IFL to the Grub boot menu\n* install /usr/local/bin/iflreboot and /usr/local/bin/iflupdate\n\nOnce the install completes, 'iflreboot' can be used to reboot the system into IFL, either manually, or through a cron job. These actions can be reversed at a later time by choosing the 'Uninstall Grub2 boot files for IFL' item from the main menu.\n\nPress <Enter> to continue, or select <Cancel> to cancel the install. \n\n" 21 80 2> /dev/null
	if [ ! "$?" = "0" ]; then return 1; fi
	clear
	echo "Attempting to install Grub2 boot files for IFL menu item"
	echo
	if [ "$grubcfgpath" = "" ]; then
	    echo "ERROR: Path to grub.cfg file not found"
	    echo "Cancelling  install of IFL files"
	    echo
	    return 1
	else
	    echo "Active copy of grub.cfg file found at $grubcfgpath"
	fi
	grubmkconfigpath=`which grub-mkconfig 2> /dev/null`
	if [ "$?" != "0" ]; then
	    grubmkconfigpath=`which grub2-mkconfig 2> /dev/null`
	    if [ "$?" != "0" ]; then
		echo "ERROR: grub-mkconfig or grub2-mkconfig not found"
		echo "Cancelling  install of IFL files"
		return 1
	    fi
	fi
	echo "grub-mkconfig file found at $grubmkconfigpath"
	echo "Creating /boot/ifl directory (if not already there)"
	mkdir -p /boot/ifl/orig_files
	rm /boot/ifl/restore 2> /dev/null
	if [ ! -f /boot/ifl/orig_files/grub.cfg ]; then 
	    echo "Backing up original grub.cfg file to /boot/ifl/orig_files directory"
	    cp $grubcfgpath /boot/ifl/orig_files/grub.cfg
	    cp $grubcfgpath $grubcfgpath-save-ifl
	fi
	if [ ! -f /boot/ifl/orig_files/40_custom ]; then 
	    echo "Backing up original 40_custom file to /boot/ifl/orig_files directory"
	    cp /etc/grub.d/40_custom /boot/ifl/orig_files/40_custom
	fi
	echo "Copying IFL files from grub directory to /boot/ifl directory"
	cp grub/vmlinuz /boot/ifl/vmlinuz
	cp grub/initrmfs.gz /boot/ifl/initrmfs.gz
	cp grub/grubmenutitle /boot/ifl/grubmenutitle
	echo "Installing IFL version of 40_custom in /etc/grub.d directory"
	cp grub/40_custom /etc/grub.d/40_custom
	# usually will not be needed, but set ownership to root and permissions to 755 to make sure
	chown 0:0 /etc/grub.d/40_custom 2> /dev/null
	chmod 755 /etc/grub.d/40_custom 2> /dev/null
	echo "Running '$grubmkconfigpath -o $grubcfgpath"
	echo "This will add IFL to the Grub2 boot menu"
	$grubmkconfigpath -o $grubcfgpath
	echo "Setting up /boot/ifl directory to process reboots to IFL"
	cp $grubcfgpath /boot/ifl/grub.cfg-normal
	cp $grubcfgpath /boot/ifl/grub.cfg-reboot
	# set up the reboot copy of grub.cfg in /boot/ifl by replacing the set default= line
	# grep all lines containing "set default=" to tmp file, then change each line to IFL menu title
	# usually will be just one line, but some distros (Fedora 19) may have 2
	grep "set default=" $grubcfgpath > defaultlines.tmp 2> /dev/null
	while read line
	do
	    sed -i -e "s/$line/set default='$grubmenutitle'/" /boot/ifl/grub.cfg-reboot
	done < "defaultlines.tmp"
	rm defaultlines.tmp 2> /dev/null

	# create /usr/local/bin/iflreboot script
	echo "Creating iflreboot reboot script at /usr/local/bin/iflreboot"
	create_grub2_reboot_script
	# create /usr/local/bin/iflupdate script
	echo "Creating iflupdate script at /usr/local/bin/iflupdate"
	create_grub2_update_script

	echo "Grub menu title for IFL boot item is '$grubmenutitle'"
	echo
	echo "Press <Enter> to continue"
	read xyz
    else
	$dialogpath --no-shadow --title "Grub Files Missing" --msgbox "\nSome or all of the required Grub2 files for IFL are missing from the grub directory. Please select 'Create Grub2 boot files for IFL' from the menu to create a full set of Grub2 files.\n\n" 9 75 2> /dev/null
    fi
}

create_grub2_reboot_script() {
    # create /usr/local/bin/iflreboot script
    echo "#! /bin/bash" > /usr/local/bin/iflreboot
    echo "date >> /usr/local/bin/iflreboot.log"  >> /usr/local/bin/iflreboot
    echo "for iflfile in grub.cfg-reboot grub.cfg-normal vmlinuz initrmfs.gz" >> /usr/local/bin/iflreboot
    echo "do" >> /usr/local/bin/iflreboot
    echo "    if [ ! -f /boot/ifl/\$iflfile ]; then" >> /usr/local/bin/iflreboot
    echo "        echo \"One or more IFL files not found in /boot/ifl. Reboot to IFL cancelled.\" >> /usr/local/bin/iflreboot.log"  >> /usr/local/bin/iflreboot
    echo "        echo \"\" >>  /usr/local/bin/iflreboot.log" >> /usr/local/bin/iflreboot
    echo "        exit 1" >> /usr/local/bin/iflreboot
    echo "    fi" >> /usr/local/bin/iflreboot
    echo "done" >> /usr/local/bin/iflreboot
    echo "cmp -s /boot/ifl/grub.cfg-normal $grubcfgpath" >> /usr/local/bin/iflreboot
    echo "if [ \"\$?\" != \"0\" ]; then" >> /usr/local/bin/iflreboot
    echo "    cmp -s /boot/ifl/grub.cfg-reboot $grubcfgpath" >> /usr/local/bin/iflreboot
    echo "    if [ \"\$?\" != \"0\" ]; then" >> /usr/local/bin/iflreboot
    echo "        echo \"Updating grub.cfg files due to mismatch between active and saved copies.\" >> /usr/local/bin/iflreboot.log" >> /usr/local/bin/iflreboot
    echo "        $grubmkconfigpath -o $grubcfgpath > /dev/null 2>&1" >> /usr/local/bin/iflreboot
    echo "        cp $grubcfgpath /boot/ifl/grub.cfg-normal" >> /usr/local/bin/iflreboot
    echo "        cp $grubcfgpath /boot/ifl/grub.cfg-reboot" >> /usr/local/bin/iflreboot
    echo "        grep \"set default=\" $grubcfgpath > /usr/local/bin/defaultlines.tmp 2> /dev/null" >> /usr/local/bin/iflreboot
    echo "        while read line" >> /usr/local/bin/iflreboot
    echo "        do" >> /usr/local/bin/iflreboot
    echo "            sed -i -e \"s/\$line/set default='$grubmenutitle'/\" /boot/ifl/grub.cfg-reboot" >> /usr/local/bin/iflreboot
    echo "        done < \"/usr/local/bin/defaultlines.tmp\"" >> /usr/local/bin/iflreboot
    echo "        rm /usr/local/bin/defaultlines.tmp 2> /dev/null" >> /usr/local/bin/iflreboot
    echo "    else" >> /usr/local/bin/iflreboot
    echo "        cp /boot/ifl/grub.cfg-normal $grubcfgpath" >> /usr/local/bin/iflreboot
    echo "    fi" >> /usr/local/bin/iflreboot
    echo "fi" >> /usr/local/bin/iflreboot
    echo "cp /boot/ifl/grub.cfg-reboot $grubcfgpath" >> /usr/local/bin/iflreboot
    echo "if [ \"\$1\" != \"\" ]; then" >> /usr/local/bin/iflreboot
    # echo "    sed -i -e \"s/vmlinuz/vmlinuz iflscript=\$1/\" $grubcfgpath" >> /usr/local/bin/iflreboot
    if [ "$iflgrubnormalpath" = "/boot/ifl/grub.cfg-normal" ]; then
	echo "    sed -i -e \"s/boot\/ifl\/vmlinuz/boot\/ifl\/vmlinuz iflscript=\$1/\" $grubcfgpath" >> /usr/local/bin/iflreboot
    else
	echo "    sed -i -e \"s/ifl\/vmlinuz/ifl\/vmlinuz iflscript=\$1/\" $grubcfgpath" >> /usr/local/bin/iflreboot
    fi
    echo "    echo \"Rebooting to IFL with iflscript=\$1.\" >> /usr/local/bin/iflreboot.log" >> /usr/local/bin/iflreboot
    echo "else" >> /usr/local/bin/iflreboot
    echo "    echo \"Rebooting to IFL with no iflscript parameter.\" >> /usr/local/bin/iflreboot.log" >> /usr/local/bin/iflreboot
    echo "fi" >> /usr/local/bin/iflreboot
    echo "echo \"\" >>  /usr/local/bin/iflreboot.log" >> /usr/local/bin/iflreboot
    echo "touch /boot/ifl/restore" >> /usr/local/bin/iflreboot
    echo "/sbin/shutdown -r now" >> /usr/local/bin/iflreboot
    chown 0:0 /usr/local/bin/iflreboot
    chmod +x /usr/local/bin/iflreboot
}

create_grub2_update_script() {
    # create /usr/local/bin/iflreboot script
    echo "#! /bin/bash" > /usr/local/bin/iflupdate
    echo "date >> /usr/local/bin/iflreboot.log"  >> /usr/local/bin/iflupdate
    echo "echo \"Running /usr/local/bin/iflupdate script.\" >> /usr/local/bin/iflreboot.log"  >> /usr/local/bin/iflupdate
    echo "rm /boot/ifl/restore 2> /dev/null" >> /usr/local/bin/iflupdate
    echo "for iflfile in grub.cfg-reboot grub.cfg-normal" >> /usr/local/bin/iflupdate
    echo "do" >> /usr/local/bin/iflupdate
    echo "    if [ ! -f /boot/ifl/\$iflfile ]; then" >> /usr/local/bin/iflupdate
    echo "        echo \"One or more IFL files not found in /boot/ifl. Update to IFL files cancelled.\" >> /usr/local/bin/iflreboot.log"  >> /usr/local/bin/iflupdate
    echo "        echo \"\" >>  /usr/local/bin/iflreboot.log" >> /usr/local/bin/iflupdate
    echo "        exit 1" >> /usr/local/bin/iflupdate
    echo "    fi" >> /usr/local/bin/iflupdate
    echo "done" >> /usr/local/bin/iflupdate
    echo "cmp -s /boot/ifl/grub.cfg-normal $grubcfgpath" >> /usr/local/bin/iflupdate
    echo "if [ \"\$?\" != \"0\" ]; then" >> /usr/local/bin/iflupdate
    echo "    cmp -s /boot/ifl/grub.cfg-reboot $grubcfgpath" >> /usr/local/bin/iflupdate
    echo "    if [ \"\$?\" != \"0\" ]; then" >> /usr/local/bin/iflupdate
    echo "        echo \"Updating grub.cfg files due to mismatch between active and saved copies.\" >> /usr/local/bin/iflreboot.log" >> /usr/local/bin/iflupdate
    echo "        $grubmkconfigpath -o $grubcfgpath > /dev/null 2>&1" >> /usr/local/bin/iflupdate
    echo "        cp $grubcfgpath /boot/ifl/grub.cfg-normal" >> /usr/local/bin/iflupdate
    echo "        cp $grubcfgpath /boot/ifl/grub.cfg-reboot" >> /usr/local/bin/iflupdate
    echo "        grep \"set default=\" $grubcfgpath > /usr/local/bin/defaultlines.tmp 2> /dev/null" >> /usr/local/bin/iflupdate
    echo "        while read line" >> /usr/local/bin/iflupdate
    echo "        do" >> /usr/local/bin/iflupdate
    echo "            sed -i -e \"s/\$line/set default='$grubmenutitle'/\" /boot/ifl/grub.cfg-reboot" >> /usr/local/bin/iflupdate
    echo "        done < \"/usr/local/bin/defaultlines.tmp\"" >> /usr/local/bin/iflupdate
    echo "        rm /usr/local/bin/defaultlines.tmp 2> /dev/null" >> /usr/local/bin/iflupdate
    echo "    else" >> /usr/local/bin/iflupdate
    echo "        echo \"Restoring /boot/ifl/grub.cfg-normal to $grubcfgpath\" >> /usr/local/bin/iflreboot.log" >> /usr/local/bin/iflupdate
    echo "        cp /boot/ifl/grub.cfg-normal $grubcfgpath" >> /usr/local/bin/iflupdate
    echo "    fi" >> /usr/local/bin/iflupdate
    echo "fi" >> /usr/local/bin/iflupdate
    echo "echo \"\" >>  /usr/local/bin/iflreboot.log" >> /usr/local/bin/iflupdate
    chown 0:0 /usr/local/bin/iflupdate
    chmod +x /usr/local/bin/iflupdate
}

uninstall_grub2_boot_files() {
    clear
    if [ -d /boot/ifl ] || [ -f /usr/local/bin/iflreboot ] || [ -f /etc/cron.d/iflcron ]; then
	$dialogpath --no-shadow --yes-label "OK" --no-label "Cancel"  --title "Confirm Uninstalling Grub2 Files For IFL" --yesno "\nTo uninstall the Grub2 boot files for IFL, the following actions will be taken:\n\n* restore original copy of /etc/grub.d/40_custom\n* delete the entire /boot/ifl directory\n* delete /usr/local/bin/iflupdate\n* delete /usr/local/bin/iflreboot and /usr/local/bin/iflreboot.log\n* delete /etc/cron.d/iflcron\n* update $grubcfgpath to remove IFL from the Grub menu\n\nPress <Enter> to continue, or select <Cancel> to cancel the uninstall. \n\n" 17 80 2> /dev/null
	if [ ! "$?" = "0" ]; then return 1; fi
	clear
	echo "Attempting to uninstall Grub2 boot files for IFL"
	echo
	grubmkconfigpath=`which grub-mkconfig 2> /dev/null`
	if [ "$?" != "0" ]; then
	    grubmkconfigpath=`which grub2-mkconfig 2> /dev/null`
	    if [ "$?" != "0" ]; then
		echo "ERROR: grub-mkconfig or grub2-mkconfig not found"
		echo "Cancelling  install of IFL files"
		return 1
	    fi
	fi
	echo "grub-mkconfig file found at $grubmkconfigpath"
	if [ -f /etc/cron.d/iflcron ]; then
	    echo "Deleting /etc/cron.d/iflcron"
	    rm /etc/cron.d/iflcron 2> /dev/null
	fi
	if [ -f /usr/local/bin/iflreboot ]; then
	    echo "Deleting /usr/local/bin/iflreboot"
	    rm /usr/local/bin/iflreboot 2> /dev/null
	fi
	if [ -f /usr/local/bin/iflupdate ]; then
	    echo "Deleting /usr/local/bin/iflupdate"
	    rm /usr/local/bin/iflupdate 2> /dev/null
	fi
	if [ -f /usr/local/bin/iflreboot.log ]; then
	    echo "Deleting /usr/local/bin/iflreboot.log"
	    rm /usr/local/bin/iflreboot.log 2> /dev/null
	fi
	if [ -f /boot/ifl/orig_files/40_custom ]; then
	    echo "Copying /boot/ifl/orig_files/40_custom to /etc/grub.d/40_custom"
	    cp /boot/ifl/orig_files/40_custom /etc/grub.d/40_custom 2> /dev/null
	fi
	if [ -d /boot/ifl ]; then
	    echo "Deleting the entire /boot/ifl directory"
	    rm -r /boot/ifl 2> /dev/null
	fi
	echo "Running '$grubmkconfigpath -o $grubcfgpath' to remove IFL item from grub.cfg"
	$grubmkconfigpath -o $grubcfgpath
	echo
	echo "Press <Enter> to continue"
	read xyz
    else
	$dialogpath --no-shadow --title "Grub2 Files Not Installed" --msgbox "\nNo Grub2 files for IFL are currently installed.\n\nTo install Grub2 IFL files, you must first select 'Create Grub2 boot files for IFL' to create them, and then select 'Install Grub2 boot files for IFL' to install them." 11 75 2> /dev/null
    fi
}


uninstall_grub1_boot_files() {
    clear
    if [ -d /boot/ifl ] || [ -f /usr/local/bin/iflreboot ] || [ -f /etc/cron.d/iflcron ]; then
	$dialogpath --no-shadow --yes-label "OK" --no-label "Cancel"  --title "Confirm Uninstalling Grub Legacy Files For IFL" --yesno "\nTo uninstall the Grub Legacy boot files for IFL, the following actions will be taken:\n\n* remove IFL from the Grub menu\n* delete the entire /boot/ifl directory\n* delete /usr/local/bin/iflupdate\n* delete /usr/local/bin/iflreboot and /usr/local/bin/iflreboot.log\n* delete /etc/cron.d/iflcron\n\nPress <Enter> to continue, or select <Cancel> to cancel the uninstall. \n\n" 17 80 2> /dev/null
	if [ ! "$?" = "0" ]; then return 1; fi
	clear
	echo "Attempting to uninstall Grub Legacy boot files for IFL"
	echo
	if [ -f /etc/cron.d/iflcron ]; then
	    echo "Deleting /etc/cron.d/iflcron"
	    rm /etc/cron.d/iflcron 2> /dev/null
	fi
	if [ -f /usr/local/bin/iflreboot ]; then
	    echo "Deleting /usr/local/bin/iflreboot"
	    rm /usr/local/bin/iflreboot 2> /dev/null
	fi
	if [ -f /usr/local/bin/iflupdate ]; then
	    echo "Deleting /usr/local/bin/iflupdate"
	    rm /usr/local/bin/iflupdate 2> /dev/null
	fi
	if [ -f /usr/local/bin/iflreboot.log ]; then
	    echo "Deleting /usr/local/bin/iflreboot.log"
	    rm /usr/local/bin/iflreboot.log 2> /dev/null
	fi
	#  see if main file matches menu.lst-reboot first - copy menu.lst-normal to it if it does
	if [ -f /boot/ifl/menu.lst-reboot ] && [ -f /boot/ifl/menu.lst-normal ]; then
	    cmp -s /boot/ifl/menu.lst-reboot $grubcfgpath
	    if [ "$?" != "0" ]; then
		cp /boot/ifl/menu.lst-normal $grubcfgpath
	    fi
	fi
	remove_grub1_ifl_lines
	if [ -d /boot/ifl ]; then
	    echo "Deleting the entire /boot/ifl directory"
	    rm -r /boot/ifl 2> /dev/null
	fi
	echo
	echo "Press <Enter> to continue"
	read xyz
    else
	$dialogpath --no-shadow --title "Grub Legacy Files Not Installed" --msgbox "\nNo Grub Legacy files for IFL are currently installed.\n\nTo install Grub Legacy files for IFL, you must first select 'Create Grub Legacy boot files for IFL' to create them, and then select 'Install Grub Legacy boot files for IFL' to install them." 11 75 2> /dev/null
    fi
}


manage_cron_jobs() {
if [ ! -f /usr/local/bin/iflreboot ]; then
    $dialogpath --no-shadow --title "Grub Files for IFL Are Not Installed" --msgbox "\nThe Grub files for IFL are not currently installed.\n\nTo be able to schedule and run IFL cron jobs, select 'Create Grub2 boot files for IFL' to create the files, and then select 'Install Grub2 boot files for IFL' to install them.\n \n" 11 75 2> /dev/null
    return 1
fi
defaultcronjobline="30 5 * * Sun root /usr/local/bin/iflreboot"
while [ 0 ]; do
    get_cron_jobs
    if [ "$njobs" -lt "2" ]; then
	menuheight=13
    else
	let menuheight=12+njobs
    fi
    cronjobtext=""
    if [ "$njobs" = "0" ]; then
	cronjobtext="No cron jobs currently defined.\n"
    else
	for i in `seq 1 $njobs`
	do
	    cronjobtext="$cronjobtext$i: ${jobarray[$i]}\n"
	done
    fi
    $dialogpath --no-shadow --title  "Schedule IFL Cron Jobs" --menu "\n$cronjobtext \n" $menuheight 80 4\
    "Exit Menu" "" \
    "Add an IFL cron job" "" \
    "Edit an existing IFL cron job" "" \
    "Delete an IFL cron job" "" 2> /tmp/CRONselect
    if [ ! "$?" = "0" ]; then
        rm -f /tmp/CRONselect 2> /dev/null
	CRONselect=""
    else
	CRONselect=`cat /tmp/CRONselect`
	rm /tmp/CRONselect 2> /dev/null
    fi

    if [ "$CRONselect" = "Exit Menu" ] || [ "$CRONselect" = "" ]; then
	return
    elif [ "$CRONselect" = "Add an IFL cron job" ]; then
	if [ "$njobs" -ge "12" ]; then
	    $dialogpath --no-shadow --title "Max IFL Cron Jobs = 12" --msgbox "\n      IFL cron jobs are limited to a max number of 12" 7 60 2> /dev/null
	else
        $dialogpath --no-shadow --max-input 250 --title "Add an IFL Cron Job" --inputbox "\nEdit the default cron job line below as needed, then press <Enter> to add it. The first 5 columns define when the cron job will run as follows:\n\n
	Column 1: Minutes after the hour (0-59)\n\
	Column 2: Hour of the day (0-23)\n\
	Column 3: Day of the month (1-31)\n\
	Column 4: Month of the year (1-12) or (Jan-Dec)\n\
	Column 5: Day of the week (0-6) or (Sun-Sat)\n\n\
	An asterisk (*) means all possible values for each column are true\n\
	Ranges of values are specified using a hyphen (e.g. 2-4 for 2 thru 4)\n\
	Lists of values are specified with a comma-separated list (e.g. 2,5,11)\n\
	A script name entered at the end of the line will auto-run on boot\n\n\
The default line below is set to run at 5:30 AM each Sunday. \n \n" 25 80  "$defaultcronjobline" 2> /tmp/JOBselect
        if [ "$?" = "0" ]; then
	    let njobs=njobs+1
	    jobarray[$njobs]=`cat /tmp/JOBselect`
	    rm /tmp/JOBselect 2> /dev/null
	    write_jobarray
	fi
	fi
    elif [ "$CRONselect" = "Edit an existing IFL cron job" ]; then
	if [ "$njobs" -gt "0" ]; then
	    case $njobs in
		1)
		    $dialogpath --no-shadow --title "Edit IFL Cron Job" --menu "\n$cronjobtext \n" 11 80 $njobs \
		    "Edit cron job 1" "" 2> /tmp/CRONselect;;
		2)
		    $dialogpath --no-shadow --title "Edit IFL Cron Job" --menu "\n$cronjobtext \n" 13 80 $njobs \
		    "Edit cron job 1" "" \
		    "Edit cron job 2" "" 2> /tmp/CRONselect;;
		3)
		    $dialogpath --no-shadow --title "Edit IFL Cron Job" --menu "\n$cronjobtext \n" 15 80 $njobs \
		    "Edit cron job 1" "" \
		    "Edit cron job 2" "" \
		    "Edit cron job 3" "" 2> /tmp/CRONselect;;
		4)
		    $dialogpath --no-shadow --title "Edit IFL Cron Job" --menu "\n$cronjobtext \n" 17 80 $njobs \
		    "Edit cron job 1" "" \
		    "Edit cron job 2" "" \
		    "Edit cron job 3" "" \
		    "Edit cron job 4" "" 2> /tmp/CRONselect;;
		5)
		    $dialogpath --no-shadow --title "Edit IFL Cron Job" --menu "\n$cronjobtext \n" 19 80 $njobs \
		    "Edit cron job 1" "" \
		    "Edit cron job 2" "" \
		    "Edit cron job 3" "" \
		    "Edit cron job 4" "" \
		    "Edit cron job 5" "" 2> /tmp/CRONselect;;
		6)
		    $dialogpath --no-shadow --title "Edit IFL Cron Job" --menu "\n$cronjobtext \n" 21 80 $njobs \
		    "Edit cron job 1" "" \
		    "Edit cron job 2" "" \
		    "Edit cron job 3" "" \
		    "Edit cron job 4" "" \
		    "Edit cron job 5" "" \
		    "Edit cron job 6" "" 2> /tmp/CRONselect;;
		7)
		    $dialogpath --no-shadow --title "Edit IFL Cron Job" --menu "\n$cronjobtext \n" 23 80 $njobs \
		    "Edit cron job 1" "" \
		    "Edit cron job 2" "" \
		    "Edit cron job 3" "" \
		    "Edit cron job 4" "" \
		    "Edit cron job 5" "" \
		    "Edit cron job 6" "" \
		    "Edit cron job 7" "" 2> /tmp/CRONselect;;
		8)
		    $dialogpath --no-shadow --title "Edit IFL Cron Job" --menu "\n$cronjobtext \n" 25 80 $njobs \
		    "Edit cron job 1" "" \
		    "Edit cron job 2" "" \
		    "Edit cron job 3" "" \
		    "Edit cron job 4" "" \
		    "Edit cron job 5" "" \
		    "Edit cron job 6" "" \
		    "Edit cron job 7" "" \
		    "Edit cron job 8" "" 2> /tmp/CRONselect;;
		9)
		    $dialogpath --no-shadow --title "Edit IFL Cron Job" --menu "\n$cronjobtext \n" 25 80 $njobs \
		    "Edit cron job 1" "" \
		    "Edit cron job 2" "" \
		    "Edit cron job 3" "" \
		    "Edit cron job 4" "" \
		    "Edit cron job 5" "" \
		    "Edit cron job 6" "" \
		    "Edit cron job 7" "" \
		    "Edit cron job 8" "" \
		    "Edit cron job 9" "" 2> /tmp/CRONselect;;
		10)
		    $dialogpath --no-shadow --title "Edit IFL Cron Job" --menu "\n$cronjobtext \n" 25 80 $njobs \
		    "Edit cron job 1" "" \
		    "Edit cron job 2" "" \
		    "Edit cron job 3" "" \
		    "Edit cron job 4" "" \
		    "Edit cron job 5" "" \
		    "Edit cron job 6" "" \
		    "Edit cron job 7" "" \
		    "Edit cron job 8" "" \
		    "Edit cron job 9" "" \
		    "Edit cron job 10" "" 2> /tmp/CRONselect;;
		11)
		    $dialogpath --no-shadow --title "Edit IFL Cron Job" --menu "\n$cronjobtext \n" 25 80 $njobs \
		    "Edit cron job 1" "" \
		    "Edit cron job 2" "" \
		    "Edit cron job 3" "" \
		    "Edit cron job 4" "" \
		    "Edit cron job 5" "" \
		    "Edit cron job 6" "" \
		    "Edit cron job 7" "" \
		    "Edit cron job 8" "" \
		    "Edit cron job 9" "" \
		    "Edit cron job 10" "" \
		    "Edit cron job 11" "" 2> /tmp/CRONselect;;
		12)
		    $dialogpath --no-shadow --title "Edit IFL Cron Job" --menu "\n$cronjobtext \n" 25 80 $njobs \
		    "Edit cron job 1" "" \
		    "Edit cron job 2" "" \
		    "Edit cron job 3" "" \
		    "Edit cron job 4" "" \
		    "Edit cron job 5" "" \
		    "Edit cron job 6" "" \
		    "Edit cron job 7" "" \
		    "Edit cron job 8" "" \
		    "Edit cron job 9" "" \
		    "Edit cron job 10" "" \
		    "Edit cron job 11" "" \
		    "Edit cron job 12" "" 2> /tmp/CRONselect;;
		*)
		    ;;
	    esac
	    if [ "$?" = "0" ]; then
		CRONselect=`cat /tmp/CRONselect`
		rm /tmp/CRONselect 2> /dev/null
		jobedit=""
		for i in `seq 1 $njobs`
		do
		    if [ "$CRONselect" = "Edit cron job $i" ]; then
			jobedit=$i
			break
		    fi
		done
		if [ "$jobedit" != "" ]; then
    		    $dialogpath --no-shadow --max-input 250 --title "Edit IFL Cron Job $jobedit" --inputbox "\nEdit cron job $jobedit below as needed, then press <Enter> to save it. The first 5 columns define when the cron job will run as follows:\n\n
	Column 1: Minutes after the hour (0-59)\n\
	Column 2: Hour of the day (0-23)\n\
	Column 3: Day of the month (1-31)\n\
	Column 4: Month of the year (1-12) or (Jan-Dec)\n\
	Column 5: Day of the week (0-6) or (Sun-Sat)\n\n\
	An asterisk (*) means all possible values for each column are true\n\
	Ranges of values are specified using a hyphen (e.g. 2-4 for 2 thru 4)\n\
	Lists of values are specified with a comma-separated list (e.g. 2,5,11)\n\
	A script name entered at the end of the line will auto-run on boot\n \n" 24 80 "${jobarray[$jobedit]}" 2> /tmp/JOBselect
    		    if [ "$?" = "0" ]; then
			jobarray[$jobedit]=`cat /tmp/JOBselect`
			rm /tmp/JOBselect 2> /dev/null
			write_jobarray
		    fi
		fi
	    fi
	fi
    elif [ "$CRONselect" = "Delete an IFL cron job" ]; then
	if [ "$njobs" -gt "0" ]; then
	    case $njobs in
		1)
		    $dialogpath --no-shadow --title "Delete IFL Cron Job" --menu "\n$cronjobtext \n" 11 80 $njobs \
		    "Delete cron job 1" "" 2> /tmp/CRONselect;;
		2)
		    $dialogpath --no-shadow --title "Delete IFL Cron Job" --menu "\n$cronjobtext \n" 13 80 $njobs \
		    "Delete cron job 1" "" \
		    "Delete cron job 2" "" 2> /tmp/CRONselect;;
		3)
		    $dialogpath --no-shadow --title "Delete IFL Cron Job" --menu "\n$cronjobtext \n" 15 80 $njobs \
		    "Delete cron job 1" "" \
		    "Delete cron job 2" "" \
		    "Delete cron job 3" "" 2> /tmp/CRONselect;;
		4)
		    $dialogpath --no-shadow --title "Delete IFL Cron Job" --menu "\n$cronjobtext \n" 17 80 $njobs \
		    "Delete cron job 1" "" \
		    "Delete cron job 2" "" \
		    "Delete cron job 3" "" \
		    "Delete cron job 4" "" 2> /tmp/CRONselect;;
		5)
		    $dialogpath --no-shadow --title "Delete IFL Cron Job" --menu "\n$cronjobtext \n" 19 80 $njobs \
		    "Delete cron job 1" "" \
		    "Delete cron job 2" "" \
		    "Delete cron job 3" "" \
		    "Delete cron job 4" "" \
		    "Delete cron job 5" "" 2> /tmp/CRONselect;;
		6)
		    $dialogpath --no-shadow --title "Delete IFL Cron Job" --menu "\n$cronjobtext \n" 21 80 $njobs \
		    "Delete cron job 1" "" \
		    "Delete cron job 2" "" \
		    "Delete cron job 3" "" \
		    "Delete cron job 4" "" \
		    "Delete cron job 5" "" \
		    "Delete cron job 6" "" 2> /tmp/CRONselect;;
		7)
		    $dialogpath --no-shadow --title "Delete IFL Cron Job" --menu "\n$cronjobtext \n" 23 80 $njobs \
		    "Delete cron job 1" "" \
		    "Delete cron job 2" "" \
		    "Delete cron job 3" "" \
		    "Delete cron job 4" "" \
		    "Delete cron job 5" "" \
		    "Delete cron job 6" "" \
		    "Delete cron job 7" "" 2> /tmp/CRONselect;;
		8)
		    $dialogpath --no-shadow --title "Delete IFL Cron Job" --menu "\n$cronjobtext \n" 25 80 $njobs \
		    "Delete cron job 1" "" \
		    "Delete cron job 2" "" \
		    "Delete cron job 3" "" \
		    "Delete cron job 4" "" \
		    "Delete cron job 5" "" \
		    "Delete cron job 6" "" \
		    "Delete cron job 7" "" \
		    "Delete cron job 8" "" 2> /tmp/CRONselect;;
		9)
		    $dialogpath --no-shadow --title "Delete IFL Cron Job" --menu "\n$cronjobtext \n" 25 80 $njobs \
		    "Delete cron job 1" "" \
		    "Delete cron job 2" "" \
		    "Delete cron job 3" "" \
		    "Delete cron job 4" "" \
		    "Delete cron job 5" "" \
		    "Delete cron job 6" "" \
		    "Delete cron job 7" "" \
		    "Delete cron job 8" "" \
		    "Delete cron job 9" "" 2> /tmp/CRONselect;;
		10)
		    $dialogpath --no-shadow --title "Delete IFL Cron Job" --menu "\n$cronjobtext \n" 25 80 $njobs \
		    "Delete cron job 1" "" \
		    "Delete cron job 2" "" \
		    "Delete cron job 3" "" \
		    "Delete cron job 4" "" \
		    "Delete cron job 5" "" \
		    "Delete cron job 6" "" \
		    "Delete cron job 7" "" \
		    "Delete cron job 8" "" \
		    "Delete cron job 9" "" \
		    "Delete cron job 10" "" 2> /tmp/CRONselect;;
		11)
		    $dialogpath --no-shadow --title "Delete IFL Cron Job" --menu "\n$cronjobtext \n" 25 80 $njobs \
		    "Delete cron job 1" "" \
		    "Delete cron job 2" "" \
		    "Delete cron job 3" "" \
		    "Delete cron job 4" "" \
		    "Delete cron job 5" "" \
		    "Delete cron job 6" "" \
		    "Delete cron job 7" "" \
		    "Delete cron job 8" "" \
		    "Delete cron job 9" "" \
		    "Delete cron job 10" "" \
		    "Delete cron job 11" "" 2> /tmp/CRONselect;;
		12)
		    $dialogpath --no-shadow --title "Delete IFL Cron Job" --menu "\n$cronjobtext \n" 25 80 $njobs \
		    "Delete cron job 1" "" \
		    "Delete cron job 2" "" \
		    "Delete cron job 3" "" \
		    "Delete cron job 4" "" \
		    "Delete cron job 5" "" \
		    "Delete cron job 6" "" \
		    "Delete cron job 7" "" \
		    "Delete cron job 8" "" \
		    "Delete cron job 9" "" \
		    "Delete cron job 10" "" \
		    "Delete cron job 11" "" \
		    "Delete cron job 12" "" 2> /tmp/CRONselect;;
		*)
		    ;;
	    esac
	    if [ "$?" = "0" ]; then
		CRONselect=`cat /tmp/CRONselect`
		rm /tmp/CRONselect 2> /dev/null
		jobdelete=""
		for i in `seq 1 $njobs`
		do
		    if [ "$CRONselect" = "Delete cron job $i" ]; then
			jobdelete=$i
			jobarray[$i]=""
			break
		    fi
		done
		if [ "$jobdelete" != "" ]; then
		    write_jobarray
		fi
	    fi
	fi
    fi
done
}

get_cron_jobs() {
    njobs=0
    if [ ! -f /etc/cron.d/iflcron ]; then return; fi
    cat /etc/cron.d/iflcron | grep " root " | grep "iflreboot" > iflcron.tmp
    while read line
    do
	let njobs=njobs+1
	jobarray[$njobs]=$line
    done < "iflcron.tmp"
    rm iflcron.tmp 2> /dev/null
}

write_jobarray() {
    firstline=0
    for i in `seq 1 $njobs`
    do
	if [ "${jobarray[$i]}" != "" ]; then
	    if [ "$firstline" = "0" ]; then
		echo "${jobarray[$i]}" > /etc/cron.d/iflcron
		firstline=1
	    else
		echo "${jobarray[$i]}" >> /etc/cron.d/iflcron
	    fi
	fi
    done
    chown 0:0 /etc/cron.d/iflcron
    chmod 644 /etc/cron.d/iflcron
    if [ "$jobdelete" != "" ]; then
	jobdelete=""
	let njobs=njobs-1
    fi
    if [ "$njobs" = "0" ]; then rm /etc/cron.d/iflcron 2> /dev/null; fi
}

###############################################################################
# script execution starts here
###############################################################################

if [ -f /tbu/utility/running ]; then
    echo "The makeGRUB script is intended to be used when running from an installed"
    echo "Linux distribution for the purpose of adding IFL to the Grub menu. It is"
    echo "not supported when running from the IFL Boot Disk. Exiting script."
    echo
    exit 1
fi

dialogpath=`which dialog 2> /dev/null`
if [ "$?" != "0" ]; then
    echo "The dialog program is required by this script to display the menus. This"
    echo "can usually be corrected by installing the 'dialog' package."
    echo
    echo "Exiting script..."
    echo
    exit 1
fi

startup_initialize
sleep 1

while [ 0 ]; do
    if [ "$grubversion" = "2" ]; then
	main_menu_grub2
    else
	main_menu_grub1
    fi
    if [ "$GRUBmselect" = "Exit Menu" ] || [ "$GRUBmselect" = "" ]; then
	break
    elif [ "$GRUBmselect" = "Create Grub2 boot files for IFL" ]; then
	create_boot_files
	if [ "$?" = "0" ]; then 
	    $dialogpath --no-shadow --title  "makeGRUB - Install Grub2 Files" --menu "\nThe Grub2 boot files have been successfully created, but not yet installed. Select 'Install Grub2 boot files for IFL' to install them now, or select 'Cancel' to do it later from the menu.\n\nInstalling the Grub2 files will add IFL to the Grub2 boot menu, or if already installed, will update the previously installed files.\n \n" 16 75 1\
	    "Install Grub2 boot files for IFL" "" 2> /tmp/G2PROMPTmselect
	    if [ ! "$?" = "0" ]; then
    		rm -f /tmp/G2PROMPTmselect 2> /dev/null
	    else
		rm /tmp/G2PROMPTmselect 2> /dev/null
		install_grub2_boot_files
	    fi
	fi
    elif [ "$GRUBmselect" = "Create Grub Legacy boot files for IFL" ]; then
	create_boot_files
	if [ "$?" = "0" ]; then 
	    $dialogpath --no-shadow --title  "makeGRUB - Install Grub Legacy Files" --menu "\nThe Grub Legacy boot files have been successfully created, but not yet installed. Select 'Install Grub Legacy boot files for IFL' to install them now, or select 'Cancel' to do it later from the menu.\n\nInstalling the Grub Legacy files will add IFL to the Grub boot menu, or if already installed, will update the previously installed files.\n \n" 16 75 1\
	    "Install Grub Legacy boot files for IFL" "" 2> /tmp/GLPROMPTmselect
	    if [ ! "$?" = "0" ]; then
    		rm -f /tmp/GLPROMPTmselect 2> /dev/null
	    else
		rm /tmp/GLPROMPTmselect 2> /dev/null
		install_grub1_boot_files
	    fi
	fi
    elif [ "$GRUBmselect" = "Install Grub2 boot files for IFL" ]; then
	install_grub2_boot_files
    elif [ "$GRUBmselect" = "Install Grub Legacy boot files for IFL" ]; then
	install_grub1_boot_files
    elif [ "$GRUBmselect" = "Schedule IFL cron jobs" ]; then
	manage_cron_jobs
    elif [ "$GRUBmselect" = "Uninstall Grub2 boot files for IFL" ]; then
	uninstall_grub2_boot_files
    elif [ "$GRUBmselect" = "Uninstall Grub Legacy boot files for IFL" ]; then
	uninstall_grub1_boot_files
    else
	break
    fi
done
clear
