; $Id: Install 1.10 2006/05/07 19:38:46 wepl Exp wepl $

(set #multiple-versions "yes")		;set to <> "" if there are multiple versions
(set #version "3")			;set if no multiple versions 0=RawDIC 1=Patcher 2=DIC 3=Files
(set #sub-dir "")			;sub directory containing data files
(set #highs-file "highs")		;name of high scores file

(set #readme-file "ReadMe")		;name of readme file
(set #man-file "Manual")		;name of manual file
(set #hint-file "Hints")		;name of hint file
(set #sol-file "Solution")		;name of solution file
(set #extra-file "Extra")		;name of extra file
(set #extra2-file "Extra2")		;name of second extra file
(set #extra3-file "Extra3")		;name of third extra file
(set #prefix @app-name)			;name of slave, directory
(set #version_suffix "")		;

(set #ico_suffix_00 "inf")		;Icon suffixes (not all of these must exist)
(set #ico_suffix_01 "magicwb")
(set #ico_suffix_02 "romicon")
(set #ico_suffix_03 "newicon")
(set #ico_suffix_04 "newexot")
(set #ico_suffix_05 "colicon")
(set #ico_suffix_06 "colexot")
(set #ico_suffix_07 "colfrank")
(set #ico_suffix_08 "colchris")
(set #ico_suffix_09 "colcode")
(set #ico_suffix_10 "colcfou")
(set #ico_suffix_11 "colcfou2")
(set #ico_suffix_12 "boxicon")

;Icon descriptions (max 21 chars if you have > 9 entries, or 27 chars otherwise.
;These are renamed to "gamename.info" so no illegal characters like :/* etc
;		  "123456789012345678901234567"
(set #ico_desc_00 "Normal")
(set #ico_desc_01 "MagicWB Icon")
(set #ico_desc_02 "RomIcon")
(set #ico_desc_03 "NewIcon")
(set #ico_desc_04 "NewIcon by Exotica")
(set #ico_desc_05 "OS3.5 Icon")
(set #ico_desc_06 "OS3.5 Icon by Exotica")
(set #ico_desc_07 "OS3.5 Icon by Frank")
(set #ico_desc_08 "OS3.5 Icon by Chris Vella")
(set #ico_desc_09 "OS3.5 Icon by Codetapper")
(set #ico_desc_10 "OS3.5 Icon by CFou 1")
(set #ico_desc_11 "OS3.5 Icon by CFou 2")
(set #ico_desc_12 "OS3.5 BoxIcon")

;---------------------------------------------------------------------
; Which version does the user want to install? This routine doesn't need
; to be changed UNLESS there are multiple versions.

(procedure P_SelectVersion
  (if (= #multiple-versions "")
    ("")
    (
      (set #version
        (askchoice
          (prompt ("Which version of %s would you like to install?" @app-name))
          (default 0)
          (choices "using disk image" "using extracted files")
          (help @askchoice-help)
        )
      )

      ; Certain versions may use different slaves, so set that up here if
      ; required. It will change the install directory and the slaves used.
       
      (select #version
;        ("")
        (set #version_suffix "")		;Tack on AGA to the slave name(s)
        ( (set #version_suffix "-files")
	  (set #sub-dir "data")
	)		
      )

;      (set #prefix ("%s%s" #prefix #version_suffix))
    )
  )
)

;---------------------------------------------------------------------
; Make the disk images for the install

(procedure P_MakeImages
  (select #version

    (				;----- 2 = DIC version (Disk Images) -----
      (P_AskSrcDrv)
  
      ;the following lines must be copied and adjusted for multiple disk images
  
      (set #CI_disknum 1)
      (set #CI_diskname ("%s Disk %ld" @app-name #CI_disknum))
      (set #CI_disklen 901120)
      (set #CI_skiptrk "")
      (P_CreateImage)
  
    )

    (				;----- 3 = DOS version (Files) -----
      (set #AD_disk "Calephar")
      (P_disk)
      (copyfiles
        (help @copyfiles-help)
        (source ("%s:" #AD_disk))
        (dest #dest)
        (pattern "~(c|l|s|libs)")
      )
    )

  )
)

;----------------------------
; Create all the icons for the install. If the game requires multiple icons
; set them up here by calling P_SelectIcon multiple times, one for each
; icon/slave that the game needs. For 99% of cases, you do not need to 
; change this routine.
;
; If you have not setup the WHDLoad tooltypes in the icons, you should
; change the line(s) below to (set #icon_set_slave_preload "true")

(procedure P_MakeIcons
  ; Uncomment the following lines if you have a separate intro slave/icon
  ;(set #icon_desc "intro")
  ;(set #icon_prefix ("%s%s" "Intro" #version_suffix))
  ;(set #icon_slave #icon_prefix)
  ;(set #icon_set_slave_preload "")
  ;(set #icon_set_deftool_stack "true")
  ;(P_CopySlave)
  ;(P_SelectIcon)

  (set #icon_desc "game")
  (set #icon_prefix #prefix)
  (set #icon_slave #icon_prefix)
  (set #icon_set_slave_preload "")
  (set #icon_set_deftool_stack "true")
  (P_CopySlave)
  (P_SelectIcon)
)

; ** ATTENTION! YOU SHOULD NOT HAVE TO CHANGE ANYTHING BEYOND THIS POINT! **


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

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

;----------------------------
; decrunch one file using XFD package
; IN:  #file - file to decrunch (full path)
; OUT: -

(procedure P_XFDDecrunch
  (set #program "xfddecrunch")
  (set #package "XFD")
  (P_ChkRun_Package)
  (if
     (<> 0 (run ("%s \"%s\" >CON:///1000//CLOSE" #program #file)))
     (abort ("\"%s\" has failed to decompress the file \"%s\"" #program #file))
  )
)

;----------------------------
; Wait for inserting disk
; IN:  #AD_disk - name of disk
; OUT: -

(procedure P_disk
  (askdisk
    (dest #AD_disk)
    (prompt ("\nInsert Disk \"%s\" in any drive!" #AD_disk))
    (help @askdisk-help)
  )
)

;----------------------------
; ask for source drive (DIC installs)

(procedure P_AskSrcDrv
  (set #dcnt 0)
  (set #CI_cfg (getenv "WHDLSrcDrv"))
  (if (#CI_cfg)
    (if (getassign (substr #CI_cfg 0 (- (strlen #CI_cfg) 1)) "d")
      (set #dcnt (+ 1 #dcnt)) (set #CI_cfg "")
    )
  )
  (set #CI_df0 "")
  (if (<> "DF0:" #CI_cfg)
    (if (getassign "DF0" "d") ((set #dcnt (+ 1 #dcnt)) (set #CI_df0 "DF0:")))
  )
  (set #CI_df1 "")
  (if (<> "DF1:" #CI_cfg)
    (if (getassign "DF1" "d") ((set #dcnt (+ 1 #dcnt)) (set #CI_df1 "DF1:")))
  )
  (set #CI_df2 "")
  (if (<> "DF2:" #CI_cfg)
    (if (getassign "DF2" "d") ((set #dcnt (+ 1 #dcnt)) (set #CI_df2 "DF2:")))
  )
  (set #CI_df3 "")
  (if (<> "DF3:" #CI_cfg)
    (if (getassign "DF3" "d") ((set #dcnt (+ 1 #dcnt)) (set #CI_df3 "DF3:")))
  )
  (set #CI_rad "")
  (if (<> "RAD:" #CI_cfg)
    (if (getassign "RAD" "d") ((set #dcnt (+ 1 #dcnt)) (set #CI_rad "RAD:")))
  )
  (set #CI_ff0 "")
  (if (<> "FF0:" #CI_cfg)
    (if (getassign "FF0" "d") ((set #dcnt (+ 1 #dcnt)) (set #CI_ff0 "FF0:")))
  )
  (if (< #dcnt 1) (set #CI_cfg "DF0:"))
  (set #CI_ask "Enter Device Name")
  (set #CI_drive
    (askchoice
      (prompt "Select source drive for diskimages")
      (choices #CI_cfg #CI_df0 #CI_df1 #CI_df2 #CI_df3 #CI_rad #CI_ff0 #CI_ask)
      (help @askchoice-help)
    )
  )
  (select #CI_drive
    (set #CI_drive #CI_cfg)
    (set #CI_drive #CI_df0)
    (set #CI_drive #CI_df1)
    (set #CI_drive #CI_df2)
    (set #CI_drive #CI_df3)
    (set #CI_drive #CI_rad)
    (set #CI_drive #CI_ff0)
    (set #CI_drive
      (askstring
        (prompt "Select source drive for diskimages")
        (default "DF0:")
        (help @askstring-help)
      )
    )
  )
  (run ("setenv WHDLSrcDrv \"%s\"\ncopy ENV:WHDLSrcDrv ENVARC:" #CI_drive))
)

;----------------------------
; ask for source drive (RawDIC/Patcher installs)

(procedure P_AskSrcDrvRaw
  (set #dcnt 0)
  (set #CI_cfg (getenv "WHDLSrcDrvRaw"))
  (if (#CI_cfg)
    (if (getassign (substr #CI_cfg 0 (- (strlen #CI_cfg) 1)) "d")
      (set #dcnt (+ 1 #dcnt)) (set #CI_cfg "")
    )
  )
  (set #CI_df0 "")
  (if (<> "DF0:" #CI_cfg)
    (if (getassign "DF0" "d") ((set #dcnt (+ 1 #dcnt)) (set #CI_df0 "DF0:")))
  )
  (set #CI_df1 "")
  (if (<> "DF1:" #CI_cfg)
    (if (getassign "DF1" "d") ((set #dcnt (+ 1 #dcnt)) (set #CI_df1 "DF1:")))
  )
  (set #CI_df2 "")
  (if (<> "DF2:" #CI_cfg)
    (if (getassign "DF2" "d") ((set #dcnt (+ 1 #dcnt)) (set #CI_df2 "DF2:")))
  )
  (set #CI_df3 "")
  (if (<> "DF3:" #CI_cfg)
    (if (getassign "DF3" "d") ((set #dcnt (+ 1 #dcnt)) (set #CI_df3 "DF3:")))
  )
  (if (< #dcnt 1)
    (set #CI_drive "DF0:")
    (if (> #dcnt 1)
      (
	(set #CI_drive
	  (askchoice
	    (prompt "Select source drive for diskimages")
	    (choices #CI_cfg #CI_df0 #CI_df1 #CI_df2 #CI_df3)
	    (help @askchoice-help)
	  )
	)
	(select #CI_drive
	  (set #CI_drive #CI_cfg)
	  (set #CI_drive #CI_df0)
	  (set #CI_drive #CI_df1)
	  (set #CI_drive #CI_df2)
	  (set #CI_drive #CI_df3)
	)
      )
      (set #CI_drive (cat #CI_cfg #CI_df0 #CI_df1 #CI_df2 #CI_df3))
    )
  )
  (run ("setenv WHDLSrcDrvRaw \"%s\"\ncopy ENV:WHDLSrcDrvRaw ENVARC:" #CI_drive))
)

;----------------------------
; Create Image using DIC
; #dest        - path to save image in
; #CI_disknum  - number of the disk image to create
; #CI_diskname - label of the disk
; #CI_disklen  - length of disk image to create
; #CI_skiptrk  - track to skip, -1 means no skip
; #CI_drive    - drive to create image from

(procedure P_CreateImage
  (message
    (cat
	"\nInsert \"" #CI_diskname "\" into drive " #CI_drive "!\n\n"
	"(make sure it's the right disk because it will not be checked)"
    )
  )
  (if (= #CI_skiptrk "")
    (set #option "")
    (set #option (cat " SKIPTRACK=" #CI_skiptrk))
  )
  (if
    (= 0
      (run
        (cat
          "cd \"" #dest "\"\n"
          "DIC " #CI_drive " FD=" #CI_disknum " LD=" #CI_disknum " SIZE="
          #CI_disklen #option " PEDANTIC >CON:///1000//CLOSE"
        )
      )
    )
    (run ("FileNote \"%s.%ld\" \"%s\" Quiet" (tackon #dest "disk") #CI_disknum @app-name))
    (abort "\"DIC\" has failed to create a diskimage")
  )
)

;----------------------------
; Select a file (when unsure what disk the file exists on)
; IN:  #SF_prompt           - message to display when asking for the file
;      #SF_default_filename - default filename
; OUT: #SF_filename         - output filename or "" if nothing selected

(procedure P_SelectFile
  (set #SF_filename "")
  (set #SF_filename
    (askfile
      (prompt #SF_prompt)
      (help @askfile-help)
      (default #SF_default_filename)
    )
  )
)

;----------------------------
; copy file including icon if exist
; #copy-file - name of file to copy

(procedure P_CopyFile
  (if (exists #copy-file) (
    (copyfiles
      (help @copyfiles-help)
      (source #copy-file)
      (dest #dest)
    )
    (if (exists ("%s.info" #copy-file)) (
      (copyfiles
        (help @copyfiles-help)
        (source ("%s.info" #copy-file))
        (dest #dest)
      )
      (tooltype
        (dest (tackon #dest #copy-file))
        (noposition)
      )
    ))
  ))
)

;----------------------------
; copy a WHDLoad slave
; #icon_slave - name of the slave to copy (without .Slave extension)

(procedure P_CopySlave
  (copyfiles
    (help @copyfiles-help)
    (source ("%s%s.Slave" #icon_slave #version_suffix))
    (newname ("%s.Slave" #icon_slave))
    (dest #dest)
  )
)
;(set #prefix ("%s%s" #prefix #version_suffix))
;----------------------------

(procedure P_Icon
  (copyfiles
    (help @copyfiles-help)
    (source ("%s.%s" #icon_prefix #icon-suf))
    (newname ("%s.info" #icon-name))
    (dest #icon-dir)
  )
  (tooltype
    (dest (tackon #icon-dir #icon-name))
    (noposition)
  )
)

;----------------------------
; Select an icon by presenting the user with a list of all found icons
; IN:  #icon_prefix - filename prefix for this icon (eg. intro, game)
;      #icon_desc   - description of the icon (eg. intro, game)
;      #icon_slave  - name of the slave the tooltype Slave= should be set 
;                     to (without the .slave suffix)

(procedure P_SelectIcon
  (set #icon 0)
  (set #icnt 0)
  (if (exists ("%s.%s" #icon_prefix #ico_suffix_12)) ((set #icon 12)(set #icnt (+ 1 #icnt))(set #icon_12 #ico_desc_12)) (set #icon_12 ""))
  (if (exists ("%s.%s" #icon_prefix #ico_suffix_11)) ((set #icon 11)(set #icnt (+ 1 #icnt))(set #icon_11 #ico_desc_11)) (set #icon_11 ""))
  (if (exists ("%s.%s" #icon_prefix #ico_suffix_10)) ((set #icon 10)(set #icnt (+ 1 #icnt))(set #icon_10 #ico_desc_10)) (set #icon_10 ""))
  (if (exists ("%s.%s" #icon_prefix #ico_suffix_09)) ((set #icon 9) (set #icnt (+ 1 #icnt))(set #icon_09 #ico_desc_09)) (set #icon_09 ""))
  (if (exists ("%s.%s" #icon_prefix #ico_suffix_08)) ((set #icon 8) (set #icnt (+ 1 #icnt))(set #icon_08 #ico_desc_08)) (set #icon_08 ""))
  (if (exists ("%s.%s" #icon_prefix #ico_suffix_07)) ((set #icon 7) (set #icnt (+ 1 #icnt))(set #icon_07 #ico_desc_07)) (set #icon_07 ""))
  (if (exists ("%s.%s" #icon_prefix #ico_suffix_06)) ((set #icon 6) (set #icnt (+ 1 #icnt))(set #icon_06 #ico_desc_06)) (set #icon_06 ""))
  (if (exists ("%s.%s" #icon_prefix #ico_suffix_05)) ((set #icon 5) (set #icnt (+ 1 #icnt))(set #icon_05 #ico_desc_05)) (set #icon_05 ""))
  (if (exists ("%s.%s" #icon_prefix #ico_suffix_04)) ((set #icon 4) (set #icnt (+ 1 #icnt))(set #icon_04 #ico_desc_04)) (set #icon_04 ""))
  (if (exists ("%s.%s" #icon_prefix #ico_suffix_03)) ((set #icon 3) (set #icnt (+ 1 #icnt))(set #icon_03 #ico_desc_03)) (set #icon_03 ""))
  (if (exists ("%s.%s" #icon_prefix #ico_suffix_02)) ((set #icon 2) (set #icnt (+ 1 #icnt))(set #icon_02 #ico_desc_02)) (set #icon_02 ""))
  (if (exists ("%s.%s" #icon_prefix #ico_suffix_01)) ((set #icon 1) (set #icnt (+ 1 #icnt))(set #icon_01 #ico_desc_01)) (set #icon_01 ""))
  (if (exists ("%s.%s" #icon_prefix #ico_suffix_00)) ((set #icon 0) (set #icnt (+ 1 #icnt))(set #icon_00 #ico_desc_00)) (set #icon_00 ""))
  (if (> #icnt 1) (
    (if (= #newstuff 1) (
      (set #icon-dir ("T:%s Icons" #icon_prefix))
      (makedir #icon-dir
        (help @makedir-help)
      )
      (if #icon_00 ((set #icon-suf #ico_suffix_00) (set #icon-name #ico_desc_00) (P_Icon)))
      (if #icon_01 ((set #icon-suf #ico_suffix_01) (set #icon-name #ico_desc_01) (P_Icon)))
      (if #icon_02 ((set #icon-suf #ico_suffix_02) (set #icon-name #ico_desc_02) (P_Icon)))
      (if #icon_03 ((set #icon-suf #ico_suffix_03) (set #icon-name #ico_desc_03) (P_Icon)))
      (if #icon_04 ((set #icon-suf #ico_suffix_04) (set #icon-name #ico_desc_04) (P_Icon)))
      (if #icon_05 ((set #icon-suf #ico_suffix_05) (set #icon-name #ico_desc_05) (P_Icon)))
      (if #icon_06 ((set #icon-suf #ico_suffix_06) (set #icon-name #ico_desc_06) (P_Icon)))
      (if #icon_07 ((set #icon-suf #ico_suffix_07) (set #icon-name #ico_desc_07) (P_Icon)))
      (if #icon_08 ((set #icon-suf #ico_suffix_08) (set #icon-name #ico_desc_08) (P_Icon)))
      (if #icon_09 ((set #icon-suf #ico_suffix_09) (set #icon-name #ico_desc_09) (P_Icon)))
      (if #icon_10 ((set #icon-suf #ico_suffix_10) (set #icon-name #ico_desc_10) (P_Icon)))
      (if #icon_11 ((set #icon-suf #ico_suffix_11) (set #icon-name #ico_desc_11) (P_Icon)))
      (if #icon_12 ((set #icon-suf #ico_suffix_12) (set #icon-name #ico_desc_12) (P_Icon)))
      (openwbobject #icon-dir)
    ))
    (set #icon
      (askchoice
        (prompt ("\nWhich %s icon would you like to install?\n" #icon_desc))
	(choices #icon_00 #icon_01 #icon_02 #icon_03 #icon_04 #icon_05 #icon_06 #icon_07 #icon_08 #icon_09 #icon_10 #icon_11 #icon_12)
        (default #icon)
        (help @askchoice-help)
      )
    )
    (if (= #newstuff 1) (
      (closewbobject #icon-dir)
      (run ("Delete \"%s\" All Quiet Force" #icon-dir))
    ))
  ))
  (if (> #icnt 0) (
    (select #icon
      (set #icon #ico_suffix_00)
      (set #icon #ico_suffix_01)
      (set #icon #ico_suffix_02)
      (set #icon #ico_suffix_03)
      (set #icon #ico_suffix_04)
      (set #icon #ico_suffix_05)
      (set #icon #ico_suffix_06)
      (set #icon #ico_suffix_07)
      (set #icon #ico_suffix_08)
      (set #icon #ico_suffix_09)
      (set #icon #ico_suffix_10)
      (set #icon #ico_suffix_11)
      (set #icon #ico_suffix_12)
    )
    (copyfiles
      (help @copyfiles-help)
      (source ("%s.%s" #icon_prefix #icon))
      (newname ("%s.info" #icon_prefix))
      (dest #dest)
    )
    ;installer has problems on deleting tooltypes with newicons
    (if (<> (substr #icon 0 3) "new")
      (tooltype
        (dest (tackon #dest #icon_prefix))
        (settooltype "Slave")
        (settooltype "PreLoad")
      )
    )
    (tooltype
      (dest (tackon #dest #icon_prefix))
      (settooltype "PreLoad" "")
      (settooltype "Slave" ("%s.Slave" #icon_slave))
    )
    (tooltype
      (dest (tackon #dest #icon_prefix))
      (setdefaulttool "WHDLoad")
      (setstack 10240)
      (noposition)
    )
  ))
)

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

(if (< @installer-version (+ (* 44 65536) 10))
  (
    (message
      (cat
        "Warning: your installer is outdated.\n"
        "Some features of this installation won't be available, "
        "such as icon show and drawer opening. "
        "You have version " (/ @installer-version 65536) "." 
        (BITAND @installer-version 65535) ", recommended is version 44.10. "
        "The 'installer' 44.10 comes with OS 3.5 but is also contained in the NDK 3.9. "
        "You may also use InstallerNG by Jens Trger."
        "\n\n"
        "The installers can be obtained from the net:\n"
        "http://www.amiga.com/3.9/download/NDK3.9.lha\n"
        "aminet:util/sys/InstallerNG.lha"
      )
    )
    (set #newstuff 0)
  )
  (set #newstuff 1)
)

(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 #program "RawDIC")
(P_ChkRun)

(set #program "Patcher")
(P_ChkRun)

(set #program "DIC")
(P_ChkRun)

(if (getenv "WHDLInstPath")
  (set @default-dest (getenv "WHDLInstPath"))
)

(set #version 0)
(P_SelectVersion)

(set #dest
  (askdir
    (prompt
      (cat
	"Where should \"" @app-name "\" be installed?\n"
	"A drawer \"" #prefix "\" will be automatically created."
      )
    )
    (help @askdir-help)
    (default @default-dest)
  )
)
(run ("setenv WHDLInstPath \"%s\"\ncopy ENV:WHDLInstPath ENVARC:" #dest))
(set #dest (tackon #dest #prefix))
(if (exists #dest) (
  (set #choice
    (askbool
      (prompt
        (cat
          "\nDirectory \"" #dest "\" already exists.\n"
          "Should it be deleted?"
        )
      )
      (default 1)
      (choices "Delete" "Skip")
      (help @askbool-help)
    )
  )
  (if (= #choice 1)
    (run ("Delete \"%s\" \"%s.info\" All Quiet Force" #dest #dest))
  )
))
(makedir #dest
  (help @makedir-help)
  (infos)
)

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

(P_MakeIcons)
(set #copy-file #readme-file)
(P_CopyFile)
(set #copy-file #man-file)
(P_CopyFile)
(set #copy-file #hint-file)
(P_CopyFile)
(set #copy-file #sol-file)
(P_CopyFile)
(set #copy-file #extra-file)
(P_CopyFile)
(set #copy-file #extra2-file)
(P_CopyFile)
(set #copy-file #extra3-file)
(P_CopyFile)
(set #dest-org #dest)
(if (= #sub-dir "")
  ("")
  (
    (set #dest (tackon #dest #sub-dir))
    (makedir #dest
      (help @makedir-help)
    )
  )
)
(set #copy-file #highs-file)
(P_CopyFile)
(P_MakeImages)
(if (= #newstuff 1)
  (openwbobject #dest-org)
)
(set @default-dest #dest-org)
(exit)
