Main:
	call PrintInitialMessage
	length #temp
	iflt #temp, 7, LOLEasterEgg
	subtract #temp, SAVE_SIZE
	iflt #temp, 0x40, .savefile_patch
	call DetectBase
	call SelectTarget
	callnz #source, ConvertCrystalVersion
	call ApplyPrismPatch
	callnz #target, MakeDebugVersion
	length #length
	ifne #length, ROM_SIZE, PatchingError
	multiply #temp, #target, HASH_SIZE
	add #temp, PrismHashes
	checksha1 #result, #temp
	jumpnz #result, PatchingError
	print PatchingCompletedMessage
	print .patch_savefile_reminder
	exit #zero

.savefile_patch
	print .savefile_string
	menu #result, YesNoMenu
	jumpz #result, PatchSavefile
	exit 1

.patch_savefile_reminder
	string "Remember that, if you want to keep your old savefile, you will have to patch it as well. You can use this patch for that purpose too."
.savefile_string
	string "The input file seems to be a savefile. Do you want to patch a savefile to the latest version?"

PatchingError:
	print .message
	exit 1

.message
	string "An error occurred while applying the patch. Please ensure that the base ROM and the patch are correct and try again."

PatchingCompletedMessage:
	string "Patching completed!"

PrintInitialMessage:
	bufstring .pokemon_prism_string
	add #address, VersionInfo, 2
	getbyteinc #temp, #address
	bufnumber #temp
	bufchar 0x2e ;0x2e is ASCII for '.'
	getbytedec #temp, #address
	bufnumber #temp
	decrement #address
	bufstring .comma_build_string
	; build number is big endian
	getbytedec #1, #address
	getbyte #temp, #address
	mulacum #1, #temp, 0x100
	set #2, 4
	call PrintWithLeadingZeros
	bufstring .parenthesis_asterisks_string
	printbuf
	return
	
.pokemon_prism_string
	string "*** Pokémon Prism patch (version "
.comma_build_string
	string ", build "
.parenthesis_asterisks_string
	string ") ***"
