;---
; Checks if given program is reachable via the path
; if not abort install
; IN:  #program - to check
; OUT: -

(procedure P_chkrun
 (if (= 0 (run ("cd SYS:\nWhich %s" #program)))
     ("")
     (abort ("You must install \"%s\" first !\nIt must be accessible via the path.\nYou can find it in the whdload package." #program))
))

(if	(exists "Games:" (noreq))
	(set @default-dest "Games:")
	(if	(exists "SYS:Games" (noreq))
		(set @default-dest "SYS:Games")
		(if	(exists "Work:Games" (noreq))
			(set @default-dest "Work:Games")
			(if	(exists "JEUX:" (noreq))
				(set @default-dest "JEUX:")
				(set @default-dest "SYS:")
			)
		)
	)
)
(set #Readme ("%s.Readme" @app-name))

(if (exists #Readme)
    (if	(= 0 (run ("SYS:Utilities/Multiview %s" #Readme)))
	("")
	(run ("SYS:Utilities/More %s" #Readme))
    )
)
(set #game_ver
 (askchoice
	(prompt ("\nWhich version of \"%s\"\nwould you like to install ?\n" @app-name))
	(help @askoptions-help)
	(choices "ECS" "AGA")
))

(set #game_ver (+ #game_ver 1))

(if	(= #game_ver 1)
	(set #CHIP "ECS")
	(set #CHIP "AGA")
)

(set #install-games
 (askoptions
	(prompt	("\nWhich version(s) of \"%s %s\"\nwould you like to install ?\n" @app-name #CHIP))
	(help @askoptions-help)
	(choices "Normal DOS version" "JST version" "WHDLoad version")
))
(if	(BITAND #install-games 4)
	((set #program "WHDLoad") (P_chkrun))
)
(if	(BITAND #install-games 2)
	((set #program "JST")	  (P_chkrun))
)

(set #dest
 (askdir
	(prompt ("Where should \"%s %s\" installed ?\nA drawer \"%s%s\" will automatically created." @app-name #CHIP @app-name #CHIP))
	(help @askdir-help)
	(default @default-dest)
	(disk)
 )
)
(set @default-dest (tackon #dest ("%s%s" @app-name #CHIP)))

(set #dest @default-dest)
(if (exists #dest)
  ( (set #choice
     (askbool
	(prompt ("\nDirectory \"%s\" already exists.\n Should it be deleted ?" #dest))
	(default 1)
	(choices "Delete" "Skip")
	(help @askbool-help)
    ))
    (if	(= #choice 1)
	(run ("Delete \"%s\" \"%s.info\" All" #dest #dest))
    )
  )
)

(askdisk
	(prompt ("\n\n\nInsert %s %s disk %ld into any drive" @app-name #CHIP #game_ver))
	(dest ("Brian the Lion %ld" #game_ver))
	(help ("Insert disk \"Brian the Lion %ld\" in any floppy drive or you'll wait for a while!"  #game_ver))
	(assigns)
)
(makedir #dest (infos))

(if (exists ("Brian the Lion %ld:Readme.txt" #game_ver))
    (copyfiles
	(source ("Brian the Lion %ld:Readme.txt" #game_ver))
	(dest #dest)
    )
)
(working "\n\n\nI'm busy...")

(run ("cd %s\n\"Brian the Lion %ld:Install.BTL\"" #dest #game_ver))

(run ("cd %s\nDelete \"Brian the Lion\"\nRename \"Brian the Lion.info\" as \"BrianTheLion.info\"" #dest))

(if	(BITAND #install-games 4)
	((copyfiles
		(source "OSEmu.4OO")
		(dest #dest)
	 )
	 (copyfiles
		(source ("%s.slave" @app-name))
		(dest #dest)
	 )
	 (copyfiles
		(source ("%sWHD.inf" @app-name))
		(dest #dest)
		(newname ("%sWHD.info" @app-name))
	))
)

(if	(BITAND #install-games 2)
	((copyfiles
		(source ("%s.jst" @app-name))
		(dest #dest)
	 )
	 (copyfiles
		(source ("%sJST.inf" @app-name))
		(dest #dest)
		(newname ("%sJST.info" @app-name))
	))
)

(if	(BITAND #install-games 1)
	(copyfiles
		(source ("%s.script" @app-name))
		(dest #dest)
		(newname @app-name)
	)
)

(copyfiles
	(source #Readme)
	(dest #dest)
	(infos)
)
(if (exists ("%s/Readme.txt" #dest))
    (if	(= 0 (run ("SYS:Utilities/Multiview %s/Readme.txt" #dest)))
	("")
	(run ("SYS:Utilities/More %s/Readme.txt" #dest))
    )
)
(exit)
