;---
; 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:")
			)
		)
	)
)

(if (exists "Readme")
    (if	(= 0 (run "SYS:Utilities/Multiview Readme"))
	("")
	(run "SYS:Utilities/More Readme")
    )
)
(set #install-games
 (askoptions
	(prompt	("\nWhich version(s) of \"%s\"\nwould you like to install ?\n" @app-name))
	(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 #gamedir "KidChaos")

(set #dest
 (tackon
  (askdir
	(prompt ("Where would you like \"%s\" installed ?\nA drawer called \"%s\" will be created." @app-name #gamedir))
	(help @askdir-help)
	(default @default-dest)
	(newpath)
  )
  #gamedir
))

(set @default-dest #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\nPlease insert %s CD-ROM in any drive" #gamedir))
	(help @askdisk-help)
	(dest @app-name)
	(assigns)
)
(makedir #dest (infos))

(copyfiles
	(source "Readme")
	(dest #dest)
	(infos)
)
(set #dest (tackon @default-dest "kcdat"))

(makedir #dest (infos))

(if	(BITAND #install-games 4)
	((copyfiles
		(source "WHD")
		(dest #dest)
		(all)
	 )
	 (copyfiles
		(source "JST/Patch")
		(dest #dest)
	 )
	 (copyfiles
		(source ("%s.slave" @app-name))
		(dest @default-dest)
	 )
	 (copyfiles
		(source ("%s.inf" @app-name))
		(dest @default-dest)
		(newname ("%s.info" @app-name))
	))
)
(if	(BITAND #install-games 2)
	(copyfiles
		(source "JST")
		(dest #dest)
		(all)
	)
)
(if	(BITAND #install-games 1)
	(copyfiles
		(source "DOS")
		(dest #dest)
		(all)
	)
)
(copyfiles
	(source ("%s:" @app-name))
	(dest #dest)
	(pattern "#?")
	(files)
)
(exit)
