;********************************************************************
; $VER: Install V3.00 Monday 07-Nov-94 12:53:42
;       Inovatronics, Inc.
;       Suite 209b
;       8499 Greenville Ave
;       Dallas, Texas 75231
;       USA
;********************************************************************
; History
; v3.00 based on Install-CanDo and InstallCanDo
; v1.xx & 2.xx old versions of the install script
;********************************************************************
; 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"))
(set aga (= (database "CHIPREV") "AA"))

;********************************************************************
; The common/general/english strings
;********************************************************************
(set Intro_text
	(cat
		"Welcome to the CanDo 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 CanDo's directory?  "
		"You should select the directory that you want to contain the CanDo directory."
	)
)

(set InsertMaster_text
	(cat
		"Please insert CanDo's \"Installation Disk\" diskette."
	)
)

(set InsertDisk2_text
	(cat
		"Please insert CanDo's \"Disk 2\" diskette."
	)
)

(set InsertDisk3_text
	(cat
		"Please insert CanDo's \"Disk 3\" diskette."
	)
)

(set InsertDisk4_text
	(cat
		"Please insert CanDo's \"Disk 4\" diskette."
	)
)

(set ReInstall_text
	(cat
		"CanDo seems to already be installed.  Do you want to delete "
		"the old installation and reinstall CanDo?"
		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 DeleteDir_text
	(cat
		"One moment please, deleting directory"
		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 MakeBackup_text
	(cat
		"If we detect old configuration files do you want us to back them up?  "
		newline
		newline
	)
)

(set BackupResources_text
	(cat
		"If we find old directories of resources (ie. Decks, Images, "
		"Sounds, etc) do you want us to back them up?  If not we will "
		"DELETE them."
		newline
		newline
	)
)

(set InstallAGA_text
	(cat
		"Do you want us to install AGA resources?"
		newline
		newline
	)
)

(set UpdateS_text
	(cat
		"One moment please, updating S:User-Startup"
		newline
		newline
	)
)

(set UpdateSPrompt_text
	(cat
		"An assignment needs to be added to the \"S:User-Startup\" "
		"so that your system will be properly configured to use CanDo."
	)
)

(set InstallEverything_text
	(cat
		"Do you want to install the tutorial and their resources?"
		newline
		newline
	)
)

(set Final_text
	(cat
		"CanDo v3.0 has now been successfully installed on your "
		"machine.  REBOOT your machine after all the drive lights "
		"have gone out."
		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 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")
	(
		; german strings go here

		(set Intro_text
			(cat "Willkommen zum Installationsprogramm von " progname ". "
		     "Um die Installtion für Sie so einfach wie möglich zu gestalten, "
		     "benutzen wir den Commodore-Installer."
		     newline newline
			)
		)
	)
)

;********************************************************************
; Some common procedures
;********************************************************************
; where do we install the CanDo directory
(procedure find_home_dir
	(
		; if we were installed, where are we?
		(set cando_dir (getassign "CanDo"))
		
		; is cando assign made?
		(if (= cando_dir nothing)
			(set cando_dir @default-dest)
			(set cando_dir (pathonly cando_dir))
		)

		; where are we to install?
		(set home_dir
			(askdir
				(prompt InstallTo_text)
				(help @askdir-help)
				(default cando_dir)
				(newpath)
			)
		)
		
		; see if there is a CanDo file already here
		(set cando_dir (tackon home_dir "CanDo"))
		(if (= (exists cando_dir) is_a_file)
			(
				; if we are actually pointing inside CanDo then fix the paths
				(set cando_dir home_dir)
				(set home_dir (pathonly home_dir))
			)
		)
		(if (= (exists cando_dir) not_here)
			(makedir cando_dir
				(infos)
			)
		)
		
		; is CanDo already installed here?
		(if (= (exists (tackon cando_dir "CanDo")) 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
				)
			)
		)
		
		;(if (= testing true)
		;	(askbool
		;		(prompt (cat "About to do" newline Unarchiving_Text FilePrefix quote archive quote newline PathPrefix quote thepath quote))
		;		(choices "Proceed" "Proceed")
		;		(help @askbool-help)
		;	)
		;)
		
		; 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)
					(
						; if an old backup exists then delete it
						(set temp2 (cat temp ".bak"))
						(if (= (exists temp2) is_a_file)
							(delete temp2)
						)
								
						; do we check for backups?
						(if (and (= checkbackup true) (= makebackups true))
							(
								; just rename it then
								(rename temp temp2)
							)
							(
								; well just delete it then
								(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, if so backup, and clear it of all files, destination=directory
(procedure resource_dir
	(
		; is it here
		(if (= (exists destination) is_a_dir)
			(
				; do we check for backups?
				(if (= backupresources true)
					(
						; just rename it then
						(rename destination (tackon (pathonly destination) (cat (fileonly destination) ".bak")))
						
						; now insure it exists
						(insure_dir)
					)
					(
						; well just delete it then
						(clear_dir)
					)
				)
						
			)
			(insure_dir)
		)
	)
)

; insure a directory exists and clear it of all files, destination=directory
(procedure clear_dir
	(
		; is it here?
		(insure_dir)
		
		(if (= (exists destination) is_a_dir)
			(
				(working ClearDir_Text destination)
				(run (cat "Delete " quote (tackon destination "#?") quote " All Quiet"))
			)
		)
	)
)

; remove the directory and any of its files
(procedure remove_dir
	(
		(if (= (exists destination) is_a_dir)
			(
				(working DeleteDir_Text destination)
				(run (cat "Delete " quote destination quote " All Quiet"))
			)
		)
	)
)

; remove the a specific file
(procedure remove_file
	(
		(if (= (exists destination) is_a_file)
			(
				(working DeleteFiles_Text destination)
				(run (cat "Delete " quote destination quote " All Quiet"))
			)
		)
	)
)

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

; *******************************************************************
; on with the show
; *******************************************************************
(welcome intro_text)

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

; force everybody to expert mode
(user 2)

; how far have we come?
(complete 0)


; *******************************************************************
; where do we install to?
; *******************************************************************
(find_home_dir)


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


; *******************************************************************
; if we are not doing a complete reinstall, do we backup the old files?
; *******************************************************************
(if (= reinstall false)
	(set makebackups
		(askbool
			(prompt MakeBackup_text)
			(help @askbool-help)
		)
	)
	(set makebackups false)
)

; *******************************************************************
; do we install everything?
; *******************************************************************
(set installeverything
	(askbool
		(prompt InstallEverything_text)
		(help @askbool-help)
	)
)

; *******************************************************************
; if we are backing up, do we backup the resource?
; *******************************************************************
(if (= installeverything true)
	(
		(if (= makebackups true)
			(set backupresources
				(askbool
					(prompt BackupResources_text)
					(help @askbool-help)
				)
			)
			(set backupresources false)
		)
		
; *******************************************************************
; install AGA resources?
; *******************************************************************
		(if (= aga true)
			(set installAGA
				(askbool
					(prompt InstallAGA_text)
					(help @askbool-help)
				)
			)
			(set installAGA false)
		)
	)
	(
		(set backupresources false)
		(set installAGA false)
	)
)
		
; back to novice mode
(user 0)


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

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


; *******************************************************************
; delete CanDo and install new one
; *******************************************************************
(set destination (tackon cando_dir "CanDo"))
(set archive "CanDo_Master:CanDo_Program.lha")
(unarchive)

; how far have we come?
(complete 5)


; *******************************************************************
; keep the defaults or install them?
; *******************************************************************
(if (= reinstall true)
	(
		; make backups of these files
		(set checkbackup true)

		; replace the icon?
		(if (<> (exists (tackon cando_dir "CanDo.info")) is_a_file)
			(
				; replace the cando icon
				(if (= testing true)
					(set destination "Ram:CanDo/CanDo.info")
					(set destination (tackon cando_dir "CanDo.info"))
				)
				(set archive "CanDo_Master:CanDo_DefIcon.lha")
				(unarchive)
			)
		)
		
		; replace the s: defaults file?
		(if (<> (exists "S:CanDo.defaults") is_a_file)
			(
				; replace the cando defaults file
				(if (= testing true)
					(set destination "Ram:S/CanDo.defaults")
					(set destination "S:CanDo.defaults")
				)
				(set archive "CanDo_Master:CanDo_Def.lha")
				(unarchive)
			)
		)
		
		; make backups of these files
		(set checkbackup false)

	)
	(
		; replace the cando icon
		(if (= testing true)
			(set destination "Ram:CanDo/CanDo.info")
			(set destination (tackon cando_dir "CanDo.info"))
		)
		(set archive "CanDo_Master:CanDo_DefIcon.lha")
		(unarchive)
	)
)

; how far have we come?
(complete 10)


; *******************************************************************
; 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
		(if (= testing true)
			(set destination "Ram:Libs/AmigaGuide.library")
			(set destination "Libs:AmigaGuide.library")
		)
		(set archive "CanDo_Master:AGuide_Lib.lha")
		(unarchive)
		
		; second, the guide tool
		(if (= testing true)
			(set destination "Ram:Utilities/AmigaGuide")
			(set destination "Sys:Utilities/AmigaGuide")
		)
		(if (<> (exists destination) is_a_file)
			(
				(set archive "CanDo_Master:AGuide_App.lha")
				(unarchive)
			)
		)
		
		; lastly the amigaguide helpfile
		(if (= testing true)
			(set destination "Ram:S/Help.guide")
			(set destination "S:Help.guide")
		)
		(if (<> (exists destination) is_a_file)
			(
				(set archive "CanDo_Master:AGuide_Hlp.lha")
				(unarchive)
			)
		)
	)
)

; how far have we come?
(complete 15)


; *******************************************************************
; now how about the ASL stuff
; *******************************************************************
(if (= (exists "Libs:ASL.library") is_a_file)
	(if (> 2490404 (getversion "Libs:ASL.library"))
		(set do_asl true)
	)
	(set do_asl true)
)

; ok do we do guide stuff?
(if (= do_asl true)
	(
		; first do the library
		(if (= testing true)
			(set destination "Ram:Libs/ASL.library")
			(set destination "Libs:ASL.library")
		)
		(set archive "CanDo_Master:ASL_Lib.lha")
		(unarchive)
	)
)

; how far have we come?
(complete 20)


; *******************************************************************
; copy readme to the destination
; *******************************************************************
(copyfiles
	(source "CanDo_Master:ReadMe")
	(dest cando_dir)
	(files)
	(infos)
)

; how far have we come?
(complete 25)


; *******************************************************************
; C: DeckBinder & DeckRunner
; *******************************************************************
(if (= testing true)
	(set destination "Ram:C/(DeckRunner|DeckBinder)")
	(set destination "C:(DeckRunner|DeckBinder)")
)
(set archive "CanDo_Master:CanDo_C.lha")
(unarchive)

; how far have we come?
(complete 30)


; *******************************************************************
; Libs: CanDo.library, CanDoGFX.library, InovaMusic.library
; *******************************************************************
(if (= testing true)
	(set destination "Ram:Libs/(CanDo|CanDoGFX|InovaMusic).library")
	(set destination "Libs:(CanDo|CanDoGFX|InovaMusic).library")
)
(set archive "CanDo_Master:CanDo_Libs.lha")
(unarchive)

; how far have we come?
(complete 35)


; *******************************************************************
; clear out the candofiles directory
; *******************************************************************
; delete these old directories, if they exist
(set destination (tackon cando_dir "CanDoFiles"))
(clear_dir)

; how far have we come?
(complete 40)


; *******************************************************************
; delete these old directories, if they exist
; *******************************************************************
(set destination (tackon cando_dir "ObjectTools"))
(remove_dir)
(set destination (tackon cando_dir "EditorTools"))
(remove_dir)
(set destination (tackon cando_dir "HelpFiles"))
(remove_dir)
(set destination (tackon cando_dir "Utilities"))
(remove_dir)
(set destination (tackon cando_dir "XtraTools"))
(remove_dir)

; how far have we come?
(complete 45)


; *******************************************************************
; delete these old files, if they exists
; *******************************************************************
(set destination (tackon cando_dir "CanDoReadMe"))
(remove_file)

; how far have we come?
(complete 50)


; *******************************************************************
; if backup, rename, else clear/makedir
; *******************************************************************
(if (= installeverything true)
	(
		(set destination (tackon cando_dir "Anims"))
		(resource_dir)
		(set destination (tackon cando_dir "BrushAnims"))
		(resource_dir)
		(set destination (tackon cando_dir "Brushes"))
		(resource_dir)
		(set destination (tackon cando_dir "Decks"))
		(resource_dir)
		(set destination (tackon cando_dir "Images"))
		(resource_dir)
		(set destination (tackon cando_dir "Palettes"))
		(resource_dir)
		(set destination (tackon cando_dir "Pointers"))
		(resource_dir)
		(set destination (tackon cando_dir "Sounds"))
		(resource_dir)
		(set destination (tackon cando_dir "Text"))
		(resource_dir)
	)
)

(set destination (tackon cando_dir "CLIUtilities"))
(resource_dir)
(set destination (tackon cando_dir "WBUtilities"))
(resource_dir)

; how far have we come?
(complete 55)


; *******************************************************************
; unarchive misc stuff from Installation Disk
; *******************************************************************
(set destination (tackon cando_dir "CanDoFiles"))
(set archive "CanDo_Master:CanDo_Stuff1.lha")
(unarchive)

; how far have we come?
(complete 60)


; *******************************************************************
; DISK 2 is nothing but resources
; *******************************************************************
(if (= installeverything true)
	(
		(askdisk
			(prompt	InsertDisk2_text)
			(help @askdisk-help)
			(dest "Disk_2")
			(newname "CanDo_Disk2")
		)
		
		; unarchive from it
		(set destination (tackon cando_dir "CanDoFiles"))
		(set archive "CanDo_Disk2:CanDo_Stuff2.lha")
		(unarchive)
		
		; how far have we come?
		(complete 65)


		; *******************************************************************
		; DISK 3
		; *******************************************************************
		(askdisk
			(prompt	InsertDisk3_text)
			(help @askdisk-help)
			(dest "Disk_3")
			(newname "CanDo_Disk3")
		)
		
		; unarchive from it
		(set destination (tackon cando_dir "CanDoFiles"))
		(set archive "CanDo_Disk3:CanDo_Stuff3.lha")
		(unarchive)
		
		; how far have we come?
		(complete 75)
	)
)
		
; *******************************************************************
; DISK 4
; *******************************************************************
(askdisk
	(prompt	InsertDisk4_text)
	(help @askdisk-help)
	(dest "Disk_4")
	(newname "CanDo_Disk4")
)

; unarchive from it
(set destination (tackon cando_dir "CanDoFiles"))
(set archive "CanDo_Disk4:CanDo_Stuff4.lha")
(unarchive)

; how far have we come?
(complete 85)

; install AGA resources?
(if (= installeverything true)
	(
		(if (= installAGA true)
			(
				(set destination (tackon cando_dir "Images"))
				(set archive "CanDo_Disk4:AGA_Stuff.lha")
				(unarchive)
			)
		)
	)
)

; how far have we come?
(complete 90)


; *******************************************************************
; almost done, modify S:User-Startup
; *******************************************************************
(working UpdateS_text)

(startup "CanDo"
	(prompt UpdateSPrompt_text)
	(help @startup-help)
	(command "ASSIGN CanDo: " quote cando_dir quote newline)
)


; *******************************************************************
; is the preference saved
; *******************************************************************
(if (<> (exists "Env:Sys/ScreenMode.Prefs") is_a_file)
	(
		(run "Sys:Prefs/ScreenMode Save")
	)
)
(if (<> (exists "Env:Sys/Overscan.Prefs") is_a_file)
	(
		(run "Sys:Prefs/Overscan Save")
	)
)

; how far have we come?
(complete 100)

; *******************************************************************
; ok we are done then
; *******************************************************************
(makeassign "CanDo_Master" (safe))
(makeassign "CanDo_Disk2" (safe))
(makeassign "CanDo_Disk3" (safe))
(makeassign "CanDo_Disk4" (safe))
(delete "Ram:LHEX" (safe))
(delete "Ram:LHEX.script" (safe))
(set @default-dest cando_dir)
(exit Final_text)
