(procedure check-system-version
 (set exec-version (/ (getversion) 65536))
 (if (< exec-version 37)
  (abort "Miami 2.1 needs Kickstart 2.04 or higher.\n")
 )
 (if (< exec-version 39)
  (set ag-browser "AmigaGuide")
  (set ag-browser "MultiView")
 )
)

(procedure select-destination-directory
 (if (exists "Miami:" (noreq))
  (
   (set old-miami-assign 1)
   (set old-miami-assign-location (expandpath "Miami:"))
   (set @default-dest
    (askdir
     (prompt "Select the directory where you want to "
      "install Miami 2.1. You already have a 'Miami:' "
      "assign on your system, so it is assumed that this "
      "is the location of an older version of Miami that "
      "you want to upgrade.")
     (help "Select the directory where you want to "
      "install Miami 2.1. Since you already have a 'Miami:' "
      "assign on your system you should probably accept "
      "the default.")
     (default "Miami:")
    )
   )
  ) (
   (set old-miami-assign 0)
   (set @default-dest
    (askdir
     (prompt "Select the directory where you want to "
      "install Miami 2.1. Most of the files do not have "
      "to be copied if you accept the default. A separate "
      "directory WILL NOT be created at the location you "
      "specify.")
     (help "Select the directory where you want to "
      "install Miami 2.1. You can choose to install "
      "'in-place', i.e. within the directory where your "
      "Miami files are currently located, or you can have "
      "Miami installed in a different location, copying "
      "all files.")
     (default source-dir)
    )
   )
  )
 )
 (makeassign "Miami" @default-dest)
)

(procedure check-registered
 (set is-registered (exists (tackon @default-dest "Miami.key2")))
)

(procedure select-cpu
 (if is-registered
  (set cpu-choice
   (askchoice
    (prompt "Please select which version of\nMiami you want to use:")
    (help "Miami exists in a 68000 version,\n a 68020 version and an\n"
     "unregistered version. The 68000 and 68020\n"
     "versions can only be used by registered users.")
    (choices "unregistered version" "registered 68000 version" "registered 68020 version")
   )
  )
  (set cpu-choice 0)
 )
)

(procedure check-binary
 (set bin-choice
  (select cpu-choice
   "Miami.eval"
   "Miami.000"
   "Miami.020"
  )
 )
 (if (not (exists (tackon source-dir bin-choice)))
  (abort "Before you can install Miami you first need to\n"
   "download and unpack the archive that contains the Miami\n"
   "binary you want to install.")
 )
)

(procedure select-icon
 (set icon-choice
  (askchoice
   (prompt "Please select which icons you\nwant to use:")
   (help "Miami comes with three different\nicon styles. Please select\n"
    "the one you prefer.")
   (choices "do not install new icons" "standard" "Magic Workbench" "NewIcons")
  )
 )
)

(procedure check-in-place
 (set is-in-place
  (= (expandpath source-dir) (expandpath @default-dest))
 )
)

(procedure copy-file
 (copyfiles
  (source (tackon source-dir to-copy))
  (dest @default-dest)
  (optional force)
 )
)

