;
;	Installer script for The Promotor
;	Should be run from the Workbench by double-clicking on the icon
;	or from the Shell through the Commodore Installer.
;

(set @app-name				"The Promotor")

;
;	The default location for the program and the configuration files.
;

(set program-def-dest	"SYS:Tools/Commodities")
(set config-def-dest		"ENVARC:")
(set source-dir			(pathonly @icon))
(complete 0)

;
;	Check for version 2.04 or higher of the OS.
;

(if (< (/ (getversion) 65536) 37)
	(
		(abort
			(cat		"This program will only work with Kickstart and Workbench "
						"2.04 or higher.  Try updating your Kickstart and Workbench!"
			)
		)
   )
)

;
;	Welcome the user and ask some questions ...
;

(welcome "The Promotor Installation")

;
;	Ok, let's say this is 1% :-]
;

(complete 1)

;
;	Which parts of the package should be installed ?
;

(set to-install
	(askoptions
		(prompt 		"Which parts of the Promotor package should be installed?")
		(help			"Here you can select which parts of the package should "
						"be copied onto your systems disk (or any other place you "
						"indicate).  This can be useful if you already installed "
						"the Promotor, but lost some of the files ...\n\n"
						"The Promotor Executable is the actual program, "
						"the Documentation contains all kinds of information you "
						"will probably need when using the Promotor.  The Example "
						"Configuration file contains some examples on how to "
						"define promotion rules for use with the Promotor and the "
						"VGAOnly Configuration File contains some promotion rules "
						"to promote all displays to 31kHz ones when VGAOnly is "
						"installed in the Devs:Monitors directory.\n\n"
						@askoptions-help
		)
		(choices		"The Promotor executable"
						"The Registration program"
						"The documentation file"
						"The example configuration file"
						"The VGAOnly configuration file"
		)
		(default 31)
	  )
)

;
;  Where should the program be installed (if applicable) ?
;

(set program-dest
	(if (IN to-install 0)
		(askdir
			(prompt		"Select the location to install the Promotor")
			(default		program-def-dest)
			(help			"Choose a destination partition or directory "
							"to contain the executable of the Promotor. "
							"The installer will NOT create any directories, "
							"but will simply copy the executable to the "
							"directory you specified.\n\n"
							@askdir-help
			)
	   )
	   (
	   	program-def-dest
	   )
	)
)
(complete 4)

;
;	Where should the registration program be installed (if applicable) ?
;

(if (IN to-install 1)
	(set register-dest
		(askdir
			(prompt		"Select the location to install the Promotor")
			(default		program-dest)
			(help			"Choose a destination partition or directory "
							"to contain the registration program for the Promotor. "
							"The installer will NOT create any directories, "
							"but will simply copy the executable to the "
							"directory you specified.\n\n"
							@askdir-help
			)
	   )
	)
)
(complete 8)

;
;	Where should the documentation be installed (if applicable) ?
;

(if (IN to-install 2)
	(set doc-dest
		(askdir
			(prompt		"Select the location to install the documentation of "
							"the Promotor"
			)
			(default		program-dest)
			(help			"Choose a destination partition or directory "
							"to contain the documentation file of the Promotor. "
							"The installer will NOT create any directories, "
							"but will simply copy the documentation file to the "
							"directory you specified.\n\n"
							@askdir-help
			)
	   )
	)
)
(complete 12)

;
;	Where should the example configuration file be installed (if applicable) ?
;

(set config-dest
	(if (IN to-install 3)
		(askdir
			(prompt		"Select the location to install the example configuration file")
			(default		config-def-dest)
			(help			"Choose a destination partition or directory "
							"to contain the example configuration file of the Promotor. "
							"The installer will NOT create any directories, "
							"but will simply copy the configuration file to the "
							"directory you specified.\n\n"
							@askdir-help
			)
	   )
		(
			config-def-dest
		)
	)
)
(complete 16)

;
;	Where should the VGAOnly configuration file be installed (if applicable) ?
;

(if (IN to-install 4)
	(set vga-config-dest
		(askdir
			(prompt		"Select the location to install the VGAOnly configuration file")
			(default		config-dest)
			(help			"Choose a destination partition or directory "
							"to contain the VGAOnly configuration file of the Promotor. "
							"The installer will NOT create any directories, "
							"but will simply copy the configuration file to the "
							"directory you specified.\n\n"
							@askdir-help
			)
	   )
	)
)
(complete 20)

;
;	Now, let's do the actual copying of the program ...
;

(if (IN to-install 0)
	(set error
		(trap 4
			(copyfiles
				(prompt (cat "\n\nCopying the Promotor executable to " program-dest))
				(source source-dir)
				(dest program-dest)
				(pattern "Promotor(|.info)")
				(help @copyfiles-help)
				(infos)
			)
		)
	)
	(if (> error 0) (message ("Dos Error %ld detected" @ioerr)))
)
(complete 40)

