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

(set #readme-file "f17challengehd.readme") ;name of readme file

;****************************
;----------------------------
; 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 #readme-file)
  (if
    (= 0 (run ("SYS:Utilities/Multiview %s" #readme-file)))
    ("")
    (run ("SYS:Utilities/More %s" #readme-file))
  )
)

(set #program "WHDLoad")
(P_chkrun)

; in expert mode ask for source drive
(if
  (= @user-level 2)
  (
    (set #CI_unit
      (askchoice
	(prompt "Select source drive for diskimages")
	(default 0)
	(choices "DF0:" "DF1:" "DF2:" "DF3:")
	(help @askchoice-help)
      )
    )
  )
)

(set #CI_drive ("DF%ld:" #CI_unit))

(set @default-dest
  (askdir
    (prompt ("Where should \"%s\" installed ?\nA drawer \"%s\" will 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 do you like to install ?\n")
      (default 0)
      (choices "Normal" "NewIcon")
      (help @askchoice-help)
    )
  )
  (set #icon 0)
)
(select #icon
  (set #icon ("%s.inf" @app-name))
  (set #icon ("%s.newicon" @app-name))
)
(copyfiles
  (help @copyfiles-help)
  (source #icon)
  (newname ("%s.info" @app-name))
  (dest #dest)
)
(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)
  )
)

(copyfiles
	(help @copyfiles-help)
	(source "blk")
	(dest #dest)
	(infos)
)

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

(message ("\n\n\n\n\n\Please insert %s Disk 1 into %s" @app-name #CI_drive))

	(if
		(= 0 (run ("boot2file %ld \"%s/boot1\" >\"con:///1000/Ripping Disk1 Boot.../CLOSE\"" #CI_unit #dest )))
		("")
		(abort "\"boot2file\" must be in your PATH !")
	)

	(if
		(= 0 (run ("rob2file %ld \"%s/data1\" 2 121 0x4349534F >con:///1000//CLOSE" #CI_unit #dest )))
		("")
		(abort "\"rob2file\" must be in your PATH !")
	)

	(if
		(= 0 (run ("C:join \"%s/boot1\" \"%s/blk\" \"%s/data1\" AS \"%s/Disk.1\" " #dest #dest #dest #dest)))
		("")
		(abort "Could not join boot and image!")
	)

(run ("Delete \"%s/boot1\" QUIET FORCE" #dest))
(run ("Delete \"%s/data1\" QUIET FORCE" #dest))


(message ("\n\n\n\n\n\Please insert %s Disk 2 into %s" @app-name #CI_drive))

	(if
		(= 0 (run ("boot2file %ld \"%s/boot2\" >\"con:///1000/Ripping Disk2 Boot.../CLOSE\"" #CI_unit #dest )))
		("")
		(abort "\"boot2file\" must be in your PATH !")
	)

	(if
		(= 0 (run ("rob2file %ld \"%s/data2\" 2 159 0x4349534F >con:///1000//CLOSE" #CI_unit #dest )))
		("")
		(abort "\"rob2file\" must be in your PATH !")
	)

	(if
		(= 0 (run ("C:join \"%s/boot2\" \"%s/blk\" \"%s/data2\" AS \"%s/Disk.2\" " #dest #dest #dest #dest)))
		("")
		(abort "Could not join boot and image!")
	)

(run ("Delete \"%s/boot2\" QUIET FORCE" #dest))
(run ("Delete \"%s/data2\" QUIET FORCE" #dest))
(run ("Delete \"%s/blk\" QUIET FORCE" #dest))

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

(exit)
