;****************************************************************
; Install Script for LW4UP v0.95 beta (general version)
;
; $VER: lw4up_install 1.0 (2001.01.25)
;****************************************************************

;*** HELP STRINGS ***********************************************

(set #notfound1-help (cat
"Select directory where the LightWave executable are located\n"
"this would normally be \"TOASTER:NewTek/Programs\""))

(set #notfound2-help (cat
"Select another directory to scan for LightWave executable"))

(set #chksumerror-help (cat
"This patch only works on (original) LightWave V4.\n"
"Make sure Your LightWave executable's version = \"4.00y\",\n"
"size = \"952428\" and creation date = \"95-Dec-20\".\n"
"\nThe file name also must be \"LightWave.FP\" or \"LightWave.FP_old\"."))

(set #patch-help (cat
"The patch is about to be applied to LightWave.\n"
"If the original was named \"LightWave.FP\" it has now\n"
"been duplicated and named \"LightWave.FP_old\".\n"
"It might be useful to keep it in place for future upgrades."))

(set #options-help (cat
"  cgx_startup - (General option)\n\n"
"Layout will try to open it's screen in the selected (CGX) ModeID\n"
"if ModeID is unknown ASL Screen-Requester opens on Workbench.\n\n"
"  notoast_prev - (Toaster option)\n\n"
"Forces Play-Preview to be displayed on Layout screen if LightWave\n"
"is started from Switcher."))

;*** PROMPT STRINGS *********************************************

(set #chksumerror-prompt (cat
"*** ERROR ***\n\nNo correct version of LighWave could be found!\n"
"\nSearch Another Directory?"))

(set #patch-prompt (cat
"Will now start patching LightWave.\n\n Continue?"))

(set #option-prompt (cat
"Select LW4UP Options"))

;*** VARIABLES **************************************************

(set #old_chksum 922136163)
(set #new_chksum 0)
(set #dup_lw 0)
(set #PCH_FILE "LW4UP_095b.pch")
(set #LW_NAME "LightWave.FP")

;*** START ******************************************************

(if (exists ("TOASTER:Programs") (noreq))
	(set @default-dest (expandpath "TOASTER:Programs"))
	(set @default-dest (askdir (disk)
		(default "NewTek/Programs")
		(prompt "Where Are LightWave?")
		(help #notfound1-help))))
			
(while (= 0 #new_chksum)
	(set #lw_old (tackon @default-dest #LW_NAME))

	(if (exists #lw_old (noreq))
		((working "Computing checksum on: " (fileonly #lw_old))
		 (set #new_chksum (getsum #lw_old))
		 (if (= #old_chksum #new_chksum)
			(set #dup_lw 1)
			(set #new_chksum 0))))

	(if (= 0 #new_chksum)
		((set #lw_old (cat #lw_old "_old"))
		 (if (exists #lw_old (noreq))
			((working "Computing checksum on: " (fileonly #lw_old))
			 (set #new_chksum (getsum #lw_old))
			 (if (= #old_chksum #new_chksum)
				(set #dup_lw 0)
				(set #new_chksum 0))))))

	(if (= 0 #new_chksum)
			(if (askbool (prompt #chksumerror-prompt) (help #chksumerror-help))
			(set @default-dest (askdir (prompt "Select Directory")
					(help #notfound2-help)
					(disk)
					(default "NewTek/Programs")))
			(abort "Patch Aborted")))
)


(if (= 1 #dup_lw)
	((copyfiles (prompt (cat "Duplicating " #LW_NAME))
		(help @copyfiles-help)
		(source #lw_old)
		(dest @default-dest)
		(newname (cat #LW_NAME "_old"))
		(infos))))


(set #outarg (cat " -o" (tackon @default-dest #LW_NAME)))
(set #pcharg (cat " -p" #PCH_FILE))
(set #oldarg (cat " " #lw_old))

(if (askbool (prompt #patch-prompt)
		(help #patch-help))
	((working "Patching File: " (fileonly #lw_old))
	 (set #status (run (cat "spatch" #outarg #pcharg #oldarg))))
	(abort "Patching Aborted."))

;*** SET ENV: OPTIONS *******************************************

(if (= 0 #status)
	((set #options (askoptions (prompt "Select LW4UP Options")
	 			(help #options-help)
				(choices "cgx_startup" "notoast_prev (Toaster)")
				(default 1)))
	 (set #startup (BITAND #options 1))
	 (set #notoast (BITAND #notoast 2))
	 (run (cat "c:setenv " "ENV:LW4UP/cgx_modeid " "0"))
	 (run (cat "c:setenv " "ENV:LW4UP/cgx_startup " #startup))
	 (run (cat "c:setenv " "ENV:LW4UP/notoast_prev " #notoast))
	 (copyfiles (prompt "Copying ENV: vars. to ENVARC:")
			(help @copyfiles-help)
			(source "ENV:LW4UP")
			(dest "ENVARC:LW4UP")
			(all))
        (tooltype (prompt "Setting Stack")
        	(help "LightWaves Stack is set to 70000")
        	(dest (tackon @default-dest #LW_NAME))
        	(setstack 70000)
		(confirm)))
	 (abort "Patch ERROR!")
)

(message "LightWave have been patched successfully.\n")
