;********************************************************************
; $VER: Install-Edge V1.0 (Monday 25-Oct-93 11:41:35)
;       Inovatronics, Inc.
;       Suite 209b
;       8499 Greenville Ave
;       Dallas, Texas 75231
;       USA
;********************************************************************

;********************************************************************
; The required variables
;********************************************************************
(set true 1)
(set false 0)
(set not_here 0)
(set is_a_file 1)
(set is_a_dir 2)
(set newline "\n")
(set quote "\"")
(set nothing "")
(set testing true)
(set testing false)
(delopts "AskUser" "OkNoDelete" "Force")
(set cpu (database "CPU"))

;********************************************************************
; The common/general/english strings
;********************************************************************
(set Intro_text
	(cat
		"Welcome to the Edge installer.  This installer uses the "
		"Commodore Amiga Installer.  All of our products use this "
		"installer and we would love to get any feedback that might "
		"help improve the installation procedure." newline
	)
)

(set InstallTo_text
	(cat
		"Where would you like to install Edge's directory?  "
		"You should select the directory that you want to contain the Edge directory."
	)
)

(set InsertMaster_text
	(cat
		"Please insert the \"Edge Program\" diskette."
	)
)

(set ReInstall_text
	(cat
		"Edge seems to already be installed.  Do you want to delete "
		"the old installation and reinstall Edge?"
		newline newline
		"If you say NO we will try to keep your old configuration files."
	)
)

(set DeleteFiles_text
	(cat
		"One moment please, deleting file(s)"
		newline
		newline
	)
)

(set CreateDir_text
	(cat
		"One moment please, creating directory"
		newline
		newline
	)
)

(set Unarchiving_text
	(cat
		"One moment please, unarchiving"
		newline
		newline
	)
)

(set Unsnapshot_text
	(cat
		"One moment please, unsnapshotting"
		newline
		newline
	)
)

(set PathPrefix "Path=")

(set FilePrefix "File(s)=")

;********************************************************************
; The error strings
;********************************************************************
(set NoSupport_error
	(cat
		"We could not find these support files in Ram:.  "
		"Please reboot and try to install again."
		newline
	)
)

(set NoRexxMast_error
	(cat
		"We could not find the program RexxMast.  Because of this, we cannot "
		"get your system to startup ARexx.  Please reconfigure your system "
		"so that ARexx starts up when your machine does."
		newline
		newline
		"EDGE cannot run without ARexx installed and running."
		newline
		newline
	)
)

(set NoWaitForPort_error
	(cat
		"We could not find the program WaitForPort.  This program is normally "
		"in the Sys:RexxC directory.  Its absence may indicate that ARexx is "
		"not installed on your machine.  Please contact your dealer about "
		"installing ARexx."
		newline
		newline
		"EDGE cannot run without ARexx installed and running."
		newline
		newline
	)
)

(set DirIsAFile_error
	(cat
		"Should be a directory but it is actually a file..."
		newline
	)
)

(set DirNotMade_error
	(cat
		"We could not create the directory..."
		newline
	)
)

;********************************************************************
; The language specific strings, overlay the pre-defined strings
;********************************************************************
(if (= @language "deutsch")
	(
		(set eddie "eddie")
	)
)

;********************************************************************
; Some common procedures
;********************************************************************
; where do we install the edge directory
(procedure find_home_dir
	(
		; where are we to install?
		(set home_dir
			(askdir
				(prompt InstallTo_text)
				(help @askdir-help)
				(default @default-dest)
				(newpath)
			)
		)
		
		; see if there is a edge file already here
		(set edge_dir (tackon home_dir "Edge"))
		(if (= (exists edge_dir) is_a_file)
			(
				; if we are actually pointing inside edge then fix the paths
				(set edge_dir home_dir)
				(set home_dir (pathonly home_dir))
			)
		)
		(if (= (exists edge_dir) not_here)
			(makedir edge_dir
				(infos)
			)
		)
		
		; is edge already installed here?
		(if (= (exists (tackon edge_dir "Edge")) is_a_file)
			(set preinstalled true)
			(set preinstalled false)
		)
	)
)

