; $VER: AFCD_Install 1.1 (6.8.99)
;   by Oliver Roberts (oliver.roberts@iname.com)

;*************
; LOCALE INIT
;*************

(if (= (exists "Env:Language") 1)
	(set @language (getenv "Language"))
)

;*****************
; ENGLISH STRINGS
;*****************

(set #str-yes "Yes")
(set #str-no "No")
(set #str-askfind "Copy AFCDFind to your hard drive?")
(set #str-findno "No - I'd rather use it from the CD")
(set #str-findprog "Yes - Just the program itself")
(set #str-findall "Yes - The program and all the indices")
(set #str-finddest "Select where the AFCDFind program files should be installed (no drawer will be created!)")
(set #str-askaminet "Copy the Aminet index listing to your hard drive?\n(requires approx 5Mb disk space)")
(set #str-copyaminetindex "Copying Aminet index file")
(set #str-settooltypes "Setting tooltypes")
(set #str-updating "Updating %s")
(set #str-updateinstaller "Checking to see whether Installer should be updated")
(set #str-updateview "Checking to see whether AFCDView needs to be copied to C: (i.e. if it's not there already or you have an old version")
(set #str-notoncd "Set the NOTONCD tooltype?\n(see help for info)")
(set #str-indexdir "Select where the AFCD index files should be installed")
(set #str-dopus (cat
	"If you wish to use the features in AFCDFind that utilise Directory "
	"Opus, you will need to install the support script for these "
	"features to work.  Install the script?"
))
(set #str-dopusdest "Select where the script should be installed")
(set #str-aminetdest "Select where the Aminet index file should be installed")
(set #str-complete (cat
	"Installation of your chosen files has now been completed\n\n"
	"The choices that you have made will be saved and will be used "
	"as the defaults when you next run this installer"
))
(set #str-installmode (cat
	"Do you wish to view/change your existing settings or just update "
	"the files using your current settings?"
))
(set #str-modenew "Change these settings")
(set #str-modeview "View settings")
(set #str-modeupdate "Update files")

(set #help-updateinstaller (cat
	"This installer script will automatically update your copy of Installer "
	"if you are using an older version"
))
(set #help-updateview (cat
	"This installer script will automatically update your copy of AFCDView "
	"which is required to view files on this CD via the AFCDPrefs system"
))
(set #help-find (cat
	"Although AFCDFind works just fine from the CD, you may wish to "
	"install it on your hard drive.  If so, then there are two different "
	"variations to choose from:\n\n"
	"1. Just the AFCDFind program files will be copied to your hard "
	"drive.  You could then create a \"left out\" icon via Workbench "
	"so you can easily access AFCDFind from Workbench.  However, for "
	"this to work, you'll still need an AFCD in your drive.\n\n"
	"2. In addition to copying all the AFCDFind program files to your "
	"hard drive, you can choose to copy all the CD index files too.  "
	"This may be faster, and it also means that you can use AFCDFind "
	"even if there is no AFCD in your CD drive.  However, you'll need "
	"to have quite a bit of spare space to use this option (30Mb+ at "
	"the time of writing!\n\n"
))
(set #help-notoncd (cat
	"Currently, setting this tooltype just changes the text in the Search "
	"cycle gadget.  Normally, there will be \"This CD\" and \"Last 5 CDs\" options. "
	"If you specify this tooltype, these will be replaced with the actual CD "
	"numbers. You may prefer this when running AFCDFind off your hard drive "
	"instead of the CD.\n\n"
))

;************
; PROCEDURES
;************

(procedure P_SaveChoices
	(textfile
		(dest "ENV:AFCD_Install")
		(append
			(cat
				("FINDINSTALL=%ld\n" #findinstall)
				("FINDDIR=%s\n" #finddir)
				("AMINETINSTALL=%ld\n" #aminetinstall)
				("AMINETDIR=%s\n" #aminetdir)
				("FINDDOPUS=%ld\n" #find-dopus)
			)
		)
		(safe)
	)
	(copyfiles
		(source "ENV:AFCD_Install")
		(dest "ENVARC:")
		(nogauge)
		(safe)
	)
)

(procedure P_ReadFindTooltypes
	(set #indexdir (tackon #finddir "AFCD_Indices"))
	(set #matchscript (tackon #finddir "AFCDFind-HandleMatch.dopus5"))
	(if (AND (exists (tackon #finddir "AFCDFind.info")) (>= @installer-version 2752524))
		(
			(set #find-notoncd "off")
			(iconinfo
				(dest (tackon #finddir "AFCDFind"))
				(gettooltype "NOTONCD" "#find-notoncd")
				(gettooltype "INDEXDIR" "#indexdir")
				(gettooltype "MATCHSCRIPT" "#matchscript")
				(safe)
			)
		)
	)
	(set #find-notoncd (<> "off" #find-notoncd))
	(if (= #indexdir (cat #cddevice ":+System+/Indices"))
		(set #indexdir (tackon #finddir "AFCD_Indices"))
	)
)

(procedure P_GetViewSettingsString
	(cat
		("Install AFCDFind?   %s\n" (select #findinstall #str-findno #str-findprog #str-findall))
		("Destination:   %s\n" #finddir)
		("Index dir:   %s\n" #indexdir)
		("NOTONCD tooltype:   %s\n" (if #find-notoncd "Set" "Unset"))
		("DOpus support:   %s\n\n" (if #find-dopus #matchscript "Off"))
		("Install Aminet Index?  %s\n" (if #aminetinstall #str-yes #str-no))
		("Destination:   %s" #aminetdir)
	)
)

;******
; INIT
;******

(if (= "AFCD" (substr (expandpath ":") 0 4))
	; started from an AFCD
	(set #cddevice (getdevice ":"))
	; not started from an AFCD (for testing purposes)
	(set #cddevice "CD0")
)

(set #finddir @default-dest)
(set #aminetdir @default-dest)
(set #findinstall 0)
(set #aminetinstall 0)
(set #find-notoncd 1)
(set #find-dopus 1)

(set #config (getenv "AFCD_Install"))
(set #configsize (strlen #config))

(if (> #configsize 0)
	(
		(set #i 0)

		(until (>= #i #configsize)
			(
				(set #k #i)
				(while (<> (substr #config #i 1) "=")
					(set #i (+ #i 1))
				)
				(set #var (substr #config #k (- #i #k)))
				(set #i (+ #i 1))
				(set #k #i)
				(while (<> (substr #config #i 1) "\n")
					(set #i (+ #i 1))
				)
				(if (> (- #i #k) 0)
					(
						(set #val (substr #config #k (- #i #k)))
						(if (= "FINDINSTALL" #var)
							(set #findinstall (+ #val))
						)
						(if (= "FINDDIR" #var)
							(set #finddir #val)
						)
						(if (= "AMINETINSTALL" #var)
							(set #aminetinstall (+ #val))
						)
						(if (= "AMINETDIR" #var)
							(set #aminetdir #val)
						)
						(if (= "FINDDOPUS" #var)
							(set #find-dopus (+ #val))
						)
					)
				)
				(set #i (+ #i 1))
			)
		)
	)
				
)

;*******************
; INTERACTIVE START
;*******************

(welcome)

(if (> #configsize 0)
	(
		(P_ReadFindTooltypes)
		(set #interactive 1)
		(set #interactive
			(askchoice
				(prompt (P_GetViewSettingsString))
				(help @askchoice-help)
				(choices (cat (if (< @installer-version 2752518) "" "\x1b[2p") #str-modenew) #str-modeupdate)
				(default #interactive)
			)
		)
	)
	; else
	(set #interactive 0)
)

(if (= #interactive 0)
	(
		(set #findinstall
			(askchoice
				(prompt #str-askfind)
				(help #help-find @askchoice-help)
				(choices (cat (if (< @installer-version 2752518) "" "\x1b[2p") #str-findno) #str-findprog #str-findall)
				(default #findinstall)
			)
		)

		(if #findinstall
			(
				(set #finddir
					(askdir
						(prompt #str-finddest)
						(help @askdir-help)
						(default #finddir)
					)
				)
				(P_ReadFindTooltypes)
				(if (= #findinstall 2)
					(set #indexdir
						(askdir
							(prompt #str-indexdir)
							(help @askdir-help)
							(default #indexdir)
							(newpath)
						)
					)
				)
				(set #find-notoncd
					(askchoice
						(prompt #str-notoncd)
						(help #help-notoncd @askchoice-help)
						(choices (cat (if (< @installer-version 2752518) "" "\x1b[2p") #str-no) #str-yes)
						(default #find-notoncd)
					)
				)
				(set #find-dopus
					(askchoice
						(prompt #str-dopus)
						(help @askchoice-help)
						(choices (cat (if (< @installer-version 2752518) "" "\x1b[2p") #str-no) #str-yes)
						(default #find-dopus)
					)
				)
				(if #find-dopus
					(set #matchscript (tackon
						(askdir
							(prompt #str-dopusdest)
							(help @askdir-help)
							(default #matchscript)
						)
						"AFCDFind-HandleMatch.dopus5")
					)
				)
			)
		)

		(set #aminetinstall
			(askchoice
				(prompt #str-askaminet)
				(help @askchoice-help)
				(choices (cat (if (< @installer-version 2752518) "" "\x1b[2p") #str-no) #str-yes)
				(default #aminetinstall)
			)
		)

		(if #aminetinstall
			(set #aminetdir
				(askdir
					(prompt #str-aminetdest)
					(help @askdir-help)
					(default #aminetdir)
				)
			)
		)
	)
	; else
	(
		(P_ReadFindTooltypes)
	)
)

;*******************
; COPY FILES ACROSS
;*******************

(onerror (P_SaveChoices))

(complete 0)

(working #str-updateinstaller)

(if (= 0 (run "C:Which Installer NORES >ENV:AFCDInst_tmp" (safe)))
	(
		(set #installerloc (getenv "AFCDInst_tmp"))
		(set #installerloc (substr #installerloc 0 (- (strlen #installerloc) 1)))
	)
	;else
	(set #installerloc "C:")
)
(delete "ENV:AFCDInst_tmp" (safe))

(copylib
	(prompt (#str-updating #installerloc))
	(help #help-updateinstaller)
	(source (cat #cddevice ":C/Installer"))
	(dest (pathonly #installerloc))
	(confirm)
)

(complete 8)

(working #str-updateview)

(copylib
	(prompt (#str-updating "C:AFCDView"))
	(help #help-updateview)
	(source (cat #cddevice ":C/AFCDView"))
	(dest "C:")
	(confirm)
)

(complete 10)

(if #findinstall
	(
		(copylib
			(source (cat #cddevice ":+System+/Find"))
			(dest #finddir)
			(newname "AFCDFind")
		)
		(if (NOT (exists (tackon #finddir "AFCDFind.info")))
			(
				(copyfiles
					(source (cat #cddevice ":+System+/Find.info"))
					(dest #finddir)
					(newname "AFCDFind.info")
				)
				(tooltype
					(prompt #str-settooltypes)
					(dest (tackon #finddir "AFCDFind"))
					(noposition)
				)
			)
		)
		; clear tooltypes first (bug in installer)
		(tooltype
			(prompt #str-settooltypes)
			(dest (tackon #finddir "AFCDFind"))
			(settooltype "NOTONCD")
			(settooltype "(NOTONCD)")
			(settooltype "FASTSTARTUP")
			(settooltype "(FASTSTARTUP)")
			(settooltype "HELPFILE")
			(settooltype "(HELPFILE")
			(settooltype "MATCHSCRIPT")
			(settooltype "(MATCHSCRIPT")
			(settooltype "INDEXDIR")
			(settooltype "(INDEXDIR")
		)
		; now set tooltypes
		(tooltype
			(prompt #str-settooltypes)
			(dest (tackon #finddir "AFCDFind"))
			(if (= 1 #find-notoncd) (settooltype "NOTONCD" "") (settooltype "(NOTONCD)" ""))
			(settooltype (if (= 2 #findinstall) "(FASTSTARTUP)" "FASTSTARTUP") "")
			(settooltype "HELPFILE" (tackon #finddir "AFCDFind.guide"))
			(settooltype "MATCHSCRIPT" #matchscript)
			(settooltype "INDEXDIR" (if (= #findinstall 2) #indexdir (cat #cddevice ":+System+/Indices")))
		)
		(if (exists (cat #cddevice ":+System+/Info/AFCDFind.guide"))
			(copyfiles
				(source (cat #cddevice ":+System+/Info/AFCDFind.guide"))
				(dest #finddir)
				(infos)
			)
		)
		(if #find-dopus
			(copyfiles
				(source (cat #cddevice ":+System+/AFCDFind-HandleMatch.dopus5"))
				(dest #finddir)
				(infos)
			)
		)
	)
)

(complete 30)

(if (= #findinstall 2)
	(
		(if (NOT (exists #indexdir))
			(makedir #indexdir (infos))
		)
		(foreach (cat #cddevice ":+System+/Indices") "(AFCD?|AFCD??)"
			(if (< @each-type 0)
				(
					(set #docopyidx 1)
					(set #idxname (cat #cddevice ":+System+/Indices/" @each-name))
					(if (exists (tackon #indexdir @each-name))
						(if (NOT (earlier (tackon #indexdir @each-name) #idxname))
							(set #docopyidx 0)
						)
					)
					(if #docopyidx
						(copyfiles
							(source #idxname)
							(dest #indexdir)
							(infos)
						)
					)
				)
			)
		)
	)
)

(complete 90)

(if #aminetinstall
	(copyfiles
		(prompt #str-copyaminetindex)
		(help @copyfiles-help)
		(source (cat #cddevice ":-In_the_Mag-/Reader_Requests/Aminet_Index"))
		(dest #aminetdir)
		(infos)
		(noposition)
		(optional nofail askuser)
		(confirm)
	)
)

;**************
; SAVE CHOICES
;**************

(complete 99)

(P_SaveChoices)

;*********
; THE END
;*********

(complete 100)

(exit #str-complete (quiet))