;
;	Now, let's do the actual copying of the registration program ...
;

(if (IN to-install 1)
	(set error
		(trap 4
			(copyfiles
				(prompt (cat "\n\nCopying the Registration program to " register-dest))
				(source source-dir)
				(dest register-dest)
				(pattern "Register(|.info)")
				(help @copyfiles-help)
				(infos)
			)
		)
	)
	(if (> error 0) (message ("Dos Error %ld detected" @ioerr)))
)
(complete 50)

;
;	Now, let's do the actual copying of the documentation ...
;

(if (IN to-install 2)
	(set error
		(trap 4
			(copyfiles
				(prompt (cat "\n\nCopying the documentation to " doc-dest))
				(source source-dir)
				(dest doc-dest)
				(pattern "Promotor.doc(|.info)")
				(help @copyfiles-help)
				(infos)
			)
		)
	)
	(if (> error 0) (message ("Dos Error %ld detected" @ioerr)))
)
(complete 60)

;
;	Now, let's do the actual copying of the example configuration ...
;

(if (IN to-install 3)
	(set error
		(trap 4
			(copyfiles
				(prompt (cat "\n\nCopying the example configuration to " config-dest))
				(source source-dir)
				(dest config-dest)
				(pattern "Promotor.config(|.info)")
				(help @copyfiles-help)
				(infos)
			)
		)
	)
	(if (> error 0) (message ("Dos Error %ld detected" @ioerr)))
)
(complete 70)

;
;	Now, let's do the actual copying of the VGAOnly configuration ...
;

(if (IN to-install 4)
	(set error
		(trap 4
			(copyfiles
				(prompt (cat "\n\nCopying the VGAOnly configuration to " vga-config-dest))
				(source source-dir)
				(dest vga-config-dest)
				(pattern "VGAOnly.config(|.info)")
				(help @copyfiles-help)
				(infos)
			)
		)
	)
	(if (> error 0) (message ("Dos Error %ld detected" @ioerr)))
)
(complete 80)

;
;	Does the user want to install a new configuration file as the default ?
;	If so, we need to know the location of the Promotor executable ...
;	And modify the appropriate tooltype ...
;

(set program-name-known FALSE)
(if	(askbool
			(prompt 		"\n\nDo you want to select a new configuration file as "
							"the default one that will be used by the Promotor?"
			)
			(help			"Here you may choose to select a new default configuration "
							"file for the Promotor, which it will try to read whenever "
							"it is started.\n\n"
							@askbool-help
			)
			(choices "Yes!" "No!")
			(default 0)
		)
	(
		(set program-name-known TRUE)
		(set program-name
			(if (IN to-install 0)
				(tackon program-dest "Promotor")
				(askfile
					(prompt		"Please tell me where the copy of the Promotor is that "
									"you wish to register"
					)
					(default		(tackon program-def-dest "Promotor"))
               (help			"You should select the copy of the Promotor which you wish "
               				"to register.  You did not install a new copy, so it is "
               				"rather difficult for me to know where your copy of the "
               				"Promotor is located.  I've made a guess, but you'll still "
               				"have to confirm it!\n\n"
               				@askfile-help
					)
				)
			)
		)
		(set new-config
			(askfile
				(prompt		"Select the configuration file to install as the default")
				(default		(if (IN to-install 3)
									(tackon config-dest "Promotor.config")
									(if (IN to-install 4)
										(tackon config-dest "VGAOnly.config")
										(tackon config-def-dest "Promotor.config")
									)
								)
				)
				(help			"Choose the configuration file that the Promotor should "
								"try to read every time it is started. "
								"The installer will modify the tooltypes of the "
								"newly installed copy of the Promotor to reflect your "
								"choice.\n\n"
								@askfile-help
				)
		   )
		)
      (tooltype (dest program-name)
      	(settooltype "CONFIG" (cat "\"" new-config "\""))
      )
	)
)
(complete 85)

;
;	Set the "FILE" tooltype of the registration program to the correct
;  copy of the Promotor executable ...
;

(if (IN to-install 1)
	(
		(if (= program-name-known FALSE)
			(set program-name
				(if (IN to-install 0)
					(tackon program-dest "Promotor")
					(askfile
						(prompt		"Please tell me where the copy of the Promotor is that "
										"you wish to use the registration program for ..."
						)
						(default		(tackon program-def-dest "Promotor"))
		            (help			"You should select the copy of the Promotor which you wish "
		            				"to use the registration program for.  You did not install "
										"a new copy, so it is "
		            				"rather difficult for me to know where your copy of the "
		            				"Promotor is located.  I've made a guess, but you'll still "
		            				"have to confirm it!\n\n"
		            				@askfile-help
						)
					)
				)
			)
		)
		(set program-name-known TRUE)
		(tooltype (dest (tackon register-dest "Register"))
      	(settooltype "FILE" (cat "\"" program-name "\""))
      )
	)
)
(complete 95)

