;*************************************************************************
;	Title:
;		InstallThor
;*************************************************************************
;	Description:
;		Commodore Installer Script for the Thor 2.22 software
;*************************************************************************
;	Author:
;		yvind Ellefsen - versions for THOR upto 1.22
;       Petter Nilsen   - versions for 1.25 ->
;*************************************************************************
;	Still to do:
;		- Show .readme file
;		- Warn against MultiUser
;
;*************************************************************************
;
;******************************************************
;***** SET UP OUR VARIABLES AND OTHER SUCH STUFF ******
;******************************************************

; some useful variables
	(set true 1)
	(set false 0)
	(set yes true)
	(set no false)
	(set is_a_file 1)
	(set is_a_dir 2)
	(set quote "\"")
	(set newline "\n")
	(set nothing "")
	(set bbspack 0)
	(set inetpack 0)

; set up our delete options
	(delopts "AskUser" "OkNoDelete" "Force")

; some useful procedures
	; change userlevel to expert
	(procedure expert_level
		(
			(user 2)
		)
	)

; reset userlevel back to default
	(procedure default_level
		(
			(user default-level)
		)
	)

; store off userlevel
	(procedure save_default_level
		(
			(set default-level @user-level)
		)
	)

; some overused strings
(set omp (cat "One moment please..." newline))

; introduce ourselfs to the viewers
(welcome "Welcome to the Thor installer.  This installer uses "
	"the Commodore Amiga Installer.  All of our future Thor releases "
	"will be using this installer and we would like to get any feedback "
	"that might help to improve the installation procedure." newline
)
; first reset the user level so that the novice can see whats going on
	(save_default_level)
	(expert_level)

;Make assign to install from
	(makeassign "Thor_Install" "" (safe))


;******************
;** Check Memory **
;******************

(run "Avail flush")

(set Running (run "Thor_Install:CheckForLib"))

(while (= Running 20)
  	(
		(Message newline "Thor seem to be running, you MUST close it NOW."	
				newline "If you don't, the installation will fail!")
		(run "Avail flush")
		(set Running (run "Thor_Install:CheckForLib"))
	)
)

;************************
;** Start Installation **
;************************

(if (exists "Thor:libs/bbsread.library" (noreq))
; Then
	((set end_text "Please use the UpdateThor script to\nupdate your already installed THOR.")
	(abort end_text))
)

(if (exists "BBSData:global.config" (noreq))
; Then
	((set end_text "Please use the UpdateThor script to\nupdate your already installed THOR.")
	(abort end_text))
)

(if (exists "envarc:THOR/BBSDataPath" (noreq))
	(
		(if	(askbool
	   			(prompt 
						"THOR seems to be already installed, "
						"do you want to install it again from scratch?")
				(help 
						"The installer script has found a previous "
						"installation of THOR.\n\n"
						"Select YES if you want it install again, otherwise NO")
				(default 1)
			)
			; Then
			(
				(run "delete envarc:thor all force")
				(run "delete env:thor all force")
			)
			; Else
			(
				(set end_text "Please use the UpdateThor script to\nupdate your already installed THOR.")
				(abort end_text)
			)
		)
	)
)

(set Thor_Dir 
	(askdir
		(prompt "Where would you like Thor installed?"
		newline "The installer will NOT create a directory!")
		(help @askdir-help)
		(default "Work:")
		(newpath)
	)
)
(set Thor_Dir (expandpath Thor_Dir))

; before we go on lets reset the user's level back to what it was
(default_level)

; verify that the Thor directory exists
(if (<> (exists Thor_Dir) is_a_dir)
	(
		(makedir Thor_Dir
			(prompt
				"The directory you have selected for Thor does not "
				"seem to exist.  Do you want us to create it for you?"
			)
			(help @makedir-help)
			(infos)
			(confirm)
		)
	)
)

; Create directories for the THOR installation

(makeassign "Thor" Thor_dir)
(makedir "Thor:Data")
(makeassign "BBSData" "Thor:data")