; unarchive an archive, destination=path/file, archive=path
(procedure unarchive
	(
		; get rid of the file(s)
		(delete_pattern)
		
		; create a batch file
		(textfile
			(dest "Ram:LHEX.script")
			(append
				(cat
					"Cd " quote thepath quote newline
					"Stack 20480" newline
					"Ram:LHEX -qf x " quote archive quote newline
				)
			)
		)
		
		; unarchive then
		(working Unarchiving_Text FilePrefix quote archive quote newline PathPrefix quote thepath quote)
		(execute "Ram:LHEX.script")
	)
)

; unsnapshot a file, thepath/thefile
(procedure unsnapshot
	(
		; unsnapshot the stuff in the directory using the pattern
		(working Unsnapshot_text PathPrefix quote thepath quote newline FilePrefix quote thefile quote)
		(foreach thepath thefile
			(
				; delete the file then
				(set temp (tackon thepath @each-name))
				(if (= (exists temp) is_a_file)
					; ok first things first
					(tooltype
						(dest (tackon thepath thefile))
						(noposition)
					)
				)
			)
		)
	)
)

; insure a directory exists, destination=path
(procedure insure_dir
	(
		(set thepath destination)
		
		; if it is a file then abort
		(if (= (exists thepath) is_a_file)
			(abort DirIsAFile_error PathPrefix quote thedir quote)
		)
		
		; if it doesn't exist make it
		(if (= (exists thepath) not_here)
			(
				(working CreateDir_text PathPrefix quote thepath quote)
				(makedir thepath
					(infos)
				)
			)
		)
		
		; verify it actually is a directory
		(if (<> (exists thepath) is_a_dir)
			(abort DirNotMade_error quote thedir quote)
		)
	)
)

; insure a directory exists and clear it of a pattern of files, destination=path/pattern
(procedure delete_pattern
	(
		(set thefile (fileonly destination))
		(set destination (pathonly destination))
		(insure_dir)
		
		; delete the stuff in the directory using the pattern
		(working DeleteFiles_text PathPrefix quote thepath quote newline FilePrefix quote thefile quote)
		(foreach thepath thefile
			(
				; delete the file then
				(set temp (tackon thepath @each-name))
				(if (= (exists temp) is_a_file)
					(
						; ok first things first
						(delete temp)
						
						; is there an icon for the file?
						(set temp (cat temp ".info"))
						(if (= (exists temp) is_a_file)
							(delete temp)
						)
					)
				)
			)
		)
	)
)

; insure a directory exists and clear it of all files, destination=directory
(procedure clear_dir
	(
		(set destination (tackon destination "#?"))
		(delete_pattern)
	)
)

; insure ARexx is installed and is running
(procedure insure_arexx
	(
		; is rexx running?
		(set dosresult (run "WaitForPort -immediate REXX"))
		
		; error 20 means that WaitForPort was not found
		(if (= dosresult 20)
			(abort NoWaitForPort_error)
		)
		
		; error 5 means that RexxMast is not running
		(if (= dosresult 5)
			(
				; rexx host is not running, try to install it then
				(if (= (exists "Sys:System/RexxMast") is_a_file)
					(
						; move it from system into wbstartup
						(rename "Sys:System/RexxMast" "Sys:WBStartup/RexxMast")
						(rename "Sys:System/RexxMast.info" "Sys:WBStartup/RexxMast.info")
						
						; make sure its icon if fixed
						(tooltype
							(dest "Sys:WBStartup/RexxMast")
							(settooltype "DONOTWAIT" 1)
							(noposition)
						)
					)
					; well its not where it supposed to be
					(abort NoRexxMast_error)
				)
			)
		)
		
		; run the rexxmast program then
		(run "run >Nil: <Nil: Sys:WBStartup/RexxMast")
	)
)

;********************************************************************
; The error handler
;********************************************************************
(onerror
	(makeassign "Edge_Master" (safe))
	(delete "Ram:LHEX" (safe))
	(delete "Ram:LHEX.script" (safe))
)

