;  This will do patches for ADPro 2.1.5

; This procedure will patch a file
; requires initialization of variables:
;    patch_type
;    patch_thisfile
;    patch_dir
;    patch_curnum
;    patch_maxnum
;    patch_cksum_0
;    patch_cksum_1
;    patch_cksum_2
;    patch_cksum_3

(procedure patch_file
	(if (= 1 (exists (tackon patch_dir patch_thisfile)))
		(
			(working ("Updating your %s %s...\n\nPlease read the ReadMe file on the ADPro Disk #1 after completing installation." patch_thisfile patch_type))
			(set fsum (getsum (tackon patch_dir patch_thisfile)))
			(while (< patch_curnum patch_maxnum)
				(
					(set tsum (select patch_curnum patch_cksum_0 patch_cksum_1 patch_cksum_2 patch_cksum_3 patch_cksum_4 patch_cksum_5 0))
					(if (= tsum fsum)
						(
							(run ("RAM:lz -N -wRam: x ADPRO2.D3:%s_Patches.lzh %s.%ld.PCH RAM:" patch_type patch_thisfile patch_curnum))
							(set rline (cat "RAM:lpatch " ("-o%s " (tackon "RAM:" patch_thisfile)) ("-p%s.%ld.PCH \"%s\"" (tackon "RAM:" patch_thisfile) patch_curnum (tackon patch_dir patch_thisfile))))
							(if (= 0 (run rline))
								(
									(protect (tackon patch_dir patch_thisfile) "+rwed")
									(copyfiles
										(prompt "")
										(help "")
										(source (tackon "RAM:" patch_thisfile))
										(dest patch_dir)
									)
									(delete (tackon "RAM:" patch_thisfile))
									(delete ("RAM:%s.%ld.PCH" patch_thisfile patch_curnum))
									(set patch_curnum (+ patch_maxnum 1))
								)
							)
						)
					)
				)
				(set patch_curnum (+ patch_curnum 1))
			)
			(if
				(= patch_curnum patch_maxnum)
				(message
					("The attempt to patch the %s %s failed.\n\n" patch_thisfile patch_type)
					"This may have happened because the file has already been patched "
					"or the file is corrupted.  Please install the module(s) again and retry the patch."
				)
			)
		)
		(
			(message
				("The %s %s " patch_thisfile patch_type)
				("did not exist in your %ss2 directory.\n\n" patch_type)
				"Please install the module again and retry this patch."
			)
		)
	)
)

(set @default-dest
	(askdir
		(prompt "Please select directory into which you have installed ADPro.")
		(help
			"You must select the directory into which Art Department Professional was installed.\n\n"
			@askdir-help
		)
		(default "SYS:ADPro")
	)
)

(set loaddir (tackon @default-dest "Loaders2"))
(if
	(= (exists loaddir) 0)
	(abort "Loaders2 Directory Does Not Exist.")
)

(set savedir (tackon @default-dest "Savers2"))
(if
	(= (exists savedir) 0)
	(abort "Savers2 Directory Does Not Exist.")
)

(copylib
	(prompt "Copying arp.library to LIBS:")
	(help
		"This library is required to install ADPro.  If you already have a copy of "
		"this library, the library included in this install is an update to the "
		"library you currently have installed.\n\n"
		@copylib-help
	)
	(source "ADPRO2.D3:libs/arp.library")
	(dest "LIBS:")
	(confirm)
)

(copyfiles
	(prompt "" )
	(help "")
	(source "ADPRO2.D3:C/lpatch")
	(dest "RAM:")
)
(if
	(= 0 (exists "RAM:lpatch"))
	(abort "Unable to copy necessary files to Ram Disk."
		 "  Please make sure you have at least 100K of memory"
		 " available before installing."
	)
)

(copyfiles
	(prompt "" )
	(help "")
	(source "ADPRO2.D3:C/lz")
	(dest "RAM:")
)
(if
	(= 0 (exists "RAM:lz"))
	(abort "Unable to copy necessary files to Ram Disk."
		 "  Please make sure you have at least 100K of memory"
		 " available before installing."
	)
)

