#!/bin/bash

main ()
{
	tempDir="${destinationDrive}/.Chameleon/"
	echo "DEBUG: tempDir is ${tempDir}"


	if [ ! -d "${tempDir}" ]; then
		echo "the temporary directory does not exist so no boot options have been selected"
	else
		# check to see if there are any boot options in the temporary directory

		cd "${tempDir}"
		Dirlist=$(ls | grep "@")

		if [ "${Dirlist}" ]; then

			echo "==============================================="
			echo "set com.apple.Boot.plist variables"
			echo "**********************************"

			bootplist="${tempDir}Extra/com.apple.Boot.plist"
			echo "DEBUG: bootplist = ${bootplist}"	

			systemplist="/Library/Preferences/SystemConfiguration/com.apple.Boot.plist"
			echo "DEBUG: systemplist= ${systemplist}"

			echo "if no directory exists named ${bootplist%/*} then mkdir ${bootplist%/*}"
			[ ! -d "${bootplist%/*}" ] && mkdir -p "${bootplist%/*}"

			if [ ! -f "${bootplist}" ]; then
				if [ -f "${systemplist}" ]; then
					echo "Using Apple's com.apple.Boot.plist as base for /Extra/com.apple.Boot.plist"
					cp  "${systemplist}" "${bootplist}"
				fi
			fi
			echo "-----------------------------------------------"
			echo ""


			echo "==============================================="
			echo "Build Extra/com.apple.Boot.plist"
			echo "********************************"

			mv "${bootplist}" "${bootplist}.orig"

			#prefunctionifs="${IFS}"

			fileinput="${bootplist}.orig"
			fileoutput="${bootplist}"
			find="<key>Kernel Flags</key>"
			replace=""
			deletelines=""
			insertlinesskip="2"

			matchlinefound="0"

			while read line
			do
			{
				if [ ! "${line}" = "${line/${find}/}" ]; then
				{
				# Trim the longest match from the end for <*
				xmlelementindent="${line%%<*}"

				# Trim the longest match from the start for *<
				xmlelementtemp="${line#*<}"
				# Add back in stripped <
				xmlelement="<${xmlelementtemp}"

				# Trim the shortest match from the start for <
				xmltagtemp="${xmlelement#<}"
				# Trim the longest match from the end for >*
				xmltag="${xmltagtemp%%>*}"

				# Trim the shortest match from the start for *>
				xmltexttemp="${xmlelement#*>}"
				# Trim the longest match from the end for <*
				xmltext="${xmltexttemp%%<*}"

				if [ "${replace}" ]; then
					echo "${xmlelementindent}<${xmltag}>${replace}</${xmltag}>" >>"${fileoutput}"
				else
					echo "${line}" >>"${fileoutput}"
				fi

				matchlinefound="1"

				}
				else
				{

				if [ "${insertlinesskip}" ] && [ "${matchlinefound}" -gt 0 ] && [ "${matchlinefound}" -le "${insertlinesskip}" ]; then
					if [ "${matchlinefound}" = "${insertlinesskip}" ]; then

						for bootOptionFile in $Dirlist ; do
							overide=$(echo $bootOptionFile | sed 's/_/ /g' | tr -d "./@" |cut -d '+' -f1)
							string=$(echo $bootOptionFile | tr -d "./@" |cut -d'+' -f2)

							bootoptionshdextra[1]="<key>${overide}</key>"
							bootoptionshdextra[2]="<string>${string}</string>"

							echo "Adding ${overide}=${string} to ${bootplist}"
	 						array=("${bootoptionshdextra[@]}")
							arraysize=0

							#prefunctionifs="${IFS}"
							#IFS="\n"

							while [ ${arraysize} -lt ${#array[@]} ];
							do
								echo "${xmlelementindent}${array[${arraysize}]}" >>"${fileoutput}"
			       					let arraysize="${arraysize}+1"
							done

							#IFS=${prefunctionifs}
						done

						#echo "set owner privileges for ${bootplist}"
						chown "${USER}:20" "${bootplist}"
					fi
				fi

				if [ "${deletelines}" ] && [ "${matchlinefound}" -gt 0 ] && [ "${matchlinefound}" -lt $((${deletelines}+${deletelinesskip})) ] && [ "${matchlinefound}" -ge ${deletelinesskip} ]; then
					:
				else
					echo "${line}" >>"${fileoutput}"
				fi

				if [ "${matchlinefound}" -gt 0 ]; then
					let matchlinefound="${matchlinefound}+1"
				fi

				}
				fi
				}
			done < "${fileinput}"

			#IFS=${prefunctionifs}
			rm -f "${fileinput}"

			echo ""

			echo "-----------------------------------------------"
			echo ""
		

			echo "==============================================="
			echo "Clean up and Remove Boot Option Null Files"
			echo "******************************************"

			# make sure we're in the right directory
			cd "${tempDir}"

			Dirlist=$(find . -type f | grep "@")
			for bootOptionFile in ${Dirlist} ; do
				echo "Removing temporary boot option file: ${bootOptionFile}"
				rm $bootOptionFile
			done

			echo "-----------------------------------------------"
			echo ""

		else
			echo "No boot options in the temporary directory."
		fi
	fi
}



echo "==============================================="
echo "Enter Build Boot Options script"
echo "*******************************"

# get the passed destination drive?

if [ "$#" -eq 1 ]; then
	destinationDrive="$1"
	echo "DEBUG: passed argument for destinationDrive = $destinationDrive"
fi

# Build Boot Options
main "${1}" "${2}" "${3}" "${4}"

exit 0
