; ************************************************
; *
; * $VER: Install-Patches v2.3.0b (25.03.93)
; *
; * This is the installation script that will patch the following modules:
; *
; *	Professional Conversion Pack (PICT, RENDITION, SUNRASTER, TARGA, TIFF, X)
; *	EPSON_SCAN (previously called EPSON_ES300C)
; *
; * for ASDG's ADPro or MorphPlus.
; *
; * Copyright © 1992-1993 ASDG Incorporated  All Rights Reserved
; *
; ************************************************


; ****************************
; * Set up some global variables.
; ****************************
;
(set OSVersion		(/ (getversion) 65536))
(set TotalFiles		0)
(set AccumFiles		0)
(set PctDone		0)

(set Disk4Name			"ADPro_D4")	; actual name of disk 4
(set DiskPatches		Disk4Name)	; patch disk




(procedure setPkgOpts
;
; ****************************
; * Ask the user which parts of the package should be installed.
; ****************************
;
	(
	(set PkgOpts
		(askoptions
			(prompt	"Select the previously installed expansion modules to patch")
			(help	"In addition to the basic ADPro/MorphPlus 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.3.0 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.3.0 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"
				"If you don't need to perform any of the patches at this time, you can stop "
				"reading now.\n"
				"\n"
				"*** Professional Conversion Pack (PCP)\n"
				"This option lets you patch the TIFF, TARGA, RENDITION, SUNRASTER, X, and PICT "
				"loader and saver modules.\n"
				"\n"
				"*** EPSON_SCAN / EPSON_ES300C\n"
				"This option lets you patch the EPSON_ES300C loader module to the "
				"EPSON_SCAN version (it will be renamed).  If you are using the older "
				"EPSON_ES300C_030 modules, you MUST reinstall the EPSON_ES300C "
				"version and delete the EPSON_ES300C_030 version before attempting "
				"the patch.\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.3.0 "
				"(or MorphPlus).  This may mean that you have to re-install your expansion "
				"modules where you just installed ADPro or MorphPlus.  If so, exit this "
				"patch installation process for now, re-install the expansion modules, "
				"execute this patch install 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.3.0 (or MorphPlus), "
				"then you may proceed with the patch process.\n"
				"\n"
				"If you have additional difficulties or questions, call ASDG at the phone "
				"number listed in your manual and please have your ADPro or MorphPlus serial "
				"number handy.  Thank you.\n"
				"\n"
				@askoptions-help)
			(choices
				; bit position 0 -- todo & 1
				;
				"Professional Conversion Pack (PCP)"

				; bit position 1 -- todo & 2
				;
				"EPSON_SCAN / EPSON_ES300C Loader"
			)
		)
	)
	)
)


(procedure patchFile
;
; ****************************
; * Patch a file.
; ****************************
;
; these variables must be defined before calling this procedure:
;    patch_type
;    patch_thisfile
;    patch_newname
;    patch_dir
;    patch_curnum
;    patch_maxnum
;    patch_cksum_0
;    patch_cksum_1
;    patch_cksum_2
;    patch_cksum_3
;    patch_cksum_4
;    patch_cksum_5
;
	(if (= 1 (exists (tackon patch_dir patch_thisfile)))
		(
		(working ("Updating your %s %s...\n\nPlease read the ReadMe file after completing the installation." patch_thisfile patch_type))

		(set fsum (getsum (tackon patch_dir patch_thisfile)))
		(set patched FALSE)
		(while (AND (<= patch_curnum patch_maxnum) (NOT patched))
			(
			(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 %s:%ssPatches.lha %s.%ld.PCH RAM:" DiskPatches patch_type patch_thisfile patch_curnum))
				(set patchCmd (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 patchCmd))
					(
					(protect (tackon patch_dir patch_thisfile) "+rwed")
					(copyfiles
						(prompt "")
						(help "")
						(source (tackon "RAM:" patch_thisfile))
						(dest patch_dir)
						(newname patch_newname)
					)
					(delete (tackon "RAM:" patch_thisfile))
					(delete ("RAM:%s.%ld.PCH" patch_thisfile patch_curnum))
					(set patched TRUE)
					)
				)
				)
			)
			)
			(set patch_curnum (+ patch_curnum 1))
		)
		(if (NOT patched)
			(
			(set tsum (getsum (tackon patch_dir patch_thisfile)))
			(if (= tsum fsum)
				(message
					("\nThe current version of the %s %s is already up-to-date.\n" patch_thisfile patch_type)
					"\n"
					"No patching needs to be done.")
				(message
					("\nThe attempt to patch the %s %s failed.\n" patch_thisfile patch_type)
					"\n"
					"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 did not exist in your %ss2 directory.\n" patch_thisfile patch_type patch_type)
			"\n"
			"Please install the module again and retry this patch."
		)
		)
	)
)


