(procedure check-system-version
 (set exec-version (/ (getversion) 65536))
 (if (< exec-version 37)
  (abort "AmiWin 2.22demo needs at least Exec version 37.\n")
 )
)

(procedure select-destination-directory
 (set @default-dest
  (askdir
   (prompt "Select the directory where you want to "
    "install AmiWin 2.22demo. Most of the files do not "
    "have to be copied if you accept the default.")
   (help "Please specify the directory where you want "
    "to install AmiWin 2.22demo. If you accept the "
    "default, then the installation is done 'in-place', "
    "i.e. no files have to be copied. If you specify a "
    "different directory, then most of the files are "
    "copied there during the installation.")
   (default source-dir)
  )
 )
)  

(procedure copy-files-to-destination
 (if (= source-dir
  (expandpath @default-dest))
  (message "\nSource and destination directories are the same, "
   "installing 'in-place'.")
  (copyfiles
   (source (tackon source-dir "x11"))
   (dest "x11:")
   (all)
  )
 )
 (makedir "x11:Help" (infos))
 (copyfiles
  (source (tackon source-dir "AmiWin.guide"))
  (dest "x11:Help")
  (infos)
 )
 (copyfiles
  (source (tackon source-dir "REGISTRATION"))
  (dest "x11:Help")
  (infos)
 )
)

(procedure copy-ppipc
 (copylib (source (tackon source-dir "x11/libs/ppipc.library"))
  (dest "libs:")
  (optional "force" "askuser")
  (prompt "The Installer is about to install ppipc.library "
  "in your LIBS: directory.")
  (help "AmiWin needs ppipc.library for the local transport. "
  "If you skip this step during the installation, you have to "
  "install ppipc.library manually somewhere within your LIBS: "
  "assign.")
  (confirm)
 )
)

(procedure ask-config
 (set keymap-name
  (select
   (askchoice
    (prompt "Please select a keyboard driver to be "
     "used by AmiWin:")
    (help "AmiWin supports different keyboard drivers, "
     "which correspond to Workbench keyboard drivers. Please "
     "select one.")
    (choices "Canadien Français" "Suisse" "Schweiz" "Deutsch"
     "Dansk" "Español" "Français" "British" "Icelandic" "Italiana" "Norsk" "Português"
     "Svenskt" "American" "Dvorak")
    (default 13)
   )
   "cdn" "ch1" "ch2" "d" "dk" "e" "f" "gb" "is" "i" "n" "po"
   "s" "usa0" "usa2"
  )
 )
 (set usemui
  (askbool
   (prompt "Do you want to use the optional MUI interface ?\n\n"
    "You must have at least MUI 2.2 installed for it (not "
    "included in this archive)")
   (help "AmiWin has an optional MUI-based graphical frontend. "
    "If you want to use it you must have MUI 2.2 or higher installed.")
  )
 )
 (set driver-name
  (select
   (askchoice
    (prompt "Please select a display driver to be "
     "used by AmiWin:")
    (help "AmiWin supports different display drivers for "
     "different graphics boards and chipsets. Please "
     "select one.")
    (choices "RTG monochrome"
     "OCS/ECS/AGA monochrome"
     "OCS/ECS/AGA/A2024 4-256 colors"
     "Picasso 256-col (original VT software)"
     "CyberGraphX 40.55 or higher"
     "Retina 256 colors"
     "HRG 256-col (Merlin/Domino/VisionA)"
     "Graffity 256 colors")
    (default 0)
   )
   "rtg-1" "aga-1" "aga-8" "picasso-l-8" "cybergraphx-8" "retina-8" "hrg-8" "graffity-8"
  )
 )
 (set pstacks
  (askoptions
   (prompt "Please select which protocol stack(s) you "
    "want to use with AmiWin:")
   (help "AmiWin can be used across a TCP/IP network. "
    "If you want to use this feature please select the "
    "protocol stack(s) you are using.")
   (choices "AmiTCP/IP 2.x or higher" "AS-225 R2, I-225, MLink")
   (default 0)
  )
 )
 (set use-amitcp (= 1 (BITAND pstacks 1)))
 (set use-as225 (= 2 (BITAND pstacks 2)))
)      