(set ptodo
	(askoptions
		(prompt "Select previously installed expansion modules to patch?")
		(help 
			"In addition to the basic ADPro system, ASDG offers several expansion "
			"modules giving you added capabilities.  These expansion modules include "
			"scanner controllers, film recorder controllers, and the ability to read and "
			"write additional file formats. "
			"\n\n"
			"If you are upgrading from ADPro 1.0.3 or earlier, you might already own some "
			"of our expansion modules. If so, read on. "
			"\n\n"
			"If you don't own any of the expansion modules now, you can skip this "
			"step but before you do so, please read the following paragraphs. "
			"\n\n"
			"If ADPro 2.1 is your first version of ADPro, or you don't own any "
			"expansion modules now, it is possible that when you purchase expansion "
			"modules in the future, you may get an older version.  If this happens, "
			"ADPro 2.1 will alert you with a message when you try to execute an "
			"expansion module designed for a previous version of ADPro. "
			"\n\n"
			"If you find that you have purchased an older expansion module, you can "
			"re-execute this installation program and select only this step to apply the "
			"patches contained here. "
			"\n\n"
			"If you don't need to perform any of the patches at this time, you can stop "
			"reading now. "
			"\n\n"
			"The modules which this sequence can patch include:  JXSCAN, JX100, ES300C, "
			"PHOVOS300C, SV6510, CI3000, and the Professional Conversion Pack (Targa, "
			"Tiff, and Rendition). "
			"\n\n"
			"To have these patches work, the modules to be patched must be installed in "
			"their proper places in the directory in which you just installed ADPro 2.1. "
			"This may mean that you have to re-install your expansion modules where you "
			"just installed ADPro 2.1.  If so, exit this sequence for now, re-install "
			"the expansion modules, execute this sequence again and select only "
			"this last step. "
			"\n\n"
			"If the expansion modules to be patched are already located in the Loaders2 "
			"and Savers2 directory where you just installed ADPro 2.1, then you may proceed "
			"with the sequence. "
			"\n\n"
			"If you have additional difficulties or questions, call ASDG at (608) 273 "
			"6585 and have your ADPro serial number ready. "
		)
		(choices
			"Pro Conversion Pack"
			"JX100"
			"JXSCAN"
			"EPSON_ES300C"
			"PHOVOS_300C"
			"CI3000"
			"SV6510"
		)
		(default 1)
	)
)
(if (= ptodo 0)
	(abort "You didn't select anything, so there's nothing to do.")
)

; this patches optional loaders
(set patch_cksum_0 0)
(set patch_cksum_1 0)
(set patch_cksum_2 0)
(set patch_cksum_3 0)
(set patch_cksum_4 0)
(set patch_cksum_5 0)
(set @execute-dir "RAM:")
(if (IN ptodo 0)
	(
		; patching RENDITION Loader
		(set patch_thisfile "RENDITION")
		(set patch_dir loaddir)
		(set patch_type "Loader")
		(set patch_curnum 0)
		(set patch_maxnum 3)
		(set patch_cksum_0 -784668211)
		(set patch_cksum_1 1509760538)
		(set patch_cksum_2 -1088099858)
		(patch_file)
		;patching TARGA Loader
		(set patch_thisfile "TARGA")
		(set patch_dir loaddir)
		(set patch_type "Loader")
		(set patch_curnum 0)
		(set patch_maxnum 6)
		(set patch_cksum_0 1482780211)
		(set patch_cksum_1 -483001049)
		(set patch_cksum_2 244623600)
		(set patch_cksum_3 -1058239812)
		(set patch_cksum_4 -1058239812)
		(set patch_cksum_5 828958717)
		(patch_file)
		;patching TIFF Loader
		(set patch_thisfile "TIFF")
		(set patch_dir loaddir)
		(set patch_type "Loader")
		(set patch_curnum 0)
		(set patch_maxnum 4)
		(set patch_cksum_0 859597812)
		(set patch_cksum_1 819297291)
		(set patch_cksum_2 1518344491)
		(set patch_cksum_3 -1641822233)
		(patch_file)
		(working "Creating X Loader...\n\nPlease read the ReadMe file on the ADPro Disk #1 after completing installation.")
		(run "RAM:lz -N -wRam: x ADPRO2.D3:Loader_Patches.lzh X.0.PCH RAM:")
		(set rline ("RAM:lpatch -oRAM:X -pRAM:X.0.PCH \"%s\"" (tackon loaddir "RENDITION")))
		(if (= 0 (run rline))
			(
				(copyfiles
					(prompt "")
					(help "")
					(source "RAM:X")
					(dest patch_dir)
				)
				(delete "RAM:X")
				(delete "RAM:X.0.PCH")
			)
		)
		(working "Creating SUNRASTER Loader...\n\nPlease read the ReadMe file on the ADPro Disk #1 after completing installation.")
		(run "RAM:lz -N -wRam: x ADPRO2.D3:Loader_Patches.lzh SUNRASTER.0.PCH RAM:")
		(set rline ("RAM:lpatch -oRAM:SUNRASTER -pRAM:SUNRASTER.0.PCH \"%s\"" (tackon loaddir "RENDITION")))
		(if (= 0 (run rline))
			(
				(copyfiles
					(prompt "")
					(help "")
					(source "RAM:SUNRASTER")
					(dest patch_dir)
				)
				(delete "RAM:SUNRASTER")
				(delete "RAM:SUNRASTER.0.PCH")
			)
		)
		;patching RENDITION Saver
		(set patch_thisfile "RENDITION")
		(set patch_dir savedir)
		(set patch_type "Saver")
		(set patch_curnum 0)
		(set patch_maxnum 3)
		(set patch_cksum_0 -1958352664)
		(set patch_cksum_1 1939121335)
		(set patch_cksum_2 1418874756)
		(patch_file)
		;patching TARGA Saver
		(set patch_thisfile "TARGA")
		(set patch_dir savedir)
		(set patch_type "Saver")
		(set patch_curnum 0)
		(set patch_maxnum 3)
		(set patch_cksum_0 242601535)
		(set patch_cksum_1 -2024131800)
		(set patch_cksum_2 -1841823675)
		(patch_file)
		;patching TIFF Saver
		(set patch_thisfile "TIFF")
		(set patch_dir savedir)
		(set patch_type "Saver")
		(set patch_curnum 0)
		(set patch_maxnum 4)
		(set patch_cksum_0 1720074818)
		(set patch_cksum_1 -1977315916)
		(set patch_cksum_2 -1780612422)
		(set patch_cksum_3 1124297027)
		(patch_file)
		(working "Creating X Saver...\n\nPlease read the ReadMe file on the ADPro Disk #1 after completing installation.")
		(run "RAM:lz -N -wRam: x ADPRO2.D3:Saver_Patches.lzh X.0.PCH RAM:")
		(set rline ("RAM:lpatch -oRAM:X -pRAM:X.0.PCH \"%s\"" (tackon savedir "RENDITION")))
		(if (= 0 (run rline))
			(
				(copyfiles
					(prompt "")
					(help "")
					(source "RAM:X")
					(dest patch_dir)
				)
				(delete "RAM:X")
				(delete "RAM:X.0.PCH")
			)
		)
		(working "Creating SUNRASTER Saver...\n\nPlease read the ReadMe file on the ADPro Disk #1 after completing installation.")
		(run "RAM:lz -N -wRam: x ADPRO2.D3:Saver_Patches.lzh SUNRASTER.0.PCH RAM:")
		(set rline ("RAM:lpatch -oRAM:SUNRASTER -pRAM:SUNRASTER.0.PCH \"%s\"" (tackon savedir "RENDITION")))
		(if (= 0 (run rline))
			(
				(copyfiles
					(prompt "")
					(help "")
					(source "RAM:SUNRASTER")
					(dest patch_dir)
				)
				(delete "RAM:SUNRASTER")
				(delete "RAM:SUNRASTER.0.PCH")
			)
		)
	)
)

