;***********************************************************
;* Installation script for MRQ, the MUI requester improver *
;* $VER: MRQ_Install 1.0 (15-Feb-98)                       *
;* © 1998 Matthias Bethke                                  *
;*                                                         *
;* ToDo: let user configure MRQ from within installer      *
;***********************************************************

(procedure expertmsg msg
	(if (> @user-level 1) (message (cat "\n\n" msg)))
)

(procedure F_GetDir _prompt _help _default
	(set #Where
		(askdir
			(prompt _prompt)
			(help _help)
			(default _default)
		)
	)
)

(procedure F_findconfig
	(set #Configfile "")
	(iconinfo (dest #Installname) (gettooltype "CONFIGFILE" "#Configfile"))
	(if	(not (strlen #Configfile))
		(
		(F_GetDir #P_WConfig #H_WConfig #WhereMRQ)
		(set #Configfile (tackon #Where "MRQ.config"))
		)
	)
)

(procedure F_findimages
	(set #WhereImages "")
	(iconinfo (dest #Installname) (gettooltype "IMAGES" "#WhereImages"))
	(if	(not (strlen #WhereImages))
		(
		(F_GetDir #P_WImages #H_WImages #WhereMRQ)
		(set #WhereImages (tackon #Where "MRQ-images"))
		)
	)
)

(procedure F_copyconfig
	(expertmsg (cat #M_CopyingConfig #Configfile))
	(copyfiles
		(source #cfgsrc)
		(dest (pathonly #Configfile))
		(newname "MRQ.config")
		(optional askuser)
	)
)

(procedure F_copymrq icon
	(if icon
		(
			(expertmsg (cat #M_CopyingMRQ #M_CopyingMRQIcon #M_To #WhereMRQ))
			(copyfiles (source "MRQ") (infos) (dest #WhereMRQ) (optional askuser))
		)
		(
			(expertmsg (cat #M_CopyingMRQ #M_To #WhereMRQ #M_KeepingMRQIcon))
			(copyfiles (source "MRQ") (dest #WhereMRQ) (optional askuser))
		)
	)
)



;************** START **************
(if (< @installer-version 2752524) (abort "This script requires Installer V42.12 or greater!"))

; !4 NOVICEs
(if (= @user-level 0) (set @user-level 1))

;no english-only config available yet
(if (= @language "english") (set #cfgsrc "MRQ.config") (set #cfgsrc "MRQ.config"))

;************** initialize variables **************
(set #M_Welcome "\n\nWelcome to\n\nMRQ\n\nThe MUI requester improver")
(set #E_Need020 "MRQ requires at least a 68020 CPU!")
(set #P_WInstall "Where do you want MRQ to be installed?")
(set #H_WInstall "The MRQ executable will be copied to this directory. If you want MRQ to start automatically on bootup, just accept the default.")
(set #P_WImages "Where do you want the images to be copied?\n(a directory will be created)")
(set #H_WImages "The installer will create a directory called\n\"MRQ-Images\"\ninside the directory you select and copy all images there")
(set #P_WConfig "Where do you want to keep the config file?\n")
(set #H_WConfig "A small file called \"MRQ.config\" will be copied to this directory")
(set #M_NoInstalledVer "There is no previously installed version")
(set #M_CopyingConfig "Copying config file as ")
(set #M_CopyingMRQ "Copying MRQ ")
(set #M_To "to ")
(set #M_CopyingMRQIcon "and its icon ")
(set #M_KeepingMRQIcon "\n(old icon will not be overwritten)")
(set #M_VersionToInstall "\n\nVersion to install: V")
(set #M_InstalledVersion "Already installed version: V")
(set #M_CfgExists1 "\n\nConfig file ")
(set #M_CfgExists2 " already exists - not overwritten")
(set #C_CheckNewImgs1 "Copy new images")
(set #C_CheckNewImgs2 "Don't copy anything")
(set #P_CheckImageDir "\n\nShould the distribution be checked for any new images that are not yet installed in ")
(set #H_CheckImageDir (cat
"If you select \"Copy new images\", installer will check for files in the distribution's MRQ-images "
"directory that you don't have in your image directory yet and copy them if it finds any.\n\n\"Don't "
"copy anything\" leaves your image directory as it is."
))
(set #M_CopyingAllImages "Copying all images to ")
(set #M_Copying1Img "Copying image: ")
(set #M_ImgDirUp2D "Image directory is up to date!")


;************** work starts **************
(message #M_Welcome)
(welcome)

(set #CPU (database "cpu"))
(if (< #CPU 68020) (abort #E_Need020))

(F_GetDir #P_WInstall #H_WInstall "SYS:wbstartup")
(set #WhereMRQ #Where)

(set @default-dest #WhereMRQ)
(set #Installname (tackon #WhereMRQ "MRQ"))

(set #Oldversion (getversion #Installname))
(set #MRQVersion (getversion "MRQ"))
(set #OldVer (/ #Oldversion 65536))
(set #OldRev (- #Oldversion (* #OldVer 65536)))
(set #MRQVer (/ #MRQVersion 65536))
(set #MRQRev (- #MRQVersion (* #MRQVer 65536)))
(set #OldExists (or #OldVer #OldRev))

(message (cat
	(cat #M_VersionToInstall #MRQVer "." #MRQRev "\n\n")
	(if #OldExists
		(cat #M_InstalledVersion #OldVer "." #OldRev)
		(#M_NoInstalledVer)
	))
)

(F_findconfig)
(F_findimages)

(if (not #OldExists)
	(
	(tooltype (dest "MRQ") (settooltype "CONFIGFILE" #Configfile))
	(tooltype (dest "MRQ") (settooltype "IMAGES" #WhereImages))
	(if (getversion "cybergraphics.library" (resident))
		(
		(tooltype (dest "MRQ") (settooltype "OK-IMG" (tackon #WhereImages "MRQ_OK.brush")))
		(tooltype (dest "MRQ") (settooltype "CANCEL-IMG" (tackon #WhereImages "MRQ_Cancel.brush")))
		)
	)
	(F_copymrq 1)
	)
	(F_copymrq 0)
)


(complete 30)

(if (not (exists #Configfile))
	(F_copyconfig)
	(message (cat  #M_CfgExists1 #Configfile #M_CfgExists2))
)

(complete 60)

(if (exists #WhereImages)
	(
		(if
			(askbool
				(prompt (cat #P_CheckImageDir #WhereImages " ?"))
				(help #H_CheckImageDir)
				(choices #C_CheckNewImgs1	#C_CheckNewImgs2)
			)
			(
				(foreach "MRQ-images" "#?"
					(
						(if	(not (exists (tackon #WhereImages @each-name)))
							(
								(expertmsg (cat #M_Copying1Img @each-name))
								(copyfiles (source (tackon "MRQ-images" @each-name)) (dest #WhereImages) (optional askuser))
							) 
						)
					)
				)
				(expertmsg #M_ImgDirUp2D)
			)
		)
	)
	(
		(expertmsg (cat #M_CopyingAllImages #WhereImages))
		(foreach "MRQ-images" "#?"
			(copyfiles (source (tackon "MRQ-images" @each-name)) (dest #WhereImages) (optional askuser))
		)
	)
)

(complete 100)