;***********************************
;***** GET FIRST DISK IN HERE ******
;***********************************

; first copy over the stuff
(working omp "Decompressing and copying Thor files.")

(run "Thor_Install:lha -a x Thor_Install:thor.lha THOR:" )

;************************
;**  Copy the keyfile  **
;************************
(if (exists "Thor_Install:THOR.key" (noreq))
; Then
	(copyfiles
		(source "Thor_Install:THOR.key")
		(dest "THOR:")
		(nogauge)
	)
)
		
;*****************************************
;**  Copy the default arexx macro file  **
;*****************************************
(if (exists "Thor_Install:THOR.macros" (noreq))
; Then
	(copyfiles
		(source "Thor_Install:THOR.macros")
		(dest "THOR:")
		(nogauge)
	)
)

(default_level)
		
;***********************************
; Create the environment variables *
;***********************************

(set Running (run "Thor:bin/basemanager convert >con:0/0/640/200/Converting/AUTO/CLOSE/WAIT"))
(if (<> 0 Running)
(
	((set end_text "Basemanager failed in setting up the env-variables.")
	(abort end_text)))
)

;******************
;** Locale stuff**
;******************

(default_level)

(set catalog
   (askoptions
	  (prompt "What languages would you like to install ?\n"
            "English is the built-in language.")
	  (help
			"This will install locale files for other languages than English. "
			"You can set the prefered language on Workbench and in Thor by"
            "using the prefs:locale tool." newline @askchoice-help
	  )
      (choices "Norsk" "Dansk" "Svenska" "Deutsch")
      (default 0)
   )
)

(makedir "thor:catalogs")

(if (bitand 1 catalog)
	(copyfiles
		(source "Thor_Install:catalogs/norsk")
		(dest "thor:catalogs/norsk")
		(all)
	)
)

(if (bitand 2 catalog)
	(copyfiles
		(source "Thor_Install:catalogs/dansk")
		(dest "thor:catalogs/dansk")
		(all)
	)
)

(if (bitand 4 catalog)
	(copyfiles
		(source "Thor_Install:catalogs/svenska")
		(dest "thor:catalogs/svenska")
		(all)
	)
)

(if (bitand 8 catalog)
	(copyfiles
		(source "Thor_Install:catalogs/deutsch")
		(dest "thor:catalogs/deutsch")
		(all)
	)
)

;*********************
;** reqtools.libary **
;*********************

(copylib
    (prompt "Copying ReqTools library")
    (help "This will copy the ReqTools library." newline @copylib-help)
    (source "Thor_Install:reqtools.library")
    (dest "libs:")
)

;***********
;** Fonts **
;***********
    
(copyfiles
    (prompt "Choose the fonts to install with Thor" newline "These are not required for Thor to run" )
	(help "This will copy the default font files for Thor." newline @copyfiles-help)
	(source "Thor_Install:Fonts")
	(dest "Fonts:")
	(fonts)
    (choices "Grn" "Thin609" "Thin611" "Thin711")
    (confirm)
)

;**************************************
;* Install the bbsread database stuff *
;**************************************

(set failed (run "thor:bin/initarc"))
(if (<> 0 failed)
	(
		((set end_text "Installation failed.\nIf you received any error message,\nplease write this down and send us\na mail about the problem.")
		(abort end_text))
	)
)

(run "thor:bin/InitCharsets")