(procedure setDirPath
;
; ****************************
; * Ask the user for a directory name.
; ****************************
;
; these variables must be defined before calling this procedure:
;    setDirPathWhy
;    setDirPathHelp
;    DirPath
;
; this procedure returns the selected directory in DirPath
;
	(
	(set moveon	FALSE)
	(while (NOT moveon)
		(
		(set DirPath
			(askdir
				(prompt	("Please select the directory %s" setDirPathWhy)
				)
				(help	setDirPathHelp
					"\n"
					@askdir-help
				)
				(default DirPath)
			)
		)

		(if (= (exists DirPath) 0)
			(makedir DirPath)
			(set moveon	TRUE)
		)
		)
	)
	)
)


(procedure copyLibFile
;
; ****************************
; * Copy the specific library file to the LIBS: directory.
; ****************************
;
; these variables must be defined before calling this procedure:
;
;    copyLibFileName
;    copyLibFileHelp
;    DirPath
;
	(
	(if (= (exists (tackon DirPath copyLibFileName)) 1)
		(
		(protect (tackon DirPath copyLibFileName) "+rwed")
		)
	)
	(copylib
		(prompt	("Copying %s to %s" copyLibFileName copyLibFileSize DirPath))
		(help	copyLibFileHelp
			"\n"
			"If you already have a copy of this library in the selected destination directory, the library "
			"included on the distribution disk will be compared to the one you "
			"already have.  If your current library is an old version, the newer "
			"version will be installed.\n"
			"\n"
			@copylib-help
		)
		(source copyLibFileName)
		(dest DirPath)
		(confirm)
	)
	)
)


(procedure incrementMeter
;
; ****************************
; * Increment the progress meter indicator.
; ****************************
;
;    addToMeter
;
	(
	(set PctDone	(/ (* AccumFiles 100) TotalFiles))
	(set AccumFiles	(+ AccumFiles addToMeter))
	(complete PctDone)
	)
)


; ****************************
; * ADPro Module Installation routine
; ****************************
;
(procedure installADProModule
;
; This procedure does the work of de-arcing and copying files.
; It requires initialization of variables:
;    moduleArcName
;    moduleFileName
;    moduleType
;    moduleDestDir
;    modulesToDo
;    moduleCurrN
;
	(
	(set addToMeter	1)
	(incrementMeter)

	(if (IN modulesToDo moduleCurrN)
		(
		(protect (tackon moduleDestDir moduleFileName) "+rwed")
		(delete (tackon moduleDestDir moduleFileName))

		(set workingMsg		("Installing %s %s..." moduleFileName moduleType))
		(set arcFileName	moduleArcName)
		(set srcFileName	moduleFileName)
		(set destDirName	moduleDestDir)
		(extractLZFile2Dir)
		)
	)
	)
)