;********************************************************************
; on with the show
;********************************************************************

; introduce ourself
(welcome intro_text)

; force everybody to expert mode
(user 2)

; how far have we come?
(complete 0)

; make sure ARexx is installed and running
(insure_arexx)

; where do we install
(find_home_dir)

; do we do a complete reinstall?
(if (= preinstalled true)
	(set reinstall
		(askbool
			(prompt ReInstall_text)
			(help @askbool-help)
		)
	)
	(set reinstall true)
)

; back to novice mode
(user 0)

; make sure our installation disk is here
(askdisk
	(prompt InsertMaster_Text)
	(help @askdisk-help)
	(dest "EdgeInstall")
	(newname "Edge_Master")
)

; copy LHEX to ram:
(copyfiles
	(source "Edge_Master:LHEX")
	(dest "Ram:")
	(files)
)

; verify we got LHEX into ram:
(if (<> (exists "Ram:LHEX") is_a_file)
	(abort NoSupport_error quote "LHEX" quote)
)

; what to do, that is the question
(if (= reinstall true)
	(
		; replace the old config stuff
		(set destination (tackon edge_dir "Edge.#?"))
		(set archive "Edge_Master:Edge_CFig.lha")
		(unarchive)
		
		; how far have we come?
		(complete 5)

		; replace the old dictionary
		(set destination (tackon edge_dir "Dictionaries/C_dictionary"))
		(set archive "Edge_Master:Edge_Dict.lha")
		(unarchive)
		
		; how far have we come?
		(complete 10)

		; replace the old templates
		(set destination (tackon edge_dir "Templates/C_templates"))
		(set archive "Edge_Master:Edge_Temp.lha")
		(unarchive)
	)
	(
		; insure these directories exists
		(set destination (tackon edge_dir "Dictionaries"))
		(insure_dir)
		
		; how far have we come?
		(complete 8)
		
		(set destination (tackon edge_dir "Templates"))
		(insure_dir)
	)
)

; how far have we come?
(complete 15)

; delete edge and install new one
(set destination (tackon edge_dir "Edge"))
(if (>= cpu 68020)
	(set archive "Edge_Master:Edge_C020.lha")
	(set archive "Edge_Master:Edge_C000.lha")
)
(unarchive)
(unsnapshot)

; how far have we come?
(complete 40)

; copy over our new text table
(set destination (tackon edge_dir "Edge.texts"))
(set archive "Edge_Master:Edge_Text.lha")
(unarchive)

; how far have we come?
(complete 45)

; delete any existing macros
(set destination (tackon edge_dir "Rexx/Menu_#?.edge"))
(set archive "Edge_Master:Edge_Rexx.lha")
(unarchive)

; how far have we come?
(complete 60)

; delete the old help file
(set destination (tackon edge_dir "Help/Edge.guide"))
(set archive "Edge_Master:Edge_Help.lha")
(unarchive)

; how far have we come?
(complete 85)

; now how about the amigaguide stuff
(if (= (exists "Libs:AmigaGuide.library") is_a_file)
	(if (> 2228235 (getversion "Libs:AmigaGuide.library"))
		(set do_guide true)
	)
	(set do_guide true)
)

; ok do we do guide stuff?
(if (= do_guide true)
	(
		; first do the library
		(set destination "Libs:AmigaGuide.library")
		(set archive "Edge_Master:AGuide_Lib.lha")
		(unarchive)
		
		; how far have we come?
		(complete 90)

		; second the guide tool
		(set destination "Sys:Utilities/AmigaGuide")
		(set archive "Edge_Master:AGuide_App.lha")
		(unarchive)
		
		; how far have we come?
		(complete 95)

		; lastly the amigaguide helpfile
		(set destination "S:Help.guide")
		(set archive "Edge_Master:AGuide_Hlp.lha")
		(unarchive)
	)
)

; how far have we come?
(complete 100)

; ok we are done then
(makeassign "Edge_Master" (safe))
(delete "Ram:LHEX" (safe))
(delete "Ram:LHEX.script" (safe))
(set @default-dest edge_dir)
(exit Final_text)