(if (exists "Thor_Install:bin/parseqwk" (noreq))
	; Then
	(
		(set bbspack 1)
	)
	; Else
	(
		(if	(askbool
				(prompt newline 
								"Would you like to install the modules for "
								"QWK, Fido, Hippo, Bluewave, ABBS/MBBS and Omen "
								"to be used with Bulletin Board Systems?\n\n "
								"(you will be able to select which modules to "
								"install later in this installtions procedure).")
				(help newline
								"This section of the installation is dedicated "
								"to installation of some common formats "
								"required if you want to use THOR with BBSes.\n\n"
			    		    	"Select YES if you want to continue with this "
								"installation, otherwise NO")
		        (default 1)
			)
			; Then 
			(
				(set bbsfile 
					(askfile
						(prompt 
								"Please select the BBS archive for THOR 2.2. "
								"(Usually named 'thor22_bbs.lha').\n\n"
								"It will be unarchived automatically.")
						(help @askfile-help)
						(default "sys:")
					)
				)
				; verify that the file exists
				(if (exists bbsfile)
					(
						(set runfile (cat runfile "Thor_Install:lha -a x " quote bbsfile quote " Thor_Install:"))
						(set failed (run runfile))
						(if (<> 0 failed)
						(
							((set failed_text "Unarchiving of the BBS archive failed.")
							(message end_text)))
						)
						; Else
						(
							(set bbspack 1)
						)
					)
				)
			)
		)
	)
)

(if (exists "Thor_Install:bin/gettcp" (noreq))
	; Then
	(
		(set inetpack 1)
	)
	; Else
	(
		(if	(askbool
				(prompt newline 
								"Would you like to install the modules for "
								"UUCP, SOUP, NNTP, SMTP and POP3 "
								"to be used with Internet?\n\n"
								"(you will be able to select which modules to "
								"install later in this installtions procedure).")
				(help newline
								"This section of the installation is dedicated "
								"to installation of some common formats "
								"required if you want to use THOR with Internet.\n\n"
			    		    	"Select YES if you want to continue with this "
								"installation, otherwise NO")
		        (default 1)
			)
			; Then 
			(
				(set inetfile 
						(askfile
							(prompt 
									"Please select the Internet archive for THOR 2.2\n"
									"(Usually named 'thor22_inet.lha').\n\n"
									"It will be unarchived automatically."
							(help @askfile-help)
							(default "sys:")
						)
					)
				)
				; verify that the file exists
				(if (exists inetfile)
					(
						(set runfile2 (cat runfile2 "Thor_Install:lha -a x " quote inetfile quote " Thor_Install:"))
						(set failed (run runfile2))
						(if (<> 0 failed)
						(
							((set failed_text "Unarchiving of the Internet archive failed.")
							(message end_text)))
						)
						; Else
						(
							(set inetpack 1)
						)
					)
				)
			)
		)
	)
)

