###############################################################################
#
# Open iT Mould script for installing patch
#


# Includes
include configure.oms
include prepare.oms
include log.oms
include verify.oms
include runinstall.oms
include backup.oms
include rollback.oms
include changelog.oms
include xml.oms
include conf.oms
include linepatching.oms
include version.oms

# Globals
string waitbeforexit
default waitbeforexit "yes"
map configfiles configfiles2 textfiles



# TODO: Write log of binaries and other files, where each changed file is listed with patch number
# TODO: Scripts and other issues cannot run twice and destroy something


# Purpose: Main entry point
function main

	# Using a loop for quick exit
	while true

		# Prepare patch commands
		call "prepareCommands"
		if haveerr
			break
		end

		# Get details about target system
		call "getSystemDetails"
		if haveerr
			break
		end

		# Get installed patches
		call "getInstalledPatches"
		if haveerr
			break
		end

		# Get patch requrements
		call "loadReqs"
		if haveerr
			break
		end

		# Prepare system for patch installation
		call "prepareSystem"
		if haveerr
			break
		end

		if ( ! patchinstalled )

			# Read patch definition file
			call "readPatchDef"
			if haveerr
				break
			end

			# Verify that we can install
			call "verifyInstall"
			if haveerr
				break
			end

			# Load change log
			call "loadChangelog"
			if haveerr
				break
			end

			# Ready
			call "info" ( "Installing patch #" . patchid )

			# Run install
			call "install"
			if haveerr
				break
			end

		end
		else
			exit 21
		end

		# Not actually looping
		break

	end

	# If all went well this far, and we have configuration files open, save
	# them now
	if ( ! haveerr & &sizeof configfiles > 0 )

		# We have configuration files to save
		string fname pdata
		number readonly
		mapgetfirst configfiles fname pdata
		while success

			# Read only?
			mapget configfiles2 fname readonly
			if ( ! readonly )

				# No
				# Save
				string func
				strpatternmatch fname "*.xml"
				if success
					eval ( func = "saveXML" )
				end
				else
					eval ( func = "saveConf" )
				end
				call func pdata fname
				if ( ! success )
					call "error" ( "Failed to save patched configuration file \"" . fname . "\"" )
				end

			end

			# Next file
			mapgetnext configfiles fname pdata
		end

	end
	
	# Save the text files if any
	if ( ! haveerr & &sizeof textfiles > 0 )
		string fname pdata
		mapgetfirst textfiles fname pdata
		while success

			call "saveText" pdata fname
			if ( ! success )
				call "error" ( "Failed to save patched text file \"" . fname . "\"" )
			end

			# Next file
			mapgetnext textfiles fname pdata
		end
	end

	# Success or failure?
	if haveerr
		if ( ! patchinstalled )
			call "warning" "The patch failed to install!"
			call "rollback"
			if ( ! success )
				call "error" "Failed to roll back installation. Sorry..."
			end
		end
	end
	else
		call "setRegistryVersion" patchid
		call "writeNewVersionFile" patchid
		
		deldir ( patchdir . "/backup" )
		call "info" "The patch was successfully installed!"
		call "saveChangelog"
		call "logEnd"
	end

	if ( waitbeforexit == "yes" )
		echo "Press Enter to exit installation"
		waitkey
	end
	if haveerr
		if patchinstalled
			exit 21
		end
		exit 20
	end

end