; ****************************
; * PCP Patch routine
; ****************************
;
(procedure PCPPatchInstall
	(
	; ****************************
	; * Patching TIFF Loader
	; ****************************
	;
	(set addToMeter	1)
	(incrementMeter)

	(set patch_thisfile	"TIFF")
	(set patch_newname	patch_thisfile)
	(set patch_dir		ADProLoadersDir)
	(set patch_type		"Loader")
	(set patch_curnum	1)
	(set patch_maxnum	5)
	(set patch_cksum_1	859597812)	; 2.0.4 (r1.0.3) -> 3.1.0 (r2.3.0)
	(set patch_cksum_2	1518344491)	; 3.0.1 (r2.0.0) -> 3.1.0 (r2.3.0)
	(set patch_cksum_3	617758844)	; 3.0.3 (r2.1.0,r2.1.5) -> 3.1.0 (r2.3.0)
	(set patch_cksum_4	1350601782)	; 3.0.5 (r2.2.0) -> 3.1.0 (r2.3.0)
	(set patch_cksum_5	-1541692165)	; r1.0.3 + oldpatch0 -> 3.1.0 (r2.3.0)
	(patchFile)
	; ----------------------------
	; latest version:
	;	(set patch_cksum_n	-1509921674)	; 3.1.0 (r2.3.0)
	; ----------------------------
	; old patches:
	;	(set patch_cksum_0	859597812)	; 2.0.4 -> 3.0.5
	;	(set patch_cksum_1	819297291)	; ??? -> 3.0.5
	;	(set patch_cksum_2	1518344491)	; 3.0.1 -> 3.0.5
	;	(set patch_cksum_3	-1641822233)	; ??? -> 3.0.5

	; ****************************
	; * Patching TARGA Loader
	; ****************************
	;
	(set addToMeter	1)
	(incrementMeter)

	(set patch_thisfile	"TARGA")
	(set patch_newname	patch_thisfile)
	(set patch_dir		ADProLoadersDir)
	(set patch_type		"Loader")
	(set patch_curnum	1)
	(set patch_maxnum	4)
	(set patch_cksum_1	1482780211)	; 2.0.4 (r1.0.3) -> 3.1.6 (r2.1.5-r2.2.0)
	(set patch_cksum_2	-1058239812)	; 3.1.4 (r2.0.0) -> 3.1.6 (r2.1.5-r2.2.0)
	(set patch_cksum_3	828958717)	; 3.1.5 (r2.1.0) -> 3.1.6 (r2.1.5-r2.2.0)
	(set patch_cksum_4	828434421)	; r2.0.0 + oldpatch3 -> 3.1.6 (r2.1.5-r2.2.0)
	(patchFile)
	; ----------------------------
	; latest version:
	;	(set patch_cksum_n	832770327)	; 3.1.6 (r2.1.5-r2.2.0)
	; ----------------------------
	; old patches:
	;	(set patch_cksum_0	1482780211)	; 2.0.4 -> 3.1.6
	;	(set patch_cksum_1	-483001049)	; ??? -> 3.1.6
	;	(set patch_cksum_2	244623600)	; ??? -> 3.1.6
	;	(set patch_cksum_3	-1058239812)	; 3.1.4 -> 3.1.6
	;	(set patch_cksum_4	-1058239812)	; ??? -> 3.1.6
	;	(set patch_cksum_5	828958717)	; 3.1.5 -> 3.1.6

	; ****************************
	; * Patching RENDITION Loader
	; ****************************
	;
	(set addToMeter	1)
	(incrementMeter)

	(set patch_thisfile	"RENDITION")
	(set patch_newname	patch_thisfile)
	(set patch_dir		ADProLoadersDir)
	(set patch_type		"Loader")
	(set patch_curnum	1)
	(set patch_maxnum	2)
	(set patch_cksum_1	-784668211)	; 2.0.3 (r1.0.3) -> 3.0.3 (r2.1.0-r2.2.0)
	(set patch_cksum_2	-1088099858)	; 3.0.5 (r2.0.0) -> 3.0.3 (r2.1.0-r2.2.0)
	(patchFile)
	; ----------------------------
	; latest version:
	;	(set patch_cksum_n	1771791148)	; 3.0.3 (r2.1.0-r2.2.0)
	; ----------------------------
	; old patches:
	;	(set patch_cksum_0	-784668211)	; 2.0.3 -> 3.0.3
	;	(set patch_cksum_1	1509760538)	; ??? -> 3.0.3
	;	(set patch_cksum_2	-1088099858)	; 3.0.5 -> 3.0.3

	; ****************************
	; * Creating SUNRASTER Loader
	; ****************************
	;
	(set addToMeter	1)
	(incrementMeter)

	(working "Creating SUNRASTER Loader...\n\nPlease read the ReadMe file after completing installation.")
	(run ("RAM:lz -N -wRAM: x %s:LoadersPatches.lha SUNRASTER.0.PCH RAM:" DiskPatches))
	(set patchCmd ("RAM:lpatch -oRAM:SUNRASTER -pRAM:SUNRASTER.0.PCH \"%s\"" (tackon ADProLoadersDir "RENDITION")))
	(if (= 0 (run patchCmd))
		(
		(if (= (exists (tackon ADProLoadersDir "SUNRASTER")) 1)
			(
			(protect (tackon ADProLoadersDir "SUNRASTER") "+rwed")
			)
		)
		(copyfiles
			(prompt "")
			(help "")
			(source "RAM:SUNRASTER")
			(dest ADProLoadersDir)
		)
		(delete "RAM:SUNRASTER")
		(delete "RAM:SUNRASTER.0.PCH")
		)
	)

	(set patch_thisfile	"SUNRASTER")
	(set patch_newname	patch_thisfile)
	(set patch_dir		ADProLoadersDir)
	(set patch_type		"Loader")
	(set patch_curnum	1)
	(set patch_maxnum	1)
	(set patch_cksum_1	-607213817)	; 3.0.0 (r2.1.0,r2.1.5) -> 3.0.0 (r2.2.0)
	(patchFile)
	; ----------------------------
	; latest version:
	;	(set patch_cksum_n	522824665)	; 3.0.0 (r2.2.0)
	; ----------------------------
	; old patches:

	; ****************************
	; * Creating X Loader
	; ****************************
	;
	(set addToMeter	1)
	(incrementMeter)

	(working "Creating X Loader...\n\nPlease read the ReadMe file after completing installation.")
	(run ("RAM:lz -N -wRAM: x %s:LoadersPatches.lha X.0.PCH RAM:" DiskPatches))
	(set patchCmd ("RAM:lpatch -oRAM:X -pRAM:X.0.PCH \"%s\"" (tackon ADProLoadersDir "RENDITION")))
	(if (= 0 (run patchCmd))
		(
		(if (= (exists (tackon ADProLoadersDir "X")) 1)
			(
			(protect (tackon ADProLoadersDir "X") "+rwed")
			)
		)
		(copyfiles
			(prompt "")
			(help "")
			(source "RAM:X")
			(dest ADProLoadersDir)
		)
		(delete "RAM:X")
		(delete "RAM:X.0.PCH")
		)
	)

	(set patch_thisfile	"X")
	(set patch_newname	patch_thisfile)
	(set patch_dir		ADProLoadersDir)
	(set patch_type		"Loader")
	(set patch_curnum	1)
	(set patch_maxnum	1)
	(set patch_cksum_1	-1915921780)	; 3.0.0 (r2.1.0,r2.1.5) -> 3.0.0 (r2.2.0)
	(patchFile)
	; ----------------------------
	; latest version:
	;	(set patch_cksum_n	1033339899)	; 3.0.0 (r2.2.0)
	; ----------------------------
	; old patches:

	; ****************************
	; * Creating PICT Loader
	; ****************************
	;
	(set addToMeter	1)
	(incrementMeter)

	(working "Creating PICT Loader...\n\nPlease read the ReadMe file after completing installation.")
	(run ("RAM:lz -N -wRAM: x %s:LoadersPatches.lha PICT.0.PCH RAM:" DiskPatches))
	(set patchCmd ("RAM:lpatch -oRAM:PICT -pRAM:PICT.0.PCH \"%s\"" (tackon ADProLoadersDir "RENDITION")))
	(if (= 0 (run patchCmd))
		(
		(if (= (exists (tackon ADProLoadersDir "PICT")) 1)
			(
			(protect (tackon ADProLoadersDir "PICT") "+rwed")
			)
		)
		(copyfiles
			(prompt "")
			(help "")
			(source "RAM:PICT")
			(dest ADProLoadersDir)
		)
		(delete "RAM:PICT")
		(delete "RAM:PICT.0.PCH")
		)
	)

	; ----------------------------
	; latest version:
	;	(set patch_cksum_n	27806237)		; 1.0.0 (r2.3.0)
	; ----------------------------
	; old patches:


	; ****************************
	; * Patching TIFF Saver
	; ****************************
	;
	(set addToMeter	1)
	(incrementMeter)

	(set patch_thisfile	"TIFF")
	(set patch_newname	patch_thisfile)
	(set patch_dir		ADProSaversDir)
	(set patch_type		"Saver")
	(set patch_curnum	1)
	(set patch_maxnum	4)
	(set patch_cksum_1	1720074818)	; 2.0.3 (r1.0.3) -> 3.1.1 (r2.3.0b)
	(set patch_cksum_2	1124297027)	; 3.0.3 (r2.0.0) -> 3.1.1 (r2.3.0b)
	(set patch_cksum_3	1124362563)	; 3.0.3 (r2.1.0-r2.2.0) -> 3.1.1 (r2.3.0b)
	(set patch_cksum_4	629023311)	; 3.1.0 (r2.3.0) -> 3.1.1 (r2.3.0b)
	(patchFile)
	; ----------------------------
	; latest version:
	;	(set patch_cksum_n	1643870817)	; 3.1.1 (r2.3.0b)
	; ----------------------------
	; old patches:
	;	(set patch_cksum_0	1720074818)	; 2.0.3 -> 3.0.3a
	;	(set patch_cksum_1	-1977315916)	; ??? -> 3.0.3a
	;	(set patch_cksum_2	-1780612422)	; ??? -> 3.0.3a
	;	(set patch_cksum_3	1124297027)	; 3.0.3a (15.8.91) 27632 -> 3.0.3a

	; ****************************
	; * Patching TARGA Saver
	; ****************************
	;
	(set addToMeter	1)
	(incrementMeter)

	(set patch_thisfile	"TARGA")
	(set patch_newname	patch_thisfile)
	(set patch_dir		ADProSaversDir)
	(set patch_type		"Saver")
	(set patch_curnum	1)
	(set patch_maxnum	2)
	(set patch_cksum_1	242601535)	; 2.0.3 (r1.0.3) -> 3.1.2 (r2.1.0-r2.2.0)
	(set patch_cksum_2	-1841823675)	; 3.1.1 (r2.0.0) -> 3.1.2 (r2.1.0-r2.2.0)
	(patchFile)
	; ----------------------------
	; latest version:
	;	(set patch_cksum_n	-1409949583)	; 3.1.2 (r2.1.0-r2.2.0)
	; ----------------------------
	; old patches:
	;	(set patch_cksum_0	242601535)	; 2.0.3 -> 3.1.2
	;	(set patch_cksum_1	-2024131800)	; ??? -> 3.1.2
	;	(set patch_cksum_2	-1841823675)	; 3.1.1 -> 3.1.2

	; ****************************
	; * Patching RENDITION Saver
	; ****************************
	;
	(set addToMeter	1)
	(incrementMeter)

	(set patch_thisfile	"RENDITION")
	(set patch_newname	patch_thisfile)
	(set patch_dir		ADProSaversDir)
	(set patch_type		"Saver")
	(set patch_curnum	1)
	(set patch_maxnum	2)
	(set patch_cksum_1	-1958352664)	; 2.0.3 (r1.0.3) -> 3.0.3 (r2.1.0-r2.2.0)
	(set patch_cksum_2	1418874756)	; 3.0.3 (r2.0.0) -> 3.0.3 (r2.1.0-r2.2.0)
	(patchFile)
	; ----------------------------
	; latest version:
	;	(set patch_cksum_n	1418874757)	; 3.0.3 (r2.1.0-r2.2.0)
	; ----------------------------
	; old patches:
	;	(set patch_cksum_0	-1958352664)	; 2.0.3 -> 3.0.3b
	;	(set patch_cksum_1	1939121335)	; ??? -> 3.0.3b
	;	(set patch_cksum_2	1418874756)	; 3.0.3a (26.3.91) 7848 -> 3.0.3b

	; ****************************
	; * Creating SUNRASTER Saver
	; ****************************
	;
	(set addToMeter	1)
	(incrementMeter)

	(working "Creating SUNRASTER Saver...\n\nPlease read the ReadMe file after completing installation.")
	(run ("RAM:lz -N -wRAM: x %s:SaversPatches.lha SUNRASTER.0.PCH RAM:" DiskPatches))
	(set rline ("RAM:lpatch -oRAM:SUNRASTER -pRAM:SUNRASTER.0.PCH \"%s\"" (tackon ADProSaversDir "RENDITION")))
	(if (= 0 (run rline))
		(
		(if (= (exists (tackon ADProSaversDir "SUNRASTER")) 1)
			(
			(protect (tackon ADProSaversDir "SUNRASTER") "+rwed")
			)
		)
		(copyfiles
			(prompt "")
			(help "")
			(source "RAM:SUNRASTER")
			(dest ADProSaversDir)
		)
		(delete "RAM:SUNRASTER")
		(delete "RAM:SUNRASTER.0.PCH")
		)
	)

	(set patch_thisfile	"SUNRASTER")
	(set patch_newname	patch_thisfile)
	(set patch_dir		ADProSaversDir)
	(set patch_type		"Saver")
	(set patch_curnum	1)
	(set patch_maxnum	1)
	(set patch_cksum_1	80549463)	; 3.0.0 (r2.1.0) -> 3.0.1 (2.2.0)
	(patchFile)
	; ----------------------------
	; latest version:
	;	(set patch_cksum_n	-1254807098)	; 3.0.0 (r2.2.0)
	; ----------------------------
	; old patches:

	; ****************************
	; * Creating X Saver
	; ****************************
	;
	(set addToMeter	1)
	(incrementMeter)

	(working "Creating X Saver...\n\nPlease read the ReadMe file after completing installation.")
	(run ("RAM:lz -N -wRAM: x %s:SaversPatches.lha X.0.PCH RAM:" DiskPatches))
	(set patchCmd ("RAM:lpatch -oRAM:X -pRAM:X.0.PCH \"%s\"" (tackon ADProSaversDir "RENDITION")))
	(if (= 0 (run patchCmd))
		(
		(if (= (exists (tackon ADProSaversDir "X")) 1)
			(
			(protect (tackon ADProSaversDir "X") "+rwed")
			)
		)
		(copyfiles
			(prompt "")
			(help "")
			(source "RAM:X")
			(dest ADProSaversDir)
		)
		(delete "RAM:X")
		(delete "RAM:X.0.PCH")
		)
	)

	(set patch_thisfile	"X")
	(set patch_newname	patch_thisfile)
	(set patch_dir		ADProSaversDir)
	(set patch_type		"Saver")
	(set patch_curnum	1)
	(set patch_maxnum	2)
	(set patch_cksum_1	1301953576)	; 3.0.0 (r2.1.0,r2.1.5) -> 3.0.0 (r2.3.0)
	(set patch_cksum_2	-36548833)	; 3.0.0 (r2.2.0) -> 3.0.1 (r2.3.0)
	(patchFile)
	; ----------------------------
	; latest version:
	;	(set patch_cksum_n	990072282)	; 3.0.1 (r2.3.0)
	; ----------------------------
	; old patches:

	; ****************************
	; * Creating PICT Saver
	; ****************************
	;
	(set addToMeter	1)
	(incrementMeter)

	(working "Creating PICT Saver...\n\nPlease read the ReadMe file after completing installation.")
	(run ("RAM:lz -N -wRAM: x %s:SaversPatches.lha PICT.0.PCH RAM:" DiskPatches))
	(set patchCmd ("RAM:lpatch -oRAM:PICT -pRAM:PICT.0.PCH \"%s\"" (tackon ADProSaversDir "RENDITION")))
	(if (= 0 (run patchCmd))
		(
		(if (= (exists (tackon ADProSaversDir "PICT")) 1)
			(
			(protect (tackon ADProSaversDir "PICT") "+rwed")
			)
		)
		(copyfiles
			(prompt "")
			(help "")
			(source "RAM:PICT")
			(dest ADProSaversDir)
		)
		(delete "RAM:PICT")
		(delete "RAM:PICT.0.PCH")
		)
	)

	; ----------------------------
	; latest version:
	;	(set patch_cksum_n	141623904)		; 1.0.0 (r2.3.0)
	; ----------------------------
	; old patches:
	)
)