(if (= bbspack 1)
	(
		(set bbs_type
		   (askoptions
			  (prompt
						"Please select which BBS modules "
						"you want to install.")
			  (help
					"What system type for BBS usage that should be "
					"installed can be selected here."
			  )

		      (choices
					"QWK        - Normal QWK"
					"Fido       - Normal Fidonet"
					"BlueWave   - Normal BlueWave"
					"ABBS       - Normal ABBS"
					"ABBS_QWK   - ABBS with QWK support"
					"MBBS       - Normal MBBS"
					"Hippo/BBBS - Normal Hippo V2 and Hippo on BBBS"
					"Omen       - Normal Omen"
			  )
		      (default -1)
		   )
		)
		(copyfiles
			(source "Thor_Install:scripts")
			(dest "THOR:scripts")
			(pattern "#?BBS#?")
		)
		(if (bitand 1 bbs_type)
			(
				(copyfiles
					(source "Thor_Install:bin")
					(dest "THOR:bin")
					(pattern "#?QWK")
				)
				(copyfiles
					(source "Thor_Install:s/cfgqwk")
					(dest "THOR:s")
				)
				(execute "thor:s/cfgqwk")
			)
		)
		(if (bitand 2 bbs_type)
			(
				(copyfiles
					(source "Thor_Install:bin")
					(dest "THOR:bin")
					(pattern "#?FIDO")
				)
				(copyfiles
					(source "Thor_Install:s/cfgfido")
					(dest "THOR:s")
				)
				(execute "thor:s/cfgfido")
			)
		)
		(if (bitand 4 bbs_type)
			(
				(copyfiles
					(source "Thor_Install:bin")
					(dest "THOR:bin")
					(pattern "#?BLUE")
				)
				(copyfiles
					(source "Thor_Install:s/cfgblue")
					(dest "THOR:s")
				)
				(execute "thor:s/cfgblue")
			)
		)
		(if (bitand 8 bbs_type)
			(
				(copyfiles
					(source "Thor_Install:bin/packambbs")
					(dest "THOR:bin")
				)
				(copyfiles
					(source "Thor_Install:bin/parsemsg")
					(dest "THOR:bin")
				)
				(copyfiles
					(source "Thor_Install:s/cfgabbs")
					(dest "THOR:s")
				)
				(execute "thor:s/cfgabbs")
			)
		)
		(if (bitand 16 bbs_type)
			(
				(copyfiles
					(source "Thor_Install:bin")
					(dest "THOR:bin")
					(pattern "#?QWK")
				)
				(copyfiles
					(source "Thor_Install:s/cfgabbs_qwk")
					(dest "THOR:s")
				)
				(execute "thor:s/cfgabbs_qwk")
			)
		)
		(if (bitand 32 bbs_type)
			(
				(copyfiles
					(source "Thor_Install:bin/packambbs")
					(dest "THOR:bin")
				)
				(copyfiles
					(source "Thor_Install:bin/parsemsg")
					(dest "THOR:bin")
				)
				(copyfiles
					(source "Thor_Install:s/cfgmbbs")
					(dest "THOR:s")
				)
				(execute "thor:s/cfgmbbs")
			)
		)
		(if (bitand 64 bbs_type)
			(
				(copyfiles
					(source "Thor_Install:bin")
					(dest "THOR:bin")
					(pattern "#?hippo")
				)
				(copyfiles
					(source "Thor_Install:s/cfghippo")
					(dest "THOR:s")
				)
				(copyfiles
					(source "Thor_Install:s/cfgbbbs")
					(dest "THOR:s")
				)
				(execute "thor:s/cfghippo")
				(execute "thor:s/cfgbbbs")
			)
		)
		(if (bitand 128 bbs_type)
			(
				(copyfiles
					(source "Thor_Install:bin")
					(dest "THOR:bin")
					(pattern "#?omen")
				)
				(copyfiles
					(source "Thor_Install:s/cfgomen")
					(dest "THOR:s")
				)
				(execute "thor:s/cfgomen")
			)
		)
	)
)

(if (= inetpack 1)
	(
		(set bbs_type
		   (askoptions
			  (prompt
						"Please select which Internet modules "
						"you want to install.")
			  (help
					"What system type for Internet usage that should be "
					"installed can be selected here."
			  )

		      (choices
						"TCP/TCP_ONLINE - POP3, SMTP and NNTP"
						"SOUP/UQWK_SOUP - Normal SOUP and SOUP w/uqwk"
						"UUCP           - Normal UUCP"
			  )
		      (default -1)
		   )
		)

		(copyfiles
			(source "Thor_Install:libs/utnet.library")
			(dest "THOR:libs")
		)

		(copyfiles
			(source "Thor_Install:bin/InitRFC")
			(dest "THOR:bin")
		)

		(copyfiles
			(source "Thor_Install:scripts")
			(dest "THOR:scripts")
			(pattern "#?UQWK#?")
		)

		(if (bitand 1 bbs_type)
			(
				(copyfiles
					(source "Thor_Install:bin")
					(dest "THOR:bin")
					(pattern "#?TCP")
				)
				(copyfiles
					(source "Thor_Install:")
					(dest "THOR:")
					(pattern "ConnectTHOR#?")
				)
				(copyfiles
					(source "Thor_Install:s/cfgtcp")
					(dest "THOR:s")
				)
				(copyfiles
					(source "Thor_Install:s/cfgtcp_online")
					(dest "THOR:s")
				)
				(execute "thor:s/cfgtcp")
				(execute "thor:s/cfgtcp_online")
			)
		)
		(if (bitand 2 bbs_type)
			(
				(copyfiles
					(source "Thor_Install:bin")
					(dest "THOR:bin")
					(pattern "#?SOUP")
				)
				(copyfiles
					(source "Thor_Install:s")
					(dest "THOR:s")
					(pattern "#?soup")
				)
				(makedir "THOR:unix")
				(copyfiles
					(source "Thor_Install:unix")
					(dest "THOR:unix")
					(all)
				)
				(execute "thor:s/cfguqwk_soup")
				(execute "thor:s/cfgsoup")
			)
		)
		(if (bitand 4 bbs_type)
			(
				(copyfiles
					(source "Thor_Install:bin")
					(dest "THOR:bin")
					(pattern "#?UUCP")
				)
				(copyfiles
					(source "Thor_Install:s/cfguucp")
					(dest "THOR:s")
				)
				(execute "thor:s/cfguucp")
			)
		)
	)
)