(if (IN ptodo 1)
	(
		; patching JX100 Loader
		(set patch_thisfile "JX100")
		(set patch_dir loaddir)
		(set patch_type "Loader")
		(set patch_curnum 0)
		(set patch_maxnum 4)
		(set patch_cksum_0 -774386942)
		(set patch_cksum_1 -1457757454)
		(set patch_cksum_2 1395406761)
		(set patch_cksum_3 -648704505)
		(patch_file)
	)
)

(if (IN ptodo 2)
	(
		; patching JXSCAN Loader
		(set patch_thisfile "JXSCAN")
		(set patch_dir loaddir)
		(set patch_type "Loader")
		(set patch_curnum 0)
		(set patch_maxnum 3)
		(set patch_cksum_0 919621839)
		(set patch_cksum_1 1052076838)
		(set patch_cksum_2 1434661152)
		(patch_file)
	)
)

(if (IN ptodo 3)
	(
		; patching EPSON_ES300C Loader
		(set patch_thisfile "EPSON_ES300C")
		(set patch_dir loaddir)
		(set patch_type "Loader")
		(set patch_curnum 0)
		(set patch_maxnum 3)
		(set patch_cksum_0 539717622)
		(set patch_cksum_1 477972192)
		(set patch_cksum_2 -1329441256)
		(patch_file)
	)
)

(if (IN ptodo 4)
	(
		; patching PHOVOS_300C Loader
		(set patch_thisfile "PHOVOS_300C")
		(set patch_dir loaddir)
		(set patch_type "Loader")
		(set patch_curnum 0)
		(set patch_maxnum 3)
		(set patch_cksum_0 2114945872)
		(set patch_cksum_1 -2119905465)
		(set patch_cksum_2 1503818038)
		(patch_file)
	)
)

(if (IN ptodo 5)
	(
		; patching CI3000 Saver
		(set patch_thisfile "CI3000")
		(set patch_dir savedir)
		(set patch_type "Saver")
		(set patch_curnum 0)
		(set patch_maxnum 3)
		(set patch_cksum_0 -352502906)
		(set patch_cksum_1 -1846595266)
		(set patch_cksum_2 -1212497233)
		(patch_file)
	)
)

(if (IN ptodo 6)
	(
		; patching SV6510 Saver
		(set patch_thisfile "SV6510")
		(set patch_dir savedir)
		(set patch_type "Saver")
		(set patch_curnum 0)
		(set patch_maxnum 2)
		(set patch_cksum_0 1138320904)
		(set patch_cksum_1 -776414085)
		(patch_file)
	)
)

(if (= 1 (exists "RAM:lpatch"))
	(delete "RAM:lpatch")
)
(if (= 1 (exists "RAM:lz"))
	(delete "RAM:lz")
)
