;
; Installer script for SortMail
;
; $VER: Install SortMail 1.2 (3.9.96)
;
; Some parts shamelessly ripped out of UpdateThor and modified to suit my needs
;
; Needs companion ARexx script checkver.rexx to work
;

; Initialization ***********************************************************

(set #welcome (cat "\nWelcome to the Installer for SortMail 3.31") )
(set #move-cfg "\nThe configuration was found in ENV(ARC):Thor/SortMail.cfg.\n\nAs SortMail 3.3 requires the configuration to be in the system's directory it has now been moved to:\n\n")
(set #old-cfg "\nAn old configuration was found in ENV(ARC):Thor/SortMail.cfg.\n\nThe format of the configuration files have changed in SortMail 3.0 and higher, so a new configuration will have to be created from scratch using the new CfgSortMail.thor.\n\nWhen you have reconfigured SortMail, please delete ENVARC:Thor/SortMail.cfg.")
(set #end-text "\nInstallation is now complete.\n\nPlease send suggestions and bug reports to eirikns@sn.no!")
(set #chk-fail "\nFailed to run checkver.rexx!\n\nPlease check that SYS:RexxC/rx and SYS:RexxC/WaitForPort exist and make sure you have started SYS:System/RexxMast.")

(makeassign "SM_Install" "" (safe) )

(welcome #welcome)

; Find Thor ****************************************************************

(if (exists "ENV:THOR/THORPath" (noreq) )
	(
		; Make sure THOR/THORPATH doesn't contain a linefeed
		(run "SetEnv THOR/THORPATH \"`GetEnv THOR/THORPATH`*N\"")

		(if (set Thor_Dir (getenv "THOR/THORPath") )
			(
				(set Thor_Dir (substr Thor_Dir 0 (- (strlen Thor_Dir) 1) ) )
				(set Thor_Dir (expandpath Thor_Dir) )
				(makeassign "Thor_SMInst" Thor_Dir (safe) )
			)
			(
				(abort "Couldn't find Thor.")
			)
		)
	)
	(
		(abort "Couldn't find Thor.")
	)
)


(set @default-dest Thor_Dir)

; Copy files and set protection bits for the scripts ***********************

(copyfiles
	(source "SM_Install:rexx")
	(dest "Thor_SMInst:rexx")
	(all)
	(optional "force" "askuser")
)

(complete 33)

(copyfiles
	(source "SM_Install:Docs")
	(dest "Thor_SMInst:docs")
	(all)
	(optional "force" "askuser")
	(infos)
	(noposition)
)

(protect "Thor_SMInst:rexx/SortMail.br" "-e +s")
(protect "Thor_SMInst:rexx/CfgSortMail.thor" "-e +s")
(protect "Thor_SMInst:rexx/Local2Email.br" "-e +s")
(protect "Thor_SMInst:rexx/RunCommand.br" "-e +s")

(complete 66)


; See if configuration files are outdated or need moving *******************

(if (exists "ENV:Thor/SortMail.cfg" (noreq) )
	(
		(set #version (run "SYS:RexxC/rx SM_Install:checkver.rexx") )

		(if (= #version 1)
			(message #old-cfg)
		)

		(if (= #version 2)
			(
				(set #sys-dir (getenv "SM_System") )
				(set #move-cfg (cat #move-cfg #sys-dir) )
				(message #move-cfg)
			)
		)

		(if (> #version 2)
			(abort #chk-fail)
		)
	)
)

(complete 100)

; Exit gracefully **********************************************************

(makeassign "SM_Install")
(makeassign "Thor_SMInst")
(run "Delete ENV:SM_System QUIET")

(exit #end-text)