;*****************************
;* Simple configuration here *
;*****************************

(if	(askbool
	(
		(prompt newline "The following section in this installation "
						"is used for setting up a download and an upload "
						"directory for message packets.  This section is "
						"optional, but the directories needs to be "
						"set in THOR if you do not set them up here.\n\n"
						"Do you want to proceed with this?")
		(help newline "This section of the installation is dedicated "
						"to some first-time parameters that THOR "
						"needs to have configured.\n\n"
			        	"Select YES if you want to continue with this "
						"configuration, otherwise NO")
        (default 1)
		)
	)
	; Then 
	(
		(set DL_Dir 
				(askdir
					(prompt "Where do you keep your downloaded message packets?\n\n"
							"This is usually the download directory "
							"configured in the communication program you "
                            "use."
					(help @askdir-help)
					(default "Work:")
					(newpath)
				)
			)
		)
		(set DL_Dir (expandpath DL_Dir))
		
		; verify that the download directory exists
		(if (<> (exists DL_Dir) is_a_dir)
			(
				(makedir DL_Dir
					(prompt
						"The directory you have selected for downloaded "
						"message packets does not seem to exist.\n\n"
						"Do you want it to be created for you?")
					(help @makedir-help)
					(infos)
					(confirm)
				)
			)
		)
		(if (= (exists DL_Dir) is_a_dir)
			(
				(set dlstr (cat dlstr "sys:rexxc/rx THOR:rexx/cfgglobal.br DNL " quote DL_Dir quote))
				(run dlstr)
				(run dlstr)
			)
		)

		(set UL_Dir 
				(askdir
					(prompt "Where would you like to keep any reply "
							"message packets that THOR generates?\n\n"
							"This is usually the upload directory "
							"configured in your communication program."
					(help @askdir-help)
					(default "Work:")
					(newpath)
				)
			)
		)
		(set UL_Dir (expandpath UL_Dir))
		
		; verify that the download directory exists
		(if (<> (exists UL_Dir) is_a_dir)
			(
				(makedir UL_Dir
					(prompt
						"The directory you have selected for upload of "
						"reply packets does not seem to exist.\n\n"
						"Do you want it to be created for you?")
					(help @makedir-help)
					(infos)
					(confirm)
				)
			)
		)
		(if (= (exists UL_Dir) is_a_dir)
			(
				(set ulstr (cat ulstr "sys:rexxc/rx THOR:rexx/cfgglobal.br UPL " quote UL_Dir quote))
				(run ulstr)
			)
		)
	)
)

;*****************************
;****** Install icons ********
;*****************************

(set icon
	(askchoice
    	(prompt
			"Please select what icon-set to install with THOR.\n")
		(help
			"If you use MagicWB on your Workbench, "
			"you might want to install the special MagicWB icons.\n\n"
			"Likewise, if you use NewIcons on your Workbench, "
			"you might want to install the special NewIcons icons.")
		(choices "Normal icons" "MagicWB icons" "NewIcons icons")
        (default 0)
	)
)

