;Patcher-install-script for Gore-Design-Installs, modified by Dr.Zarkov
;---

(set #readme-file (cat @app-name ".Txt"))	; name of readme file
(set #cleanup "")				; files to delete after install
(set #last-file "Disk.2")			; last file imager should create

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

;---

(set #program "WHDLoad")
(P_chkrun)

(set #program "Patcher")
(P_chkrun)

(if (exists #readme-file)
    (if	(= 0 (run ("SYS:Utilities/Multiview %s" #readme-file)))
	("")
	(run ("SYS:Utilities/More %s" #readme-file))
    )
)

;---
(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 #game_ver
 (askchoice
	(prompt ("\nWhich version of %s do you wish to install ?\n" @app-name))
	(help @askoptions-help)
	(choices "ECS floppy version" "AGA floppy version" "CD32 version")
 )
)
(if (= #game_ver 2)
    (set #DF "CD") ;CD32
    (set #DF "DF") ;ECS/AGA
)
(if (= @user-level 2)
    ((set #CI_unit
      (askchoice
	(prompt "\nSelect source drive to install from\n")
	(default 0)
	(choices ("%s0:" #DF) ("%s1:" #DF) ("%s2:" #DF) ("%s3:" #DF))
	(help @askchoice-help)
     ))
     (set #CI_drive ("%s%ld:" #DF #CI_unit))
    )
    (set #CI_drive ("%s0:" #DF))
)

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

(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))
    ))
)
(makedir #dest
	(help @makedir-help)
)
(copyfiles
	(help @copyfiles-help)
	(source ("/%s.info" @app-name))
	(dest @default-dest)
)

;---

(copyfiles
	(help @copyfiles-help)
	(source ("%s.info" @app-name))
	(dest #dest)
)
;(copyfiles
;	(help @copyfiles-help)
;	(source ("%sInstall" @app-name))
;	(dest #dest)
;	(infos)
;)

(if (exists #readme-file)
    (copyfiles
	(help @copyfiles-help)
	(source #readme-file)
	(dest #dest)
	(infos)
    )
)
(if (= #game_ver 2)
    ( ;CD32
	(copyfiles
		(help @copyfiles-help)
		(source ("%sCD.Slave" @app-name))
		(dest #dest)
		(newname ("%s.Slave" @app-name))
	)
	(copyfiles
		(help @copyfiles-help)
		(source "OSEmu.400")
		(dest #dest)
	)
	(message ("\n\n\nPlease insert %s CD-ROM into drive %s !!!" @app-name #CI_drive))
	(copyfiles
		(help @copyfiles-help)
		(source #CI_drive)
		(dest #dest)
		(pattern "(TrollsCD|data)")
	)
	(if	(< (getsize (tackon #dest "TrollsCD")) 267584)
		(if	(= 0 (run ("xfddecrunch >NIL: %s" (tackon #dest "TrollsCD"))))
			("")
			(message "\n\nFile \"TrollsCD\" is shorter than 267584 bytes.\n\nPlease make sure that this file is not packed\Nand if it is, please unpack it immediately after installation!")
		)
	)
    )
    ( ;ECS/AGA
	(copyfiles
		(help @copyfiles-help)
		(source ("%s.Slave" @app-name))
		(dest #dest)
	)
;	(copyfiles
;		(help @copyfiles-help)
;		(source ("%s.Imager" @app-name))
;		(dest #dest)
;	)
	(run ("Assign \"%s:\" \"%s\"" @app-name #dest))
	(run ("Patcher -p%s.Imager -s%s" @app-name #CI_drive))
	(run ("Assign \"%s:\" Remove" @app-name))

	(if (exists (tackon #dest #last-file))
	    ("")
	    (abort "Diskimaging not successful !\nThe Patcher couldn't create all files needed!")
	)
    )
)
(run ("Delete %s ALL QUIET FORCE" #cleanup))    ;delete temporary files

(exit)
