;
; BOGL installer
;

;;
;; Want at least OS v39
;;
(if (< (/ (getversion) 65536) 39)
	(
		(message "You need at least OS v39")
		(exit (quiet))
	)
)

;;
;; BOGL only makes sense with AmigaMesaRTL
;;
(if (not (exists "libs:mesamain.library"))
	(
		(message "You don't appear to have AmigaMesaRTL installed. BOGL needs it.")
		(exit (quiet))
	)
)

;;
;; AmigaMesaRTL should be at least v2
;;
(if (< (/ (getversion "libs:mesamain.library") 65536) 2)
	(
		(message "You need at least v2.0 or AmigaMesaRTL installed")
		(exit (quiet))
	)
)

;;
;; Excuse me Sir, where is the madhouse?
;;
(if (exists "SYS:Tools/Madhouse")
	(set #maddir "SYS:Tools/Madhouse")
	(if (exists "SYS:Utilities/Madhouse")
		(set #maddir "SYS:Utilities/Madhouse")
		(if (exists "SYS:Madhouse")
			(set #maddir "SYS:Madhouse")
			(set #maddir
				(askdir
					(prompt "Where is the Madhouse directory?")
					(help @askdir-help)
					(default "SYS:")
				)
			)
		)
	)
)

(set #madblankdir
	(tackon #maddir "blankers")
)

;;
;; Make sure the blankers dir is where we expect it to be
;;
(if (not (exists #madblankdir))
	(set #madblankdir
		(askdir
			(prompt "Can't find Madhouse blankers directory.\nWhere is it?")
			(help @askdir-help)
			(default #madblankdir)
		)
	)
)

(set #boglblankdir
	(tackon #madblankdir "BOGL")
)

(set #boglblanker
	(tackon #boglblankdir "blanker")
)

;;
;; Copy main BOGL module
;;
(copylib
	(prompt "Copying BOGL module to " #boglblankdir)
	(source "blanker/blanker")
	(dest #boglblankdir)
)

;;
;; Install Blank output handler
;;
(copylib
	(prompt "Copying Blank output handler")
	(source "outputhandlers/Blank")
	(dest "libs:outputhandlers/")
)

;;
;; Oooh, look at all those blanker options!
;;
(copyfiles
	(prompt "Copying BOGL gadget file to " #boglblankdir)
	(source "blanker/gadget")
	(dest #boglblankdir)
	(files)
)

;;
;; If there isn't a programme already, install one
;;
(set #boglprogramme
	(tackon #boglblankdir "BOGL.programme")
)

(if (not (exists #boglprogramme))
	(copyfiles
		(prompt "Copying example BOGL programme to " #boglblankdir)
		(source "blanker/BOGL.programme")
		(dest #boglblankdir)
		(files)
	)
)

;;
;; If there isn't a preference setting, install one
;;
(set #boglprefs
	(tackon #boglblankdir "pref")
)

(if (not (exists #boglprefs))
	(copyfiles
		(prompt "Copying BOGL preference file to " #boglblankdir)
		(source "blanker/pref")
		(dest #boglblankdir)
		(files)
	)
)

;;
;; Make sure the AmigaMesaRTL/Window variable is set
;;
(set #windowenv "AmigaMesaRTL/Window")

(if (= "" (getenv #windowenv))
	(
		(if (exists "libs:outputhandlers/dl1")
			(set #defwindowhandler "libs:outputhandlers/dl1")
		)
		(if (exists "libs:outputhandlers/dl1plus")
			(set #defwindowhandler "libs:outputhandlers/dl1plus")
		)
		(set #windowhandler
			(askfile
				(prompt "The AmigaMesaRTL/Window environment variable must be set to an output handler for windows.\nMost likely you want this to be DL1Plus or DL1")
				(help @askdir-help)
				(default #defwindowhandler)
			)
		)
		(textfile
			(dest (tackon "ENV:" #windowenv))
			(append (fileonly #windowhandler))
		)
		(textfile
			(dest (tackon "ENVARC:" #windowenv))
			(append (fileonly #windowhandler))
		)
	)
)

;;
;; Copy guide file
;;
(if (exists "Help:english")
	(set #helpdir "Help:english")
	(if (exists "Help:")
		(set #helpdir "Help:")
		(set #helpdir
			(askdir
				(prompt "Where do you want the BOGL Guide file?")
				(help @askdir-help)
				(default #boglblankdir)
			)
		)
	)
)

(copyfiles
	(prompt "Copying guide file to " #helpdir)
	(source "BOGL.guide")
	(dest #helpdir)
	(files)
	(infos)
)

;;
;; Yay! We're done
;;
(message "The blanker is in:\n" #boglblankdir "\nThe AmigaGuide file is in\n" #helpdir)
(message "Everything is now (probably) installed.\nYou should now edit the programme\n(" #boglprogramme script ")\nto suit your system before using BOGL,\n and set the preferences in Madhouse" (all))
(set @default-dest #boglblankdir)