(if (= icon 1)
	(
		(copyfiles
			(prompt "Copying MagicWB icon files")
			(source "Thor_Install:MWBIcons")
			(dest "Thor:")
			(pattern "~(THOR.guide.info)")
			(nogauge)
		)
		(copyfiles
			(prompt "Copying MagicWB icon files")
			(source "Thor_Install:MWBIcons/THOR.guide.info")
			(dest "Thor:Docs")
			(newname "THOR.guide.info")
			(nogauge)
		)
		(copyfiles
			(prompt "Copying MagicWB icon files")
			(source "Thor_Install:MWBIcons/THOR.guide.info")
			(dest "Thor:Docs")
			(newname "ConfigTHOR.guide.info")
			(nogauge)
		)
		(copyfiles
			(prompt "Copying MagicWB icon files")
			(source "Thor_Install:MWBIcons/THOR.guide.info")
			(dest "Thor:Docs")
			(newname "ARexx.guide.info")
			(nogauge)
		)
		(copyfiles
			(prompt "Copying MagicWB icon files")
			(source "Thor_Install:MWBIcons/THOR.guide.info")
			(dest "Thor:Docs")
			(newname "BBSReadRexx.guide.info")
			(nogauge)
		)
		(copyfiles
			(prompt "Copying MagicWB icon files")
			(source "Thor_Install:MWBIcons/THOR.guide.info")
			(dest "Thor:Docs")
			(newname "THOR_Rexx.guide.info")
			(nogauge)
		)
		(copyfiles
			(prompt "Copying MagicWB icon files")
			(source "Thor_Install:MWBIcons/THOR.guide.info")
			(dest "Thor:Docs")
			(newname "Installation.guide.info")
			(nogauge)
		)
	)
)
(if (= icon 2)
	(
		(copyfiles
			(prompt "Copying NewIcons icon files")
			(source "Thor_Install:NewIcons")
			(dest "Thor:")
			(pattern "~(THOR.guide.info)")
			(nogauge)
		)
		(copyfiles
			(prompt "Copying NewIcons icon files")
			(source "Thor_Install:NewIcons/THOR.guide.info")
			(dest "Thor:Docs")
			(newname "THOR.guide.info")
			(nogauge)
		)
		(copyfiles
			(prompt "Copying NewIcons icon files")
			(source "Thor_Install:NewIcons/THOR.guide.info")
			(dest "Thor:Docs")
			(newname "ConfigTHOR.guide.info")
			(nogauge)
		)
		(copyfiles
			(prompt "Copying NewIcons icon files")
			(source "Thor_Install:NewIcons/THOR.guide.info")
			(dest "Thor:Docs")
			(newname "ARexx.guide.info")
			(nogauge)
		)
		(copyfiles
			(prompt "Copying NewIcons icon files")
			(source "Thor_Install:NewIcons/THOR.guide.info")
			(dest "Thor:Docs")
			(newname "BBSReadRexx.guide.info")
			(nogauge)
		)
		(copyfiles
			(prompt "Copying NewIcons icon files")
			(source "Thor_Install:NewIcons/THOR.guide.info")
			(dest "Thor:Docs")
			(newname "THOR_Rexx.guide.info")
			(nogauge)
		)
		(copyfiles
			(prompt "Copying NewIcons icon files")
			(source "Thor_Install:NewIcons/THOR.guide.info")
			(dest "Thor:Docs")
			(newname "Installation.guide.info")
			(nogauge)
		)
	)
)

(if (exists "env:sys/def_drawer.info" (noreq))
	(copyfiles
		(prompt "Copying default drawer icon")
		(help "This will copy the default drawer icon." newline @copyfiles-help)
		(source "env:sys/def_drawer.info")
		(dest "Thor:")
		(newname "Docs.info")
		(infos)
	)
)


;*****************************
;***** WE ARE ALMOST DONE ****
;*****************************

; make sure that default-dir is pointing to the right place
(set @default-dest Thor_Dir)
	
(makeassign "Thor_Install") 	
(makeassign "BBSData")
(makeassign "Thor_Disk" (safe))

; final message for our viewers
	(set end_text (cat "Hope you like THOR! Don't hesitate to "
					"send us comments, bugreports and suggestions."))

; now for the exit
	(exit end_text)

