| !Fetch_NS.!Run  2.50  17-03-2023
|
| Originally based on an idea by John Williams.
| Check http://aconet.nl/tools/#fetchns for the current version.
|
| Downloads and installs the latest version of NetSurf.
| Requires wget (or cURL - see !Help).

| Set application and choices directory and resource path
Set Fetch_NS$Dir <Obey$Dir>
Set Fetch_NS$Choices <Choices$Write>.Fetch_NS
Set Fetch_NSRes$Path <Obey$Dir>.Resources.

| Create the ShowText alias if it does not exist already
IfThere <ShowText$Dir>.ShowText Then Else Fetch_NSRes:!ShowText.!Boot

| Set report header for ShowText (requires version 2.02 or later)
Set Fetch_NS$MsgFrom [`Message from Fetch_NS

| Read the settings from the Choices structure or create it on the first run
IfThere Choices:Fetch_NS.Settings Then Obey Choices:Fetch_NS.Settings Else Obey Fetch_NSRes:FirstRun

| Set default if download directory was not specified
If "<Fetch_NS$Download$Dir>" = "" Then Set Fetch_NS$Download$Dir <Fetch_NS$Dir>.^

| 16k should be enough...
WimpSlot -min 16k -max 16k

| Bring PreviousVersion variable in line with LatestVersion
If "<Fetch_NS$LatestVersion>" > "" Then Set Fetch_NS$PreviousVersion <Fetch_NS$LatestVersion>

| Fetch the file which names the latest version
TaskWindow "Do <Fetch_NS$App> <Fetch_NS$Url>LATEST <Fetch_NS$Opt> <Fetch_NS$Choices>.LATEST" -wimpslot 4200k -name "Fetch LATEST file data" -quit

| BASIC routine reads the filename from the file and arranges to download it with an Obey file
Fetch_NSRes:SetLatest

| Skip the rest if SetLatest says so
If "<Fetch_NS$Aborted>" > "" Then ShowText <Fetch_NS$Aborted><Fetch_NS$MsgFrom>
If "<Fetch_NS$Aborted>" > "" Then Obey


| If NetSurf is to be installed, make sure SparkFS has at least been seen by the filer...
If "<Fetch_NS$Install$Dir>" > "" Then If "<SparkFS$Dir>" = "" Then Set Fetch_NS$Aborted SparkFS has not been seen by the Filer
If "<Fetch_NS$Aborted>" > "" Then ShowText <Fetch_NS$Aborted><Fetch_NS$MsgFrom>
If "<Fetch_NS$Aborted>" > "" Then Obey

| ...and start it if necessary
If "<Fetch_NS$Install$Dir>" > "" Then Fetch_NSRes:IfTask -nf SparkFiler <SparkFS$Dir>.!Run


| Run the actual Obey file to do the downloading
ShowText Fetching NetSurf development build #<Fetch_NS$LatestVersion>...<Fetch_NS$MsgFrom>
Run <Fetch_NS$Choices>.Action

| Abort if the download failed (i.e. no file of this minimum size can be found)
Fetch_NSRes:IfSize <Fetch_NS$Download$Dir>.<Fetch_NS$LatestVersion> < 2M Then Set Fetch_NS$Aborted Download failed
If "<Fetch_NS$Aborted>" > "" Then ShowText <Fetch_NS$Aborted><Fetch_NS$MsgFrom>
If "<Fetch_NS$Aborted>" > "" Then Obey

| Set the type (cannot be done earlier or curl will fail - known ImageFS 'feature')
SetType <Fetch_NS$Download$Dir>.<Fetch_NS$LatestVersion> zip

| Also exit here if there is no install directory
If "<Fetch_NS$Install$Dir>" = "" Then Set Fetch_NS$Aborted Install directory not specified
If "<Fetch_NS$Aborted>" > "" Then ShowText <Fetch_NS$Aborted><Fetch_NS$MsgFrom>
If "<Fetch_NS$Aborted>" > "" Then Obey

| Quit NetSurf if it is running
Fetch_NSRes:KillTask NetSurf

| Make sure the !Run (which stays open as long as NetSurf is running) is closed before proceding
IfThere <Fetch_NS$Install$Dir>.!NetSurf Then Fetch_NSRes:WaitWhileOpen <Fetch_NS$Install$Dir>.!NetSurf.!Run

| Remove any existing version
IfThere <Fetch_NS$Install$Dir>.!NetSurf Then Wipe <Fetch_NS$Install$Dir>.!NetSurf ~CFR~V

| Install the new version
Copy <Fetch_NS$Download$Dir>.<Fetch_NS$LatestVersion>.!NetSurf <Fetch_NS$Install$Dir>.!NetSurf A~C~DF~L~N~P~QR~S~T~V

| Run the new version
IfThere <Fetch_NS$Install$Dir> Then ShowText NetSurf installed. Starting...<Fetch_NS$MsgFrom>
IfThere <Fetch_NS$Install$Dir> Then StartDesktopTask <Fetch_NS$Install$Dir>.!NetSurf
