; PGP Installer script
; $VER: PGP-Installer 0.7 for 2.3a.3 (30.5.94)

; ***********************************************************
; * Basic PGP v2.3a.2 Installation
; *
; * Installs PGP, support files, and AmigaGuide documentation
; *
; * by Jacob Ellis <jellis@nah.oau.org>
; *
; *
; * Tries to be smart about where files should go, especially
; * if upgrading from a previous version of PGP.
; *
; * I have tried my best to work around some of the limitations
; * of Installer, which while powerful in some ways, it is extremely
; * limited in others (very annoying!!!). Hopefully you'll think
; * the results are worthwhile.
; *
; *
; * Note: Unlike many Installer scripts, the novice install
; *       is truly fully automatic (as per the Installer
; *       manual). Don't panic though, it'll make good guesses on
; *       where things should go (IMHO). And, as you might expect,
; *       expert install truly does require you to confirm
; *       EVERY action.
; *
; *
; *
; ***********************************************************
; *
; * Limitations:
; *
; *     Required OS 2.04 or better (this isn't a limitation
; *     in my opinion!). This is simply because I was too
; *     lazy to ask the 1.3 user where they keep ENVARC: (and what
; *     a hassle if they don't have an ENVARC:!)
; *
; *     Only supports English (but does install the PGP
; *     extra language files). Support is in for other languages,
; *     but this will require someone to translate the text
; *     messages (I have to say I am very disappointed on how
; *     well Installer supports other languages).
; *
; *	Does not currently install any extras. Installer does
; *     not support lists (as far as I can tell), which means
; *     each filename must be specified (unless, of course,
; *     you just wanted to copy whole directories) individually
; *     each time you want to do something with it.
; *
; *     Will only install PGP v2.3a.2 reliably, this script
; *     should abort on any other version of PGP. Expert user
; *     can override this.
; *
; *	The method used to determine if an older version of PGP
; *     is being installed is very weak (ie. file date), but
; *     better than nothing.
; *
; *	Other minor limitations may exist where I got lazy.
; *
; ***********************************************************
; *
; * General Program Outline:
; *
; * Note: Novice users are not asked anything. Defaults for them
; *       are set to the logic outlined below.
; *
; *     1. Check if ENV:PGP exists.
; *     	A) Yes, get ENV:PGPPATH for directions.
; *                Check path for directions to PGP binary.
; *		B) No, looks like a new install. Use default settings.
; *
; * 	2. Ask user where PGP and support should be installed.
; * 		A) Look if PGP might exist there.
; * 			i)  Yes, assume an upgrade, use current settings.
; * 			ii) No, assume new install.
; *
; *     3. Check if AmigaGuide is installed.
; * 		A) Yes, find most logical place for AmigaGuide files.
; *                     i)  ENV:AmigaGuide/Path
; *                     ii) Check OS versions
; * 				a) => OS 2.1 then HELP:@language
; * 				b) < OS 2.1 then S:
; *		       iii) Ask for user override
; *             B) No, copy AmigaGuide documentation with support files.
; *
; *     4. Check if PGP to install is older than PGP already installed.
; *             A) Yes, ask if user wants to abort.
; *             B) No, continue install.
; *
; * 	5. Upgrading from a previous version?
; *             A) Yes, ask if user wants new configuration.
; *                     i) Yes, install new configuration.
; *                    ii) Yes, backup old configuration, then install new.
; *             B) No, keep old configuration.
; *
; *     6. Install files to selected destinations.
; *
; *     7. New installation?
; *             A) Yes, set ENV: and ENVARC: to match installed settings.
; *             B) No, leave ENV: and ENVARC: alone.
; *
; *     8. View readme
; *             A) OS => 3.0 then use Multiview
; *             B) OS < 3.0 then use More
; *
; *
; ***********************************************************
; *
; * History
; *
; * 0.1	Initial version (24.2.94)
; *
; * 0.2 Minor cleanup (25.2.94)
; *	      + Accidently left some old debug code in
; *
; * 0.3 Minor fix for OS 2.04 users (26.2.94)
; *	      + Novice OS 2.04 users that added AmigaGuide and
; *		did not bother to specify a location for
; *             AmigaGuide files would end up with their
; *		PGP AmigaGuide files in HELP:(language).
; *		This now defaults to S: (same as AmigaGuide) if
; *             no other more logical location can be found.
; *           + Added history section
; *           + Added more comments to obscure sections
; *           + Fixed prompt and help messages for AmigaGuide
; *             installation.
; *           + Added % complete indicators
; *
; * 0.4 Minor fix for ENV: and ENVARC: paths (28.2.94)
; *           + ENV: and ENVARC: variables are now forced to use
; *             absolute paths. Previously, a relative path could
; *		be specified.
; *
; * 0.5 Minor change to account for upgrading users (1.3.94)
; *     Major change to account for workbench users
; *           + Discovered major problem with the script if run
; *		from Workbench. Workbench only gives the
; *		Installer the default system path, this consists
; *		of Current_dir and C:. This is not acceptable,
; *		so the script is now an IconX script that calls
; *             Installer. This seems to have solved the problem,
; *             but I wish there was a neater way to do this. :(
; *           + A user who upgrades from a previous version of PGP
; *             and wishes to install PGP in a new location could
; *             cause PGP to get confused about what it should do
; *             with the old configuration file. PGP-Install now
; *             treats a user in this class as a completely new install,
; *             rather than as an upgrade (so they get a new Config
; *             file in the new location and their old Config is not
; *             touched).
; *	      + Added program outline for those curious about PGP-Install's
; *             logic and reasoning. This is really to explain why
; *		PGP-Install chooses where things go, and to help
; *             you identify what went wrong if PGP-Install should
; *		break on your configuration.
; *
; * 0.6 Major fix for AmigaGuide path finding logic (2.3.94)
; *     Major change for PGP revision finding code
; *           + If your ENV:AmigaGuide/Path variable contains
; *             multiple paths or leading spaces (shame on you),
; *             PGP-Install can now extract a readable path from
; *             that variable.
; *           + Installer has trouble understanding PGP's version
; *             string, so the method for determining an out of date
; *             version of PGP is changed. It is now based on file date
; *             and size. Obviously, this could have a lot of problems,
; *             but it's better than nothing. :)
; *
; * 0.7 Major change for PGP integrity check (30.5.94)
; *           + The check now does MD5SUM checking instead of the
; *             older Installer getsum() method. The entire archive
; *             is now checked in less than the time it took to check
; *             just the PGP binary with the old method.
; *           + Updated files for PGP Amiga 2.3a.3
; *
; ***********************************************************



; ***********************************************************
; * Set APPNAME if started directly from shell
; * Note: this is broken!
; ***********************************************************

(set @app-name "PGP 2.3a.3")

(set osver (/ (getversion) 65536)) ; get OS version



; ***********************************************************
; * Important file names and stuff to be installed
; * This applies ONLY to 2.3a.3
; ***********************************************************

(set newpgpbin "bin/PGP")
(set newpgpbinsum 1659748947)
(set newpgpbinsize 152260)

; Unforunately, Installer does not seem to be able to deal with
; lists. So each file must be listed individually (yuck!).

; Misc files

; "bin/es.hlp" "bin/fr.hlp" "bin/keys.asc" "bin/language.txt" "bin/pgp.hlp"

; "doc/keyserv.doc" "doc/newfor22.doc" "doc/newfor23.doc" "doc/pgformat.doc" "doc/PGP.faq" "doc/politic.doc" "doc/readme.doc" "doc/setup.doc"

; AmigaGuide files
; "doc/pgpdoc1.guide" "doc/pgpdoc2.guide"


(set pgpconfig "PGP.config")
(set newpgpconfig (tackon "bin/" pgpconfig))



; ***********************************************************
; * Check to make sure English is the language used here
; * Other languages will hopefully be supported in the future
; ***********************************************************

(if(= @language "english")

	; Set up strings for English usage
        (
	 (set #yes "Yes")
	 (set #no "No")

	 (set #already-installed (cat
                @app-name " appears to already be installed.\n"
		"Do you wish to install anyway?")
	 )
	 (set #already-installed-help (cat
                @app-name " appears to already be installed.")
	 )
	 (set #already-installed-abort (cat
		@app-name " already installed.\nAborting install.")
	 )

	 (set #old-installed (cat
                "You seem to be attempting to install an older"
		" version of PGP. Installation of an older"
		" version of PGP is not recommended. Do you wish"
		" to continue?")
	 )
	 (set #old-installed-help (cat
		@app-name " may not be the latest version of PGP. "
		" You are strongly adviced to examine this archive"
		" and your currently installed version of PGP"
		" more closely.")
	 )
	 (set #old-installed-abort (cat
		@app-name " may be out of date. Please check your"
		" currently installed version of PGP and examine "
		" the PGP included in this archive more closely.")
	 )


	 (set #pgp-bin-prompt (cat
		"Please choose where to install the " @app-name
        	" program binary.")
         )
	 (set #pgp-bin-help (cat
		"This is where the " @app-name " binary will"
                " be installed. Ideally, it should be somewhere"
		" in your shell path.")
	 )

	 (set #pgp-ag-prompt (cat
		"Please choose where to install the " @app-name
		" AmigaGuideŽ documentation.")
	 )
	 (set #pgp-ag-help (cat
		"This should be where you normally keep your"
		" AmigaGuideŽ help files and documentation.\n\n"
		"AmigaGuideŽ is the Amiga hypertext help system.")
	 )

	 (set #pgp-path-prompt (cat
		"Please choose where to install the " @app-name
		" configuration and support files.")
	 )
         (set #pgp-path-help (cat
		"This is where files necessary to " @app-name
                " will be installed. These files include the"
                " configuration file, language files, internal"
		" help, as well as the PGP keyrings.\n\n"
		"These files do not have to be located in the"
		" same place as the binary.")
	 )

	 (set #config-backup-prompt (cat
		"Do you wish to overwrite your current PGP configuration"
		" with the new one from this archive? (Not"
		" recommended.)")
	 )
         (set #config-backup-help (cat
                "In most cases, this is not necessary. The configuration"
		" file has changed very little (if at all) from previous"
                " versions of PGP.\nI would, however, recommend that you"
		" read the the PGP documentation for any information on"
		" important configuration file changes.")
	 )
	 (set #config-backup-choices1 (cat
                #no ".")
	 )
         (set #config-backup-choices2 (cat
                #yes ".")
	 )
         (set #config-backup-choices3 (cat
		#yes ", but backup my old configuration first!")
	 )


	 (set #working-pgp-bin (cat
		"Installing " @app-name " binary.")
	 )
         (set #working-pgp-etc (cat
		"Installing " @app-name " support files.")
	 )
	 (set #working-config-backup (cat
		"Backing up old configuration.")
	 )
	 (set #working-ag (cat
		"Installing " @app-name " AmigaGuideŽ hypertext"
		" help files.")
	 )

	 (set #asksumming-prompt (cat
		"Should the data integrity of " @app-name
		" be checked before attempting to install?\n\n"
		"(This is recommended.)")
         )
	 (set #asksumming-help (cat
		"This will calculate an MD5SUM checksum for the "
		@app-name " archive and compare it with known values.\n\n"
		"This will take a few moments,"
		" depending on the speed of your system.")
	 )
	 (set #pgpsumming (cat
		"Checking the integrity of " @app-name "."
		"\n\nThis may take a few moments, depending on the"
		" speed of your system.")
	 )
	 (set #badpgpmsg (cat
		"WARING: This version of " @app-name " is"
                " incorrect, damaged, or has been tampered with. Please check the"
		" archive and the archive source carefully. If you"
		" suspect the archive has been modified please contact"
		" Peter Simons <simons@peti.GUN.de> immediately!")
	 )

	 (set #working-env-prompt (cat
		"Creating enviroment variable for " @app-name
		".")
	 )
	 (set #working-env-help (cat
		"This will create the necessary enviroment variables"
		" for the proper operation of " @app-name ".")
	 )

	 (set #bad-os (cat
		"PGP-Install requires OS 2.04 or newer.\n\n"
		"Time to upgrade your system!")
	 )

	 (set #ask-view-prompt (cat
		"Do you wish to read the ReadMe.Amiga file for "
		@app-name " now?\n\n"
                "(This is recommended.)")
	 )
         (set #ask-view-help (cat
		"This will display some important information you should"
		" know about " @app-name ". It is a good idea to read"
		" this before attempting to use the program.")
	 )


	)

	; Complain that English wasn't chosen as the language
	(
	 (abort (cat "At present the PGP-Installer currently only"
	             " supports English. In the future there will"
                     " hopefully be more support for other languages."
                     " My apologies for the inconvenience.\n\n"
                     "If you would like to help support a version of"
                     " the PGP-Installer in your language, please"
                     " contact me (Jacob Ellis <jellis@nah.oau.org>)"
                     " or Peter Simons <simons@peti.GUN.de> for more"
                     " information. Thank you!")
	 )
	)
) ;endif not-english



