;===============================================================
;$VER: SUPERVISOR-Install 1.0 (19.9.95)
;Installer script for SUPERVISOR Copyright © 1997 THE 1702 GROUP
;===============================================================

;** This script must be used with CBM-Installer. (It should be located	 **
;** in "SYS:Utilities/". You should start it from its icon. It's assumed **
;** that your system software is properly installed and initialized (ie: **
;** the workbench has been loaded and all system files are available).	 **

;** to do: UnInstall based on information located in ENV:SUPERVISOR/     **

(working "\n\n\n--- Setting up ... please wait ---")
(set #__debug (= @app-name "__debug"))
(set #cpu (database "cpu"))
(set #vblank (+ (database "vblank")))
(set #gfxmem (+ (database "graphics-mem")))
(set #totmem (+ (database "total-mem")))
(set #kick (/ (getversion "exec.library" (resident)) 65536))
(set #demoflag (not (or (exists "Licenza") (exists "Licence"))))
(set #vernum (getversion "tools/mon"))
(set #svver (/ #vernum 65536))
(set #svrev (- #vernum (* #svver 65536)))
(set #agflag (set #vernum (getversion "libs:amigaguide.library")))
(set #agver (/ #vernum 65536))
(set #agrev (- #vernum (* #agver 65536)))
(set #agstat (if (exists "ENVARC:" (noreq)) 1 0)) ;(exists) results: 0(not found) 1(file) 2(dir)
(set #agstat (if (exists "ENVARC:AmigaGuide" (noreq)) 2 #agstat))
(set #agstat (+ #agstat (exists "ENVARC:AmigaGuide/Path" (noreq))))
(set @execute-dir (pathonly @icon))
(if (not (and (exists "Tools/Mon") #svver)) (abort "\n\n\nSome files in this distribution appear to be altered or missing"))
(run "which >ENV:SVI_Guide2DocPath.tmp Guide2Doc" (safe))
(set #guide2doc "__dummy__")
(if (getsize "ENV:SVI_Guide2DocPath.tmp") ((set #guide2doc (substr (getenv "SVI_Guide2DocPath.tmp") 0 (- (getsize "ENV:SVI_Guide2DocPath.tmp") 1)))))
;   ^(getenv returns an error if file is empty!
(delete "ENV:SVI_Guide2DocPath.tmp" (prompt "Removing temp var") (help "") (safe))


;--- Procedure: P_InstallDocs(#inst_mode) ----
(procedure P_InstallDocs	;BEGIN: P_InstallDocs()
(set #docmask (bitor (if #agver %1 %0) (if (exists (cat #guide2doc)) %10 %00)))
(set #docmask (bitor #docmask (if #demoflag %000 %100)))
(set #docopt (askoptions (prompt "Choose the files to install:")
			(help @askoptions-help)
			(if #demoflag
				(choices "AmigaGuide docs"
					 "ASCII docs"
				)
				(choices "AmigaGuide docs"
					 "ASCII docs"
					 "Include files"
				)
			)
			(default #docmask)
	    )
)
(if #inst_mode
    (
     (if (bitand #docopt %11) (set #docdir (askdir (prompt "Where do you want to install the documents?")
						 (default @default-dest)
						 (help @askdir-help)
					)
			    )
     )
     (if (bitand #docopt %100) (set #incdir (askdir (prompt "Where do you want to install the includes?")
						  (default @default-dest)
						  (help @askdir-help)
					  )
			      )
     )
    )
    (
     (set #docdir (tackon @default-dest "Docs"))
     (set #incdir (tackon @default-dest "Include"))
     (if (not (exists #docdir)) (makedir #docdir (prompt (cat "Creating '" #docdir "'...")) (help @makedir-help) (infos) (confirm)))
     (if (not (exists #docdir)) (set #docdir @default-dest))	;#docdir doesn't exists if user skips the makedir above
     (if (and (bitand #docopt %100) (not (exists #incdir))) (makedir #incdir (prompt (cat "Creating '" #incdir "'...")) (help @makedir-help) (infos) (confirm)))
     (if (not (exists #incdir)) (set #incdir @default-dest))	;#incdir doesn't exists if user skips the makedir above
    )
)
(if (bitand #docopt %1)
    ((foreach "Docs" "#?.guide"
	      (copylib (prompt ("Installing %s ..." @each-name))
		       (help @copylib-help)
		       (source (tackon "Docs" @each-name)) (dest #docdir)
		       (infos) (noposition) (confirm)
	      )
     )
    )
)
(if (bitand #docopt %10)
    ((if (exists "Docs/SUPERVISOR.doc")
	(foreach "Docs" "#?.doc"
		 (copylib (prompt ("Installing %s ..." @each-name))
			  (help @copylib-help)
			  (source (tackon "Docs" @each-name)) (dest #docdir)
			  (infos) (noposition) (confirm)
		 )
	)
	((if (not (exists (cat #guide2doc)))
	    (set #guide2doc (askstring (prompt "Enter the full pathname of the doc converter command")
				      (help (cat "The documents included in this archive are in AmigaGuide format only.\n"
						 "This program is able to convert them into plain text using the Guide2Doc utility.\n"
						 "Guide2Doc is not included in this distribution nor is in your command paths.\n"
						 "If you have this tool somewhere else you should enter its full path name in order to get the desired .doc files"))
				      (default "Guide2Doc")
			   )
	    )
	 )
	 (foreach "Docs" "#?.guide"
		  (run ('"%s" "Docs/%s" "%s/%s.doc"' #guide2doc @each-name #docdir @each-name)
			   (prompt ("Installing %s.doc ..." @each-name))
			   (help "")
			   (confirm)
		  )
	 )
	)
     )
    )
)
(set #agpath ("%s\n" #docdir))
(if (bitand #docopt %100)
    ((set #incdir (tackon #incdir "supervisor"))
     (set #agpath (cat #agpath #incdir "\n"))
     (foreach "Include/supervisor" "#?"
	      (copylib (prompt ("Installing %s ..." @each-name))
		       (help @copylib-help)
		       (source (tackon "Include/supervisor" @each-name)) (dest #incdir)
		       (confirm)
	      )
     )
    )
)
(if (or (<> @default-dest #docdir) (bitand #docopt %100))
    (select #agstat
;-> #agstat = 0: missing 'ENVARC:'
	 (startup "SUPERVISOR" (prompt "Configuring your system for on-line help")
			       (help @startup-help)
			       (command "IF NOT EXISTS RAM:ENV\n"
					" MakeDir RAM:ENV\n"
					" Assign ENV: RAM:ENV\n"
					"ENDIF\n"
					"MakeDir ENV:AmigaGuide\n"
					(if (bitand #docopt %100)
					    ("Echo \"%s*n%s\" >ENV:AmigaGuide/Path\n" #docdir #incdir)
					    ("Echo \"%s\" >ENV:AmigaGuide/Path\n" #docdir)
					)
			       )
	 )
;-> #agstat = 1: missing 'ENVARC:AmigaGuide/'
	 (if (makedir "ENVARC:AmigaGuide" (prompt "Configuring your system for on-line help") (help @makedir-help) (confirm))
	     (textfile (dest "ENVARC:AmigaGuide/Path") (append #agpath))
	 )
;-> #agstat = 2: missing 'ENVARC:AmigaGuide/Path'
	 (textfile (dest "ENVARC:AmigaGuide/Path") (append #agpath) (prompt "Configuring your system for on-line help") (help "") (confirm))
;-> #agstat = 3: add path to 'ENVARC:AmigaGuide/Path' (if required)
	 (if (not (patmatch ("#?%s#?" #agpath) (getenv "AmigaGuide/Path")))
	     (textfile (dest "ENVARC:AmigaGuide/Path") (include "ENVARC:AmigaGuide/Path") (append (cat "\n" #agpath)) (prompt "Configuring your system for on-line help") (help "") (confirm))
	 )
     )
)

)				;END: P_InstallDocs()


;--- CHECK FOR INVALID CONFIGURATIONS ---
(if (< #totmem 700000)
    (abort "There is not enough memory in your system to run SUPERVISOR properly")
)

;--- STARTUP MESSAGE ---
(message "\n" (if (not (* #agflag #__debug)) "\n")
	 "-----------------------------\n"
	 "SUPERVISOR® " #svver "." #svrev " (" (if #demoflag "EVALUATION" "REGISTERED") ")\n"
	 "-----------------------------\n"
	 "\n"
	 "Designed and Coded by\n"
	 "Dan\n\n"
	 "© 1992-97 THE 1702 GROUP\n"
	 "All Rights Reserved"
(if (* #agflag #__debug)
	 (cat "\n\n\nAmigaGuide " #agver "." #agrev " detected")
)
)

(welcome)

;--- INSTALLATION MODE SELECTION ---
(complete 0)
(select
	(set #inst_mode (askchoice (prompt "Select the installation mode:")
				  (choices "Single destination"
					   "Multi destination"
				  )
				  (help (cat "Single destination - installs all files to "
					     "a drawer created in the destination path.\n"
					     "Multi destination - allows different paths for "
					     "program and document files. No drawer will "
					     "be created.\n\n"
					     @askdir-help
					)
				  )
		       )
	)


	(	;*** BEGIN: Single destination ***
(set #default-dest
     (askdir (prompt "Please select where you wish to install this program\n(A drawer called SUPERVISOR will be created)")
	     (default @default-dest)
	     (help @askdir-help)
     )
)
;#default-dest is used to avoid problems if user aborts during 'askdir'
(set @default-dest (tackon #default-dest "SUPERVISOR"))
(makedir @default-dest (prompt (cat "Creating '" @default-dest "'...")) (help @makedir-help) (infos))
	)	;*** END: Single destination ***


	(	;*** BEGIN: Multi destination ***
(if (exists "SUPERVISOR:" (noreq))
    (set @default-dest "SUPERVISOR:")
    ((if (exists (set #__tmp (tackon @default-dest "SUPERVISOR")) (noreq))
	 (set @default-dest #__tmp)
	 ((if (exists "C:" (noreq)) (set @default-dest "C:"))
	  (if (exists "SYS:Utilities" (noreq)) (set @default-dest "SYS:Utilities"))
	  (if (exists "SYS:Tools" (noreq)) (set @default-dest "SYS:Tools"))
	  (if (exists "Extras:" (noreq)) (set @default-dest "Extras:"))
	  (if (exists "Extras:Utilities" (noreq)) (set @default-dest "Extras:Utilities"))
	  (if (exists "Extras:Tools" (noreq)) (set @default-dest "Extras:Tools"))
	  (if (exists "Work:SUPERVISOR" (noreq)) (set @default-dest "Work:SUPERVISOR"))
	  (if (exists (set #__tmp (tackon @default-dest "SUPERVISOR")) (noreq)) (set @default-dest #__tmp))
	 )
     )
    )
)
(set #default-dest
     (askdir (prompt "Please select where you wish to install this program\n(No drawer will be created)")
	     (default @default-dest)
	     (help @askdir-help)
     )
)
(set @default-dest #default-dest)	;to avoid problems if user aborts during 'askdir'
	)	;*** END: Multi destination ***
)


(complete 1)
(copylib (prompt "Installing the executable...\n(The only required file. All others are optional)")
	 (help @copylib-help)
	 (source "Tools/Mon") (dest @default-dest)
	 (infos) (noposition) (confirm)
)
(complete 50)
(P_InstallDocs)
(complete 100)

(if (exists "Plug-In") (exit "NOTE: You can find some extra stuff "
			     "in the 'Plug-In' drawer.\n"
			     "You must install them manually since "
			     "at this time there is no support for them "
			     "in this procedure.\n"
			     "Sorry for the inconvenience!"
		       )
)

;(set @default-dest "")
