;****************************

(set #sub-dir     "data")		;sub directory containing data files
(set #readme-file "ReadMe")		;name of readme file
(set #osemu-file  "OSEmu.400")	;name of osemu file

;****************************
;----------------------------
; checks if given program is installed, if not abort install
; #program - to check

(procedure P_ChkRun
  (if
    (= 0 (run ("cd SYS:\nWhich %s" #program)))
    ("")
    (abort
      (cat
	"You must install \"" #program "\" first!\n"
	"It must be accessible via the path.\n"
	"You can find it in the WHDLoad package."
      )
    )
  )
)

;****************************

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

(set #program "WHDLoad")
(P_ChkRun)

(set @default-dest
  (askdir
    (prompt ("Where should \"%s\" be installed?\nA drawer \"%s\" will be automatically created." @app-name @app-name))
    (help @askdir-help)
    (default @default-dest)
    (disk)
  )
)
(set #dest (tackon @default-dest @app-name))
(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)
  (infos)
)

;----------------------------

(copyfiles
  (help @copyfiles-help)
  (source ("%s.slave" @app-name))
  (dest #dest)
)
(if
  (exists ("%s.newicon" @app-name))
  (set #icon
    (askchoice
      (prompt "\nWhich icon would you like to install?\n")
      (default 0)
      (choices "Normal" "NewIcon" "ColorIcon")
      (help @askchoice-help)
    )
  )
  (set #icon 0)
)
(select #icon
  (set #icon ("%s.orgicon" @app-name))
  (set #icon ("%s.newicon" @app-name))
  (set #icon ("%s.colicon" @app-name))
)
(copyfiles
  (help @copyfiles-help)
  (source #icon)
  (newname ("%s.info" @app-name))
  (dest #dest)
)
(tooltype
  (dest (tackon #dest @app-name))
  (settooltype "SLAVE" ("%s.slave" @app-name))
  (settooltype "PRELOAD" "")
  (setdefaulttool "WHDLoad")
  (setstack 10240)
  (noposition)
)
(if
  (exists #readme-file)
  (copyfiles
    (help @copyfiles-help)
    (source #readme-file)
    (dest #dest)
  )
)
(if
  (exists ("%s.info" #readme-file))
  (
    (copyfiles
      (help @copyfiles-help)
      (source ("%s.info" #readme-file))
      (dest #dest)
    )
    (tooltype
      (dest (tackon #dest #readme-file))
      (noposition)
    )
  )
)

(if
  (= #sub-dir "")
  ("")
  (
    (set #dest (tackon #dest #sub-dir))
    (makedir #dest
      (help @makedir-help)
    )
  )
)
(if
  (exists #osemu-file)
  (copyfiles
    (help @copyfiles-help)
    (source #osemu-file)
    (dest #dest)
  )
)

;----------------------------

; cant use regular P_Disk as game disks for
; each version are different names
(message ("\nInsert %s disq in DF0:, then click \"Proceed\"" @app-name))

(copyfiles
	(prompt "Copying game files to \"" #dest "\"")
	(help @copyfiles-help)
	(source "DF0:")
	(pattern "(shapes|starwars|1|list)")
	(dest #dest)
	(optional fail force)
)

; fix for seppo's cracked version, maybe others
(if (<> 1 (exists (tackon #dest "starwars")))(
	(if (= 1 (exists (tackon #dest "1")))(
		(rename (tackon #dest "1") (tackon #dest "starwars"))
	))
))

;----------------------------

(exit)