; ***********************************************************
; * start installer
; ***********************************************************

(if (< osver 37) ; Make sure OS 2.04 is running
	(abort #bad-os) ; abort if not OS 2.04
)

(welcome) ; start the main installer procedure

(complete 5)
(if (> 2 @user-level) ; ask if expert user wishes to check PGP binary
	(
		(set choice 1) ; novice and average get this by default
	)
	(
		(set choice (askbool (prompt #asksumming-prompt) (help #asksumming-help) (choices #yes #no) (default 1)))
	)
)
(complete 10)
(if (= choice 1) ; yes, a check is wanted
        (
		(working #pgpsumming)
;		(if (<> newpgpbinsum (getsum(newpgpbin))) ; checking
;			(abort #badpgpmsg) ; abort if not what was expected
		(if (run "contrib/md5sum/md5sum -c Readme.Amiga" (safe))
			(
				(abort #badpgpmsg)
			)
		)
	)
)



; ***********************************************************
; * Discover if PGP is already installed.
; * And if so, find where the binary and support files are
; ***********************************************************

(complete 13)

(if (exists "ENV:PGPPATH")
	(
                (set pgppath (getenv "PGPPATH"))
		(if (run "C:Which PGP NoRes" (safe))
			(
				(set pgpbinpath  (tackon @default-dest "PGP"))
			)
			(
				(run "setenv pgpinstall.tmp `C:Which PGP NoRes`" (safe))
				(set pgpbinpath (getenv "pgpinstall.tmp"))
        		        (set pgpbinpath (pathonly pgpbinpath))
				(delete "ENV:pgpinstall.tmp" (safe))
			)
		)
		(set pgpexists TRUE)
	)
	(
                (set pgpexists FALSE)
                (set pgpbinpath (tackon @default-dest "PGP"))
                (set pgppath (tackon @default-dest "PGP"))
	)
)



(complete 15)
; ***********************************************************
; * Discover if and where AmigaGuide files are kept
; ***********************************************************

(if (exists "libs:amigaguide.library") ; see if AmigaGuide lib exists

	; AmigaGuide exists
        (
	 (set agexists TRUE)
	 (if (exists "ENV:AmigaGuide/Path")
		( ; AmigaGuide user that read the manual
                	(set agpath (getenv "AmigaGuide/Path")) ; my personal favorite

			; This is a little Installer program that will parse an ENV:
			; variable and return the first item in the variable

			(set maxn (strlen agpath))
			(set n 0)
			(set inquote FALSE)
			(set agtmp "")
			(set afterlead FALSE)

			(while (<= n maxn)
				(
					(set skipchar FALSE)	; quotes can't be part of the path
								; so they must not be copied

					(set char (substr agpath n 1)) ; get character

					(if (AND (= " " char) (NOT afterlead)) ; skip any leading space
						(
			                        	(set skipchar TRUE)
			                        )
					)

					(if (AND (= char "\"") inquote) ; end of quote?
						(
							(set n maxn)        ; end quote
							(set skipchar TRUE) ; skip this char
						)
					)

					(if (AND (= char "\"") (NOT inquote)) ; start of quote?
						(
							(set inquote TRUE)  ; start quote
							(set skipchar TRUE) ; skip this char
						)
			                )

			                (if (AND afterlead (AND (NOT inquote) (OR (= char " ") (= char "\0")))) ; check if space or NULL and not in a quote
						(
							(set n maxn) ; found end of a path, get out
						)
			                )

			                (if (NOT skipchar)
			                        (
							(set agtmp (cat agtmp char))
							(set afterlead TRUE)
						)
					)

					(set n (+ n 1))
				)
			)

			(set agpath agtmp)
		)
		(
                        (if (exists (tackon "HELP:" @language))
				( ; OS2.1 or later
					(set agpath (tackon "HELP:" @language))
				)
				( ; OS2.04 (AmigaGuide not included)
					(set agpath "S:")
				)
                        )
		)
	 )
	) ;end amigaguide exists

	; AmigaGuide does not exist, AmigaGuide files will be placed
	; in the default destination
	(
	 (set agexists FALSE)
	)
)
(complete 18)



; ***********************************************************
; * Ask where to install various PGP stuff
; ***********************************************************

; make sure user really wants to install PGP binary here
(set pgpbinpath (askdir (prompt #pgp-bin-prompt) (help #pgp-bin-help) (default pgpbinpath) (newpath)))

(complete 20)
; make sure user really wants to install PGP support here
(set pgppath (askdir (prompt #pgp-path-prompt) (help #pgp-path-help) (default pgppath) (newpath)))

(complete 23)
; See if PGP might really be installed even though no ENV: variable
; This is necessary for the config stuff below
(if (exists (tackon pgppath pgpconfig))
	(
		(set pgpexists TRUE)  ; PGP really does exist at where the
	)                             ; users wants to install
	(
		(set pgpexists FALSE) ; PGP does NOT exist where the user
	)                             ; wants to install
                                      ; But it might really exist someplace else
)


(set @default-dest pgppath) ; when in doubt - the default destination

(complete 25)
; Ask where to install AmigaGuide files
(if agexists
	(       ; make sure user really wants to install PGP AmigaGuide files here
		(set agpath (askdir (prompt #pgp-ag-prompt) (help #pgp-ag-help) (default agpath) (newpath)))
	)
	(       ; User does not have AmigaGuide, but still needs the files for documentation
		(set agpath pgppath)
	)
)



; ***********************************************************
; * Discover if user is trying to install an older PGP
; ***********************************************************

(complete 27)
(if pgpexists
	(
		(set oldpgpbin (tackon pgpbinpath "PGP"))
		(if (exists oldpgpbin)
			(
				(if (= (getsize oldpgpbin) newpgpbinsize)
					(
						(if(askbool (prompt #already-installed) (help #already-installed-help) (default 0) (choices #yes #no))
		      					(set donothing TRUE)
							(abort #already-installed-abort)
		                 		)
		                	)
                                	(
				 		(if (earlier newpgpbin oldpgpbin)
							(
								(if(askbool (prompt #old-installed) (help #old-installed-help) (default 0) (choices #yes #no))
	      								(set donothing TRUE)
									(abort #old-installed-abort)
			                	 		)
				                	)
		        		 	)
					)
				)
			)
		)
	)
)



; ***********************************************************
; * Install PGP binary
; ***********************************************************

(complete 30)
(copyfiles (prompt #working-pgp-bin) (help (cat #pgp-bin-help "\n\n" @copyfiles-help)) (source newpgpbin) (dest pgpbinpath) (files) (confirm))



; ***********************************************************
; * Install PGP support files
; ***********************************************************

; for novice user, do not install new configuration file unless
;  this is a new installation.
; for intermediate or expert, offer to backup old configuration first

(if pgpexists
	(
		(complete 35)
		(set config-choice (askchoice (prompt #config-backup-prompt) (help #config-backup-help) (choices #config-backup-choices1 #config-backup-choices2 #config-backup-choices3)))
                (if (= 2 config-choice)
			(       ; backup old configuration
				(complete 40)
                                (set sourceconfig (cat (tackon pgppath pgpconfig)))
				(set destconfig (cat pgpconfig ".bak"))
				(copyfiles (prompt #working-config-backup) (help (cat #working-config-backup "\n\n" @copyfiles-help)) (source sourceconfig) (dest pgppath) (newname destconfig) (files) (confirm))
                        )
		)
		(if (< 0 config-choice)
                        (       ; user wnats new Config file
				(complete 45)
				(copyfiles (prompt #working-pgp-etc) (help (cat #working-pgp-etc "\n\n" @copyfiles-help)) (source newpgpconfig) (dest pgppath) (files) (confirm))
			)
		)
	)
	(       ; new installation, so new Config file needed (redundant!)
		(complete 40)
		(copyfiles (prompt #working-pgp-etc) (help (cat #working-pgp-etc "\n\n" @copyfiles-help)) (source newpgpconfig) (dest pgppath) (files) (confirm))
	)
)

; install rest of support files

(complete 45)

(copyfiles (prompt #working-pgp-etc) (help (cat #working-pgp-etc "\n\n" @copyfiles-help)) (choices "bin/es.hlp" "bin/fr.hlp" "bin/keys.asc" "bin/language.txt" "bin/pgp.hlp" "doc/keyserv.doc" "doc/newfor22.doc" "doc/newfor23.doc" "doc/pgformat.doc" "doc/PGP.faq" "doc/politic.doc" "doc/readme.doc" "doc/setup.doc") (dest pgppath) (source "") (files) (confirm))



; ***********************************************************
; * Install PGP AmigaGuide files
; ***********************************************************

; For non-AmigaGuide system, AmigaGuide files will be copied with
; the rest of the PGP support files.
; For AmigaGuide system, AmigaGuide files will be copied to
; the following (in order):
;	1. User-Specified location
; 	2. ENV:AmigaGuide/Path setting
;	3. HELP:(language) where language is the language in use
;	4. S: for OS2.04 systems that added AmigaGuide and haven't
;             bothered to set a special place for AmigaGuide files

(complete 75)

(copyfiles (prompt #working-ag) (help (cat #working-ag "\n\n" @copyfiles-help)) (source "") (dest agpath) (choices "doc/pgpdoc1.guide" "doc/pgpdoc2.guide") (files) (confirm))



; ***********************************************************
; * Set up ENV: and ENVARC: to point to where PGP was installed
; ***********************************************************

(if (NOT pgpexists) ; Don't do anything if this is an upgrade
	(       ;set ENV:
		(complete 85)
		(textfile (prompt #working-env-prompt) (help #working-env-help) (dest "ENV:PGPPATH") (append (expandpath pgppath)) (confirm))
	        ;set ENVARC:
		(complete 90)
		(textfile (prompt #working-env-prompt) (help #working-env-help) (dest "ENVARC:PGPPATH") (append (expandpath pgppath)) (confirm))
	)
)



; ***********************************************************
; * Installation basically complete, just show the readme now
; ***********************************************************

; View readme with:
;	1. ENV:PAGER
;	2. More if not OS 3.x
;	3. Multiview if OS 3.x

; NOTE: This has problems when run from workbench, which is
;       why full paths are given below.

(if (exists "ENV:PAGER")
	(
		(set pager (getenv "PAGER"))
	)
        (
                (if (< osver 39)
			(
                                (set pager "SYS:Utilities/More")
			)
			(
				(set pager "SYS:Utilities/Multiview")
        		)
		)
	)
)


(complete 95) ; 95% done


(if (> 2 @user-level) ; ask expert if they wish NOT to view the readme
	(
		(set choice 1) ; default yes
	)
	(
		(set choice (askbool (prompt #ask-view-prompt) (help #ask-view-help) (choices #yes #no) (default 1)))
	)
)
(if (= choice 1) ; yes, they want to read it
		(run (cat pager " ReadMe.Amiga") (safe))
)

(complete 100) ; 100% done

; The end! Phew!