; ****************************
; * EPSON Patch routine
; ****************************
;
(procedure EPSONPatchInstall
	(
	; ****************************
	; * Patching EPSON_SCAN / ESPON_ES300C Loader
	; ****************************
	;
	(set addToMeter	1)
	(incrementMeter)

	(if (= (exists (tackon ADProLoadersDir "EPSON_ES300C")) 1)
		(
		(protect (tackon ADProLoadersDir "EPSON_ES300C") "+rwed")
		(rename (tackon ADProLoadersDir "EPSON_ES300C") (tackon ADProLoadersDir "EPSON_SCAN"))
		)
	)

	(set patch_thisfile	"EPSON_SCAN")
	(set patch_newname	patch_thisfile)
	(set patch_dir		ADProLoadersDir)
	(set patch_type		"Loader")
	(set patch_curnum	1)
	(set patch_maxnum	4)
	(set patch_cksum_1	477972192)	; 2.1.0 (r???) -> 3.0.3 (r2.2.0)
	(set patch_cksum_2	539717622)	; 2.2.0 (r???) -> 3.0.3 (r2.2.0)
	(set patch_cksum_3	-1329441256)	; 3.0.1 (r???) -> 3.0.3 (r2.2.0)
	(set patch_cksum_4	1871690267)	; 3.0.2 (r???) -> 3.0.3 (r2.2.0)
	(patchFile)
	; ----------------------------
	; latest version:
	;	(set patch_cksum_n	-196811803)	; 3.0.3 ieee (r2.1.0-r2.2.0)
	;	(set patch_cksum_n+1	-529146241)	; 3.0.3 m68881 (r2.1.0-r2.2.0)
	; ----------------------------
	; old patches:
	;	(set patch_cksum_0	539717622)	; 2.2.0 (07.29.91) -> 3.0.2a
	;	(set patch_cksum_1	477972192)	; 2.1.0 (05.24.91) -> 3.0.2a
	;	(set patch_cksum_2	-1329441256)	; 3.0.1 (06.11.91) -> 3.0.2a
	)
)