(procedure set-env-vars
 (set env-equal-envarc (= (expandpath "ENV:") (expandpath "ENVARC:")))
 (if (not (exists "ENV:AMIWIN"))
  (makedir "ENV:AMIWIN")
 )
 (if (not env-equal-envarc)
  (if (not (exists "ENVARC:AMIWIN"))
   (makedir "ENVARC:AMIWIN")
  )
 )
 (if (exists "ENV:AMIWIN/XAW") 
  (delete "ENV:AMIWIN/XAW")
 )
 (if (not env-equal-envarc)
  (if (exists "ENVARC:AMIWIN/XAW") 
   (delete "ENVARC:AMIWIN/XAW")
  )
 )
 (textfile
  (dest "ENV:AMIWIN/AWOPTIONS")
  (append "-fp x11:lib/x11/fonts/misc\n")
  (append (cat "-keymap " keymap-name "\n"))
  (append "-lalt s\n")
  (append (if usemui "" "nogui\n"))
  (append "-ralt s\n")
  (append "-reset 0\n")
  (append (cat "-xsd " driver-name "\n"))
 )
 (if (not env-equal-envarc)
  (copyfiles
   (dest "ENVARC:AMIWIN")
   (source "ENV:AMIWIN/AWOPTIONS")
  )
 )
 (textfile
  (dest "ENV:AMIWIN/XTRANSPORTS")
  (append (if use-amitcp "amitcp\n" ""))
  (append (if use-as225 "as225\n" ""))
 )
 (if (not env-equal-envarc)
  (copyfiles
   (dest "ENVARC:AMIWIN")
   (source "ENV:AMIWIN/XTRANSPORTS")
  )
 )
 (textfile
  (dest "ENV:DISPLAY")
  (append "local:0\n")
 )
 (if (not env-equal-envarc)
  (copyfiles
   (dest "ENVARC:")
   (source "ENV:DISPLAY")
  )
 )
 (textfile
  (dest "ENV:XAUTHORITY")
  (append "X11:.Xauthority\n")
 )
 (if (not env-equal-envarc)
  (copyfiles
   (dest "ENVARC:")
   (source "ENV:XAUTHORITY")
  )
 )
 (textfile
  (dest "ENV:XFILESEARCHPATH")
  (append "x11:lib/x11/%T/%N\n")
 )
 (if (not env-equal-envarc)
  (copyfiles
   (dest "ENVARC:")
   (source "ENV:XFILESEARCHPATH")
  )
 )
)
 
(procedure add-to-startup
 (set startup-addition (cat
  ";BEGIN AmiWin 2.22d\n"
  "assign X11: \"" (tackon @default-dest "X11") "\"\n"
  "path X11:bin add\n"
  ";END AmiWin 2.22d\n"
  )
 )
 (if
  (askbool
   (prompt "Do you want Installer to make the required "
    "changes to your s:user-startup script ?")
   (help "AmiWin requires some additions to your "
    "s:user-startup script. Installer will make these "
    "changes for you, or you can choose to skip this "
    "step and make the changes yourself.")
  )
  (startup @app-name
   (command startup-addition)
   (prompt "Installer will modify your s:user-startup "
    "script. The following lines will be appended to "
    "it:\n\n" startup-addition)
   (help "AmiWin requires some additions to your "
    "s:user-startup script, that Installer is about to make.")
  )
  (textfile (dest (tackon @default-dest "addition-to-user-startup"))
   (append startup-addition)
  )
 )
)

;;;; Start

;(welcome "Welcome to the AmiWin 2.22demo installation.\n")

(set app-name "AmiWin 2.22demo")

(check-system-version)

(complete 0)

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

(select-destination-directory)

(if (not (exists (tackon @default-dest "x11")))
 (makedir (tackon @default-dest "x11") (infos))
)

(makeassign "X11" (tackon @default-dest "x11"))

(complete 10)

(copy-files-to-destination)

(complete 70)

(run (cat "protect x11:bin/startx +s"))

(complete 75)

(copy-ppipc)

(complete 80)

(ask-config)

(set-env-vars)

(complete 90)

(add-to-startup)

(complete 100)

(exit "Installation of AmiWin 2.22demo finished.\n\n"
 "You should reboot your machine now. After that to "
 "start AmiWin type 'startx'")