(procedure copy-files
 (if (exists (tackon @default-dest "Miami"))
  (delete (tackon @default-dest "Miami") force)
 )
 (if (not (= icon-choice 0)) (
  (if (exists (tackon @default-dest "Miami.info"))
   (delete (tackon @default-dest "Miami.info") force)
  )
 ))
 (if is-in-place
  (
   (select cpu-choice
    (
     (delete (tackon @default-dest "Miami.020") force)
     (delete (tackon @default-dest "Miami.020.info") force)
     (delete (tackon @default-dest "Miami.000") force)
     (delete (tackon @default-dest "Miami.000.info") force)
     (rename (tackon @default-dest "Miami.eval") (tackon @default-dest "Miami"))
     (delete (tackon @default-dest "Miami.eval.info") force)
    )
    (
     (delete (tackon @default-dest "Miami.020") force)
     (delete (tackon @default-dest "Miami.020.info") force)
     (delete (tackon @default-dest "Miami.eval") force)
     (delete (tackon @default-dest "Miami.eval.info") force)
     (rename (tackon @default-dest "Miami.000") (tackon @default-dest "Miami"))
     (delete (tackon @default-dest "Miami.000.info") force)
    )
    (
     (delete (tackon @default-dest "Miami.000") force)
     (delete (tackon @default-dest "Miami.000.info") force)
     (delete (tackon @default-dest "Miami.eval") force)
     (delete (tackon @default-dest "Miami.eval.info") force)
     (rename (tackon @default-dest "Miami.020") (tackon @default-dest "Miami"))
     (delete (tackon @default-dest "Miami.020.info") force)
    )
   )
   (complete 50)
  ) (
   (select cpu-choice
    (set to-copy "Miami.eval")
    (set to-copy "Miami.000")
    (set to-copy "Miami.020")
   )
   (copy-file)
   (select cpu-choice
    (rename (tackon @default-dest "Miami.eval") (tackon @default-dest "Miami"))
    (rename (tackon @default-dest "Miami.000") (tackon @default-dest "Miami"))
    (rename (tackon @default-dest "Miami.020") (tackon @default-dest "Miami"))
   )
   (set to-copy "Miami.guide") (copy-file)
   (set to-copy "Miami.guide.info") (copy-file)
   (set to-copy "MiamiArp") (copy-file)
   (set to-copy "MiamiFinger") (copy-file)
   (set to-copy "MiamiIfConfig") (copy-file)
   (set to-copy "MiamiInit") (copy-file)
   (set to-copy "MiamiInit.info") (copy-file)
   (set to-copy "MiamiInitSANA2") (copy-file)
   (set to-copy "MiamiInitSANA2.info") (copy-file)
   (set to-copy "MiamiMapMBone") (copy-file)
   (set to-copy "MiamiMRInfo") (copy-file)
   (set to-copy "MiamiMRouteD") (copy-file)
   (set to-copy "MiamiMTrace") (copy-file)
   (set to-copy "MiamiNetStat") (copy-file)
   (set to-copy "MiamiPing") (copy-file)
   (set to-copy "MiamiRegister") (copy-file)
   (set to-copy "MiamiRegister.info") (copy-file)
   (set to-copy "MiamiResolve") (copy-file)
   (set to-copy "MiamiRoute") (copy-file)
   (set to-copy "MiamiSysCtl") (copy-file)
   (set to-copy "MiamiTCPDump") (copy-file)
   (set to-copy "MiamiTraceRoute") (copy-file)
   (set to-copy "Miami_README") (copy-file)
   (set to-copy "Miami_README.info") (copy-file)
   (complete 25)
   (if (not (exists (tackon @default-dest "libs")))
    (makedir (tackon @default-dest "libs"))
   )
   (copyfiles
    (source (tackon source-dir "libs"))
    (dest (tackon @default-dest "libs"))
    (all)
   )
  )
 )
 (if (not (= icon-choice 0)) (
  (set to-copy
   (select icon-choice
    ""
    "Icons/Standard/Miami.info"
    "Icons/MagicWB/Miami.info"
    "Icons/NewIcons/Miami.info"
   )
  )
  (copyfiles
   (source (tackon source-dir to-copy))
   (dest @default-dest)
  )
  (copyfiles
   (source (tackon source-dir to-copy))
   (dest @default-dest)
   (newname "MiamiInit.info")
  )
  (copyfiles
   (source (tackon source-dir to-copy))
   (dest @default-dest)
   (newname "MiamiInitSANA2.info")
  )
  (copyfiles
   (source (tackon source-dir to-copy))
   (dest @default-dest)
   (newname "MiamiRegister.info")
  )
 ))
 (complete 50)
)

(procedure set-tooltypes
 (if (not (= icon-choice 0)) (
  (tooltype
   (dest (tackon @default-dest "Miami"))
   (noposition)
  )
  (tooltype
   (dest (tackon @default-dest "MiamiInit"))
   (noposition)
  )
  (tooltype
   (dest (tackon @default-dest "MiamiInitSANA2"))
   (noposition)
  )
  (tooltype
   (dest (tackon @default-dest "MiamiRegister"))
   (noposition)
  )
  (tooltype
   (dest (tackon @default-dest "Miami.guide"))
   (noposition)
   (setdefaulttool ag-browser)
  )
 ))
)

(procedure add-to-startup
 (if old-miami-assign
  (if (= (old-miami-assign-location (expandpath @default-dest)))
   (set change-startup 0)
   (set change-startup 1)
  )
  (set change-startup 1)
 )
 (if change-startup
  (
   (set startup-addition (cat
    "assign Miami: \"" (expandpath @default-dest) "\"\n"
    )
   )
   (if
    (askbool
     (prompt "It is strongly recommended that an assign "
      "'Miami:' is created for the Miami installation "
      "directory. Miami by itself will work fine without "
      "this assign, but some utility programs like "
      "MiamiTCPDump need it. Would you like Installer "
      "to make the required change to your user-startup "
      "file ?")
     (help "It is strongly recommended that an assign "
      "'Miami:' is created for the Miami installation "
      "directory. Miami by itself will work fine without "
      "this assign, but some utility programs like "
      "MiamiTCPDump need it. Please indicate if you would like Installer "
      "to make the required change to your user-startup "
      "file.")
     (choices "Yes" "No")
    )
    (startup "Miami"
     (command startup-addition)
     (prompt "Installer will modify your s:user-startup "
      "script. The following lines will be added:\n\n"
      startup-addition)
     (help "Installer will modify your s:user-startup "
      "script. The following lines will be added:\n\n"
      startup-addition)
    )
    (textfile
     (dest (tackon @default-dest "addition-to-user-startup"))
     (append startup-addition)
    )
   )
  )
 )
)
     

;;;; Start

(welcome "Welcome to the Miami 2.1 installation.\n")

(set app-name "Miami 2.1")

(check-system-version)

(complete 0)

(set source-dir (if (= 1 (exists @icon)) (pathonly (expandpath @icon))
 (expandpath @icon))
)

(select-destination-directory)

(check-registered)

(select-cpu)

(check-binary)

(select-icon)

(check-in-place)

(copy-files)

(set-tooltypes)

(complete 75)

(add-to-startup)

(complete 100)