; ****************************
; * General CleanUp routine
; ****************************
;
(procedure cleanUp
	(
	; ****************************
	; * delete temporary files.
	; ****************************
	;
	(if (= (exists "RAM:lz") 1)
		(delete "RAM:lz")
	)
	(if (= (exists "RAM:lpatch") 1)
		(delete "RAM:lpatch")
	)
	(if (= (exists "RAM:X.0.PCH") 1)
		(delete "RAM:X.0.PCH")
	)
	(if (= (exists "RAM:SUNRASTER.0.PCH") 1)
		(delete "RAM:SUNRASTER.0.PCH")
	)
	(if (= (exists "RAM:PICT.0.PCH") 1)
		(delete "RAM:PICT.0.PCH")
	)


	; ****************************
	; * exit the program.
	; ****************************
	;
	(complete 100)

	(exit)
	)
)


; *************************************************************************
; *                         ACTUAL START OF SCRIPT
; *
; * Ask all questions for the user now, do all of the work later.
; *************************************************************************
;

; ****************************
; * set the default destination to SYS:<ProductBaseName>.
; ****************************
;
(onerror
	(cleanUp)
)

; ****************************
; * set the default destination to ADPRO: (if exists) or SYS:<ProductBaseName> (if it doesn't).
; ****************************
;
(if (= (getassign "ADPRO" "a") "")
	(set @default-dest "SYS:ADPro")
	(set @default-dest (getassign "ADPRO" "a"))
)
;(set @default-dest	"SYS:ADPro")

(setPkgOpts)
(if (= PkgOpts 0)
	(abort "You didn't select anything, so there is nothing to do.\n")
)

(if (IN PkgOpts 0)					; if bit position 0 is on
	(set TotalFiles		(+ TotalFiles 12))	; 6 loaders + 6 savers
)

(if (IN PkgOpts 1)					; if bit position 1 is on
	(set TotalFiles		(+ TotalFiles 1))	; 1 loader
)

(set validDir FALSE)
(while (NOT validDir)
	(
	(set setDirPathWhy	"where the ADPro (or MorphPlus) program is located.")
	(set setDirPathHelp	"You must select the directory into which the ADPro (or MorphPlus) program was installed.\n")
	(set DirPath		@default-dest)
	(setDirPath)
	(set ADProDir		DirPath)
	(set @default-dest	ADProDir)

	(set ADProLoadersDir	(tackon ADProDir "Loaders2"))
	(if (= (exists ADProLoadersDir) 0)
		(message ("The %s directory does not exist.\n\nThis directory needs to be available for the patch process to succeed.\n" ADProLoadersDir))
		(
		(set ADProSaversDir	(tackon ADProDir "Savers2"))
		(if (= (exists ADProSaversDir) 0)
			(message ("The %s directory does not exist.\n\nThis directory needs to be available for the patch process to succeed.\n" ADProSaversDir))
			(set validDir TRUE)
		)
		)
	)
	)
)

(set TotalFiles	(+ TotalFiles 2))				; lz; lpatch

(if (= (exists "LIBS:arp.library") 0)				; arp.library
	(set TotalFiles	(+ TotalFiles 1))
)


(complete 0)

(message "\nShall I proceed with the installation?"
	(help	"Once you press the Proceed button, the selected modules will be patched.  This is your last chance "
		"to abort this procedure.\n"
		"\n"
		"After you press Proceed, you can use the Esc key to abort the patch procedure.\n")
)


; ****************************
; * copy arp.library to the LIBS: directory so that the lz unarchiver
; * can do its thing.
; ****************************
;
(set addToMeter	1)
(incrementMeter)

(set copyLibFileName	("%s:Libs/arp.library" DiskPatches))
(set copyLibFileHelp	"This library is required to patch the modules, but not to execute it.\n")
(set DirPath		"LIBS:")
(copyLibFile)


; ****************************
; * copy lz and lpatch to the RAM: directory.
; ****************************
;
(set addToMeter	2)
(incrementMeter)

(copyfiles
	(prompt "")
	(help "")
	(source ("%s:C/lz" DiskPatches))
	(dest "RAM:")
)
(copyfiles
	(prompt "")
	(help "")
	(source ("%s:C/lpatch" DiskPatches))
	(dest "RAM:")
)
(if (OR (= (exists "RAM:lz") 0) (= (exists "RAM:lpatch") 0))
	(abort	"Unable to copy necessary files to RAM:.\n"
		"\n"
		"Please make sure you have at least 100K of memory available before patching the modules.\n")
)


; ****************************
; * Initialize some variables.
; ****************************
;
(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)


(if (IN PkgOpts 0)
	(PCPPatchInstall)
)
(if (IN PkgOpts 1)
	(EPSONPatchInstall)
)

(cleanUp)
