;
; GMPlay install script
; ---------------------
; © 1997 by Christian Buchner
;
; $VER: GMPlayInstall (28-May-97)
;

(set @app-name "GMPlay V1.3")

; Set current version and revision

(set myver 1)
(set myrev 3)

; Do not allow Novice User (when running from Shell)

(if (= @user-level 0)
    (set @user-level 1)
)


;=============================================================================
; English strings

(set default_lang 4)

(set #welcomestring "GMPlay V1.3\n--------------\n©1995-97 by Christian Buchner & Alessandro de Luca\nGiftware, any donations welcome!\nPlease support the development of a fine program!\n")
(set #whereprogram "Where shall I install GMPlay?\n A drawer called \"GMPlay\" will be created there.")
(set #installwhat "Please select what parts of the\npackage you want to install.")
(set #install0 "Install documentation (recommended)")
(set #install1 "Install XPK libraries (recommended)")
(set #install2 "Install the 14 bit sound driver (recommended)")
(set #install3 "Install the IFF sound samples (recommended)")
(set #install4 "Install the 14 bit calibration utility")
(set #install5 "Install the example MIDI files")
(set #install6 "Install GMDIR environment variable (recommended)")
(set #copyprogram "Copying GMPlay executable")
(set #wheredocs "Where shall I place the documentation guide?\nPerhaps you prefer the HELP: directory?")
(set #copydocs "Copying documentation guide.")
(set #wherelibs "Where do you want the libraries installed?\nMake sure that there is a LIBS: assign to that directory.")
(set #copyxpk "Copying xpkmaster.library.")
(set #copyshri "Copying xpk SHRI sublibrary.")
(set #copysqsh "Copying xpk SQSH sublibrary.")
(set #wheredrivers "Where do you want to install the sound drivers?")
(set #SDGMDir "In the GMPlay SoundDrivers directory")
(set #SDDevs  "In DEVS:SoundDrivers")
(set #copy14bit "Copying 14 bit sound driver.")

(set #copytones "Copying tone samples (XPK SQSH packed!).")
(set #copydrums "Copying drum samples (XPK SQSH packed!).")
(set #copymaps "Copying map files.")
(set #copycalib "Copying 14 bit calibration utility.")
(set #copymidi "Copying example MIDI files (XPK SHRI packed!).")
(set #addpathvar "Installing GMDIR environment variable.")
(set #savepathvar "Saving GMDIR environment variable in ENVARC:")
(set #gmdirhelp "The GMDIR environment variable will tell\nGMPlay where its program directory is located.\nIf you still have the GM: assign installed\nyou may remove it now. It is no longer needed.\n")
(set #envarchelp "This step will copy the GMDIR environment\nvariable into your ENVARC: directory where\nit is safely stored on hard drive\nin order to survive resets.\n")
(set #endmessage "GMPlay has been successfully installed.\nNote that GMPlay is a CLI-based program.\nPlease read the documentation.\nThen try playing the example songs.\n")

(complete 0)

; Welcome the user

(welcome #welcomestring)



; Find the location of an existing GMPlay installation

(set #GMDir (getassign "GM"))

(if (exists "ENV:GMDIR" (noreq))
	(
		(set #GMDir (getenv "GMDIR"))
	)
)


; remove any slash at the end of #GMDir (if present)

(if (strlen #GMDir)
	(
		(if (= (substr #GMDir (- (strlen #GMDir) 1) 1) '/')
			(
				(set #GMDir (substr #GMDir 0 (- (strlen #GMDir) 1) ) )
			)
		)
	)
)


; Expand the path (and check if directory exists)

(if (strlen #GMDir)
	(
		(set #GMDir (expandpath #GMDir))
	)
)


; Now check the version of the current installation

; default: Create a new installation
(set #InstallChoice 2)

(if (strlen #GMDir)
	(
		(set #GMProg (tackon #GMDir "GMPlay"))
		
		(if (exists #GMProg)
			(
				(set vernum (getversion #GMProg))
				(set ver (/ vernum 65536) )
				(set rev (- vernum (* ver 65536) ) )
		
				(message (cat "Found an existing GMPlay installation in\n\n" #GMDir "\n\nInstalled Version: " ver "." rev "\n") )
		
				(set #Choice0 "Update the old installation")
				(set #Choice1 "Reinstall (parts of) this installation")
				(set #Choice2 "Create a new installation")
		
				(set #InstallDefault 0)
				
				(if  (>= rev myrev)
					(
						(set #Choice0 "")
						(set #InstallDefault 1)
					)
					(
						(set #Choice1 "")
					)
				)
		
				(set #InstallChoice
					(askchoice
						(prompt "Please specify the type of installation")
						(help @askchoices-help)
						(choices #Choice0
							 #Choice1
							 #Choice2
						)
						(default #InstallDefault)
					)
				)
			)
		)
	)
)


; Update the old installation

(if (= #InstallChoice 0 )
	(
		(set #BackupDir  (tackon #GMDir "Backup"))
		(set #TablesDir  (tackon #GMDir "Tables"))
		(set #GuideFile  (tackon #GMDir "GMPlay.guide"))
		(set #DriversDir (tackon #GMDir "SoundDrivers"))
		(set #TonesDir   (tackon #GMDir "Tones"))
		(set #DrumsDir   (tackon #GMDir "Drums"))
		(set #ToneMap    (tackon #GMDir "ToneMap"))
		(set #DrumMap    (tackon #GMDir "DrumMap"))
		(makedir #BackupDir)
		
		(if (exists #TablesDir)
			(
				(run (cat "Delete \"" #TablesDir "\" All Quiet Force")
					(prompt "Deleting the old Tables directory")
					(help "This is necessary because the format\nof the tables has changed.\n")
					(confirm)
				)
			)
		)
		(if (exists #GuideFile)
			(
				(delete #GuideFile
					(prompt "Deleting obsolete program documentation")
					(infos)
					(confirm)
				)
			)
		)
		(if (exists #DriversDir)
			(
				(run (cat "Delete \"" #DriversDir "\" All Quiet Force")
					(prompt "Deleting the old sound drivers directory")
					(help "This is necessary because you will\nrequire a new 14 bit sound driver.\nThis will be installed in DEVS:SoundDrivers")
					(confirm)
				)
			)
		)
		(if (exists #TonesDir)
			(
				(set #Choice
					(askchoice
						(prompt "Delete or backup the old tones?")
						(help @askchoices-help)
						(choices "Delete"
							 "Backup"
						)
						(default 0)
					)
				)
				(if (= #Choice 0)
					(
						(run (cat "Delete \"" #TonesDir "\" All Quiet Force")
							(prompt "Deleting the old tones directory")
							(help "You will get new and better\ntones with the current release.\n");
							(confirm)
						)
					)
				)
				(if (= #Choice 1)
					(
						(run (cat "Rename \"" #TonesDir "\" To " #BackupDir "/")
							(prompt "Backing up the old tones directory")
							(help "You will find them in the Backup drawer.\n");
							(confirm)
						)
					)
				)
			)
		)
		(if (exists #DrumsDir)
			(
				(set #Choice
					(askchoice
						(prompt "Delete or backup the old drums?")
						(help @askchoices-help)
						(choices "Delete"
							 "Backup"
						)
						(default 0)
					)
				)
				(if (= #Choice 0)
					(
						(run (cat "Delete \"" #DrumsDir "\" All Quiet Force")
							(prompt "Deleting the old drums directory")
							(help "You will get new and better\ndrums with the current release.\n");
							(confirm)
						)
					)
				)
				(if (= #Choice 1)
					(
						(run (cat "Rename \"" #DrumsDir "\" To " #BackupDir "/")
							(prompt "Backing up the old drums directory")
							(help "You will find them in the Backup drawer.\n");
							(confirm)
						)
					)
				)
			)
		)
		(if (OR (exists #ToneMap) (exists #DrumMap) )
			(
				(set #Choice
					(askchoice
						(prompt "Delete or backup the old mapfiles?")
						(help @askchoices-help)
						(choices "Delete"
							 "Backup"
						)
						(default 0)
					)
				)
				(if (= #Choice 0)
					(
						(run (cat "Delete \"" #DrumMap "\" \"" #ToneMap "\" Quiet Force")
							(prompt "Deleting the mapfiles")
							(help "You will get new and better\nmapfiles with the current release.\n");
							(confirm)
						)
					)
				)
				(if (= #Choice 1)
					(
						(run (cat "Rename \"" #ToneMap  "\" \"" #DrumMap "\" To " #BackupDir "/")
							(prompt "Backing up the mapfiles")
							(help "You will find them in the Backup drawer.\n");
							(confirm)
						)
					)
				)
			)
		)
	)
)


; Ask for target directory (only if new installation desired)

(if (= #InstallChoice 2 )
	(
		(set #TargetDir
			(askdir
				(prompt #whereprogram)
				(help @askdir-help)
				(default
					(expandpath
						(if (exists "Music:" (noreq))
							"Music:"
						(if (exists "Sound:" (noreq))
							 "Sound:"
						(if (exists "Work:Music" (noreq))
							 "Work:Music"
						(if (exists "Work:Sound" (noreq))
							 "Work:Sound"
						(if (exists "Work:" (noreq))
							 "Work:"
						"SYS:" )))))
					)
				)
				(newpath)
			)
		)
		(set #DestDir (tackon #TargetDir "GMPlay"))
		(makedir #DestDir (infos))
	)

	; ELSE: Install into existing directory!

	(
		(set #DestDir #GMDir)
	)
)


(complete 10)

; Ask what parts of the program shall be installed

(set #InstFlags
	(askoptions
		(prompt #installwhat)
		(help @askoptions-help)
		(choices #install0
			 #install1
			 #install2
			 #install3
			 #install4
			 #install5
			 #install6
		)
	)
)

(complete 20)

; Copy GMPlay main program

(copyfiles
	(prompt #copyprogram)
	(help @copyfiles-help)
	(source "GMPlay")
	(dest #DestDir)
	(infos)
	(confirm)
)

(complete 30)

; Install GMPlay documentation

(if (BITAND #InstFlags 1)
	(
		(set #DocsDir
			(askdir
				(prompt #wheredocs)
				(help @askdir-help)
				(default #DestDir)
				(newpath)
			)
		)
		
		(makedir #DocsDir)
		
		(copyfiles
			(prompt #copydocs)
			(help @copyfiles-help)
			(source "GMPlay-Eng.guide")
			(dest #DocsDir)
			(infos)
			(confirm)
		)
	)
)

(complete 40)

; Install XPK libraries

(if (BITAND #InstFlags 2)
	(
		(set #LibsDir
			(askdir
				(prompt #wherelibs)
				(help @askdir-help)
				(default "LIBS:")
				(newpath)
			)
		)
		
		(set #CompDir (tackon #LibsDir "compressors"))
		(makedir #LibsDir)
		(makedir #CompDir)
		
		(copylib
			(prompt #copyxpk)
			(help @copylib-help)
			(source "Libs/xpkmaster.library")
			(dest #LibsDir)
			(confirm)
		)
		
		(copylib
			(prompt #copyshri)
			(help @copylib-help)
			(source "Libs/compressors/xpkSHRI.library")
			(dest #CompDir)
			(confirm)
		)
		
		(copylib
			(prompt #copysqsh)
			(help @copylib-help)
			(source "Libs/compressors/xpkSQSH.library")
			(dest #CompDir)
			(confirm)
		)
	)
)


(complete 50)

; Query for sounddriver destination directory

(if (BITAND #InstFlags 4)
	(
		(set #DriverSelect
			(askchoice
				(prompt #wheredrivers)
				(help @askchoices-help)
				(choices #SDGMDir
					 #SDDevs)
				(default 1)
			)
		)
		
		(if (= #DriverSelect 0)
			(
				(set #DriverDir (tackon #DestDir "SoundDrivers"))
				(makedir #DriverDir)
			)
		)
		
		(if (= #DriverSelect 1)
			(
				(set #DriverDir "DEVS:SoundDrivers")
				(makedir #DriverDir)
			)
		)
	)
)

; Install 14 bit sound driver

(if (BITAND #InstFlags 4)
	(
		(copylib
			(prompt #copy14bit)
			(help @copylib-help)
			(source "SoundDrivers/14bit.driver")
			(dest #DriverDir)
			(confirm)
			(infos)
		)
	)
)

(complete 60)

; Install Tones, Drums, Mapfiles

(if (BITAND #InstFlags 8)
	(
		(copyfiles
			(prompt #copytones)
			(help @copyfiles-help)
			(source "")
			(pattern "Tones")
			(dest #DestDir)
			(confirm)
		)
		
		(copyfiles
			(prompt #copydrums)
			(help @copyfiles-help)
			(source "")
			(pattern "Drums")
			(dest #DestDir)
			(confirm)
		)
		
		(copyfiles
			(prompt #copymaps)
			(help @copyfiles-help)
			(source "")
			(pattern "MapFiles")
			(dest #DestDir)
			(confirm)
		)
	)
)

(complete 70)

; Copy 14 bit calibration program

(if (BITAND #InstFlags 16)
	(
		(copyfiles
			(prompt #copycalib)
			(help @copyfiles-help)
			(source "14Bit_Calibration")
			(dest #DestDir)
			(infos)
			(confirm)
		)
	)
)

(complete 80)

; Copy example MIDI files

(if (BITAND #InstFlags 32)
	(
		(copyfiles
			(prompt #copymidi)
			(help @copyfiles-help)
			(source "")
			(pattern "MIDI-Files")
			(dest #DestDir)
			(infos)
			(confirm)
		)
	)
)

(complete 90)

; Set GMDIR environment variable

(if (BITAND #InstFlags 64)
	(
		(run (cat "SetEnv GMDIR \"" #DestDir "\"")
			(prompt #addpathvar)
			(help #gmdirhelp)
			(confirm)
		)
		(run (cat "Copy ENV:GMDIR ENVARC:")
			(prompt #savepathvar)
			(help #envarchelp)
			(confirm)
		)
	)
)


(complete 100)

; Tell the user that the assign is no longer needed.

(if (getassign "GM")
	(
		(if	(askbool
				(prompt "Remove the obsolete GM: assign\nfrom your user-startup?")
				(help "You do no longer require the GM: assign.\nIt has been replaced with the GMDIR variable.\n")
			)
			(
				(startup "GMPlay"
					(prompt "Deleting the assign from your user-startup")
					(help @startup-help)
					(command "; GMPlay's GM: assign has been removed")
				)
			)
		)
	)
)

; Say goodbye to the user

(message #endmessage)
(exit (quiet))

; End of File
