; Installer for prism2.device.
; Copyright  2005-2013 Neil Cafferkey.
; $VER: "Install prism2.device" 2.3 (28.8.2013)


; Set constants

(set #dev-name "prism2.device")
(set #devs-source "Devs/Networks")
(set @default-dest "DEVS:Networks")
(set #pccard-name "pccard.library")
(set #libs-source "Libs")
(set #libs-dest "LIBS:")
(set #util-name "WirelessManager")
(set #c-source "C")
(set #c-dest "C:")
(set #firmware-source "Devs/Firmware")
(set #firmware-dest "DEVS:Firmware")
(set #startup-command (cat "Run <>NIL: C:" #util-name " " #dev-name))

(set #kick-too-low-msg "Kickstart 3.0 or greater is required.")
(set #passphrase-error-msg
   "An error occurred generating the wireless preferences file."
)
(set #ask-pccard-prompt
   "Do you want to install pccard.library?"
)
(set #ask-pccard-help
   (cat "pccard.library is needed if you want to use PCMCIA cards in an"
      " A1200's or A600's PCMCIA slot. It is unnecessary when using"
      " PCI-to-PCMCIA adapters."
   )
)
(set #ask-firmware-prompt
   "Do you want to install firmware for the Hermes chipsets?"
)
(set #ask-firmware-help
   (cat "A firmware file is needed for some PCMCIA cards, namely those"
      " based on the Hermes-I or Hermes-II chipsets."
      " Hermes-I cards do not need the firmware if WPA/WPA2 is not used."
      " The files will be installed in \"" #firmware-dest "\"."
      " The firmware is ignored if unneeded, and no permanent changes"
      " are ever made to any card."
   )
)
(set #ask-config-prompt
   "Do you want to set your wireless network preferences now?"
)
(set #ask-config-help
   (cat "To connect to a WPA/WPA2-protected network, a wireless preferences"
      " file needs to be created. If you answer Yes, you will be asked to"
      " enter your network name and WPA passphrase. The secret key for your"
      " WPA/WPA2 network will then be calculated, and a wireless"
      " preferences file will be saved with this information. Note that key"
      " generation may take a long time (up to several minutes with a slow"
      " CPU). If you answer No, you will need to create a preferences file"
      " using a text editor. See the manual for more details."
   )
)
(set #ask-ssid-prompt
   "What is your main network's name (SSID)?"
)
(set #ask-ssid-help
   (cat "The name of your network will be stored in the wireless"
      " preferences and is also needed to generate the secret key."
   )
)
(set #ask-pass-prompt
   "What is your main network's WPA/WPA2 passphrase?"
)
(set #ask-pass-help
   (cat "The passphrase will be used in combination with the network name"
      " to generate the secret key. Note that if you change your network"
      " name, the secret key will need to be regenerated. If you want to"
      " enter a hexadecimal key directly, enter a dummy passphrase here,"
      " and then read the manual section regarding how to configure keys"
      " manually."
   )
)
(set #ask-userstartup-prompt
   "Do you want WirelessManager to be started automatically during boot up?"
)
(set #ask-userstartup-help
   (cat "WirelessManager negotiates and maintains a connection to your"
      " wireless network. If you answer Yes, it will be added to your"
      " S:User-Startup script and remain running in the background."
      " If you answer No, you will need to start WirelessManager manually"
      " to connect. Note that WirelessManager does not start your TCP/IP"
      " stack."
   )
)
(set #os4-startup-prompt
   (cat "Installation complete!\n\n"
      "To enable WirelessManager to be started automatically during"
      " boot up, please manually add the following line to your"
      " S:Startup-sequence script, on the line before AddNetInterfaces or"
      " Network-Startup is called:\n\n"
   )
)


; Check OS version isn't too old

(if (< (/ (getversion) 65536) 39)
   (abort #kick-too-low-msg)
)


; Check which OS we're running

(set #is-mos (= (run "Version \"MorphOS\"") 0))
(set #is-os4 (AND (>= (/ (getversion) 65536) 50) (NOT #is-mos)))


; Install pccard.library on OSs with PCMCIA support

(if (AND (NOT #is-os4) (NOT #is-mos))
   (copylib
      (source (tackon #libs-source #pccard-name))
      (dest #libs-dest)
      (help @copylib-help)
   )
)


; Install device

(if #is-os4 (set #devs-source (tackon #devs-source "OS4")))
(if #is-mos (set #devs-source (tackon #devs-source "MOS")))

(copyfiles
   (source
      (tackon #devs-source #dev-name)
   )
   (dest @default-dest)
   (help @copylib-help)
)


; Install the wireless parameter utility

(copylib
   (source (tackon #c-source #util-name))
   (dest #c-dest)
   (help @copylib-help)
)


; Install Hermes firmware

(if
   (if
      (= @user-level 2)
      (askbool
         (prompt #ask-firmware-prompt)
         (help #ask-firmware-help)
      )
      1
   )
   (copyfiles
      (source #firmware-source)
      (dest #firmware-dest)
      (all)
      (help @copylib-help)
   )
)


; Ask for the network name and passphrase

(if
   (if
      (= @user-level 2)
      (askbool
         (prompt #ask-config-prompt)
         (help #ask-config-help)
      )
      1
   )
   (
      (set #ssid
         (askstring (prompt #ask-ssid-prompt) (help #ask-ssid-help))
      )
      (set #pass
         (askstring (prompt #ask-pass-prompt) (help #ask-pass-help))
      )


      ; Generate wireless configuration file

      (working "Generating key...\n\n" "(this may take several minutes)")
      (if
         (<> 0
            (run
               ("C/wpa_passphrase \"%s\" \"%s\" >>ENVARC:Sys/Wireless.prefs"
                  #ssid
                  #pass
               )
            )
         )
         (abort #passphrase-error-msg)
      )
   )
)


; Start WirelessManager at boot

(if
   #is-os4
   (exit (cat #os4-startup-prompt #startup-command) (quiet))
   (startup
      #util-name
      (prompt #ask-userstartup-prompt)
      (help #ask-userstartup-help)
      (command #startup-command)
   )
)