;
;	Does the user want to register his copy of the Promotor ?
;	If so, we need to know the location of the Promotor executable again.
;	Then we simply run the register program.
;

(if	(askbool
			(prompt 		"\n\nDo you want to register your copy of the Promotor now?\n"
         				"Only choose to do this if you have sent in your shareware "
         				"fee and have received a registration key for the Promotor."
			)
			(help			"Here you may choose to register your copy of the Promotor. "
							"To register, you'll need a registration key, which you can "
							"obtain by sending the shareware fee to the author:\n"
							"Kurt Haenen, Homsemstraat 53, B-3891 Borlo, Belgium.\n\n"
							@askbool-help
			)
			(choices "Yes!" "No!")
			(default 0)
		)
	(
		(if (= program-name-known FALSE)
			(set program-name
				(if (IN to-install 0)
					(tackon program-dest "Promotor")
					(askfile
						(prompt		"Please tell me where the copy of the Promotor is that "
										"you wish to register"
						)
						(default		(tackon program-def-dest "Promotor"))
	               (help			"You should select the copy of the Promotor which you wish "
	               				"to register.  You did not install a new copy, so it is "
	               				"rather difficult for me to know where your copy of the "
	               				"Promotor is located.  I've made a guess, but you'll still "
	               				"have to confirm it!\n\n"
	               				@askfile-help
						)
					)
				)
			)
		)
		(set program-name-known TRUE)
		(set copy-keyfile FALSE)
		(if (= (exists (tackon source-dir "KeyFile") (noreq)) 1)
         (	(set keyfile (tackon source-dir "KeyFile"))
				(set keyfile-detected TRUE)
				(set copy-keyfile TRUE)
			)
			(if (= (exists "L:Promotor.key" (noreq)) 1)
         	(	(set keyfile "L:Promotor.key")
         		(set keyfile-detected TRUE)
				)
				(set keyfile-detected FALSE)
			)
		)
		(if copy-keyfile
			(set error
				(trap 4
					(copyfiles
						(prompt (cat "\n\nCopying the key-file to L:"))
						(source keyfile)
						(dest "L:")
						(newname "Promotor.key")
						(help @copyfiles-help)
					)
				)
			)
			(if (> error 0) (message ("Dos Error %ld detected" @ioerr)))
		)

		(if keyfile-detected
			(
				(working "Detected a registration key ...")
				(copyfiles (source keyfile) (dest "ENV:")
					(newname "Promotor.key")(nogauge))
				(set Key (getenv "Promotor.key"))
				(delete "ENV:Promotor.key")
				(message
					(cat
							"\n\nI will now start the registration program. "
							"I will use the registration key I found in "
							"\"" keyfile "\"."
					)
				)
				(working	"Registering your copy of the Promotor.")
				(set result
					(run
						(cat
							"Register FILE=\"" program-name "\" KEY=" Key "\n"
						)
					)
				)
			)
			(
				(message	"\n\nI will now start the registration program. "
							"A window with a text-area will appear.  Type in your "
							"registration key and select the Continue gadget when "
							"finished."
				)
				(working	"Registering your copy of the Promotor.")
				(set result
					(run
						(cat
							"Register FILE=\"" program-name "\"\n"
						)
					)
				)
			)
		)
		(complete 98)
		(if (> result 7) (set result 7))
		(message (select result
			(cat	"\n\nYou have now succesfully registered your copy of "
					"the Promotor."
			)
			(cat	"\n\nSomething when wrong while registering!  This should "
					"not happen normally."
			)
			(cat	"\n\nYou've chosen not to register!  Ok, but remember: this "
					"is a shareware program.  If you use it on a regular basis, "
					"you should pay the shareware fee."
			)
			(cat	"\n\nThe file you were referring to seems to have vanished!"
			)
			(cat	"\n\nThe file you were referring to is no original executable of "
					"the Promotor.  If you have crunched the executable, then you "
					"should decrunch it and try again.  Once registered, the program "
					"may be crunched again."
			)
			(cat	"\n\nYou have supplied an illegal registration key.  If you "
					"have actually sent in your shareware fee and have received this "
					"key from the author, you should try it a couple of times more "
					"and if none of them succeed, then contact Kurt Haenen and report "
					"the problem."
			)
			(cat	"\n\nWe couldn't write the registration information to the executable "
					"file.  Try removing any kind of write-protection that may be active."
			)
			(cat	"\n\nSomething went wrong ... I don't know what ... Very weird ..."
			)
		))
	)
)
(complete 100)

;
;	Done !
;

(exit (quiet))
