.KEY DEST,ARCH,BRANCH,SILENT/S

; $VER: uhcinstall 1.47 (19.10.2025) UHCTools Install-script
; This script will setup the needed ENV-variables and download
; and extract the UHC amigabin_xxx.lha file.

Failat 21

Set UHCMIRROR0 "http://uhc.megaburken.net/"
Set UHCMIRROR1 "http://uhc.driar.se/"
Set UHCMIRROR2 "http://uhc.a1k.org/"
Set UHCMIRROR3 "http://uhc.no0b.de/"

If "<ARCH>" NOT EQ ""
  Set OSTAG "<ARCH>"
  If "$OSTAG" EQ os3
    Skip OSDETECTDONE
  EndIf
  If "$OSTAG" EQ mos
    Skip OSDETECTDONE
  EndIf
  If "$OSTAG" EQ os4
    Skip OSDETECTDONE
  EndIf
  If "$OSTAG" EQ aros-i386
    Skip OSDETECTDONE
  EndIf
  If "$OSTAG" EQ aros-m68k
    Skip OSDETECTDONE
  EndIf
  Echo "$OSTAG is not supported"
  Skip CLEANUP
EndIf

Set SHOWCONFIGFILE "T:showconfig_out"
Version >NIL: exec.library 50
If WARN
  ; We are running some version of classic AmigaOS since all AmigaNG
  ; OS:es has exec v50 or higher, for now we just report back 3.x.
  Set OSTAG "os3"
  Skip OSDETECTDONE
EndIf
If EXISTS SYS:MorphOS
  Set OSTAG "mos"
  Skip OSDETECTDONE
EndIf
Version >NIL: newlib.library
If NOT WARN
  Set OSTAG "os4"
  Skip OSDETECTDONE
EndIf
Version >NIL: aros.library
If NOT WARN
  ; Default to aros-i386 if ShowConfig cannot be executed, should we?
  Set OSTAG "aros-i386"
  ShowConfig >"$SHOWCONFIGFILE"
  If NOT WARN
    Search >NIL: "$SHOWCONFIGFILE" "X86/LE"
    If NOT WARN
      Set OSTAG "aros-i386"
      Skip OSDETECTDONE
    EndIf
    Search >NIL: "$SHOWCONFIGFILE" "M68K/BE"
    If NOT WARN
      Set OSTAG "aros-m68k"
      Skip OSDETECTDONE
    EndIf
  Skip OSDETECTDONE
EndIf
Echo "*nArch not detected, aborting!"
Skip CLEANUP
Lab OSDETECTDONE

Set AGET="aget"
Which >NIL: aget
If WARN
  Which >NIL: wget
  If WARN
    Echo "*nCouldn't find aget or wget on your system so the installation can't continue."
    Skip CLEANUP
  Else
    Set UHCCOUNTER 0
    Lab WGETMIRRORLOOP
    Set UHCROOTURL "`Get "UHCMIRROR$UHCCOUNTER"`"
    If NOT "<BRANCH>" EQ ""
      Set UHCROOTURL "${UHCROOTURL}branches/<BRANCH>/"
    EndIf
    If "<SILENT>" EQ ""
      Echo "Fetching ${UHCROOTURL}aget.$OSTAG"
    EndIf
    Set PREVDIR="`Cd`"
    Cd T:
    Stack 65536
    wget --quiet "${UHCROOTURL}aget.$OSTAG"
    Cd "$PREVDIR"
    Unset PREVDIR
    If NOT EXISTS "T:aget.$OSTAG"
      Set UHCCOUNTER "`Eval "$UHCCOUNTER + 1"`"
      Get >NIL: "UHCMIRROR$UHCCOUNTER"
      If WARN
        Echo "Couldn't download the needed file, all mirrors appears to be down!*n*nTry again later!*n"
        Skip CLEANUP
      EndIf
      Echo "Using next mirror"
      Skip WGETMIRRORLOOP BACK
    EndIf

    Rename "T:aget.$OSTAG" T:aget >NIL:
    Set AGET="T:aget"
    ; Because some version of wget removes the executable flag
    Protect T:aget +e
  EndIf
EndIf

Set UHCARCHIVE "uhctools_${OSTAG}.lha"

Set DEST="SYS:UHC"
If "<DEST>" NOT EQ ""
  Set DEST="<DEST>"
EndIf
If "`Echo "$DEST" LEN=1`" EQ "/"
  Echo "$DEST must be entered without ending slash"
  Skip CLEANUP
EndIf

If "<SILENT>" EQ ""
  Echo "This script will download and extract the file ${UHCARCHIVE} to"
  Echo "the directory $DEST"
  Echo "It will also setup the required ENV-variables that allows you to update"
  Ask "the tools in the future. Proceed (y/n)?"
  If NOT WARN
    Echo "*nAborting...*n"
    Skip CLEANUP
  EndIf

  Echo ""
EndIf

If "`Echo "$DEST" LEN=1`" NOT EQ ":"
  If EXISTS "$DEST"
    If "<SILENT>" EQ ""
      Ask "$DEST already exists, do you want to continue anyway (y/n)?"
      If NOT WARN
        Echo "*nAborting...*n"
        Skip CLEANUP
      EndIf
    EndIf
  Else
    MakeDir "$DEST"
    If WARN
      Skip CLEANUP
    EndIf
  EndIf
  Set DEST="$DEST/"
EndIf

Set UHCCOUNTER 0
Lab MIRRORLOOP
Set UHCROOTURL "`Get "UHCMIRROR$UHCCOUNTER"`"
If NOT "<BRANCH>" EQ ""
  Set UHCROOTURL "${UHCROOTURL}branches/<BRANCH>/"
EndIf
If "<SILENT>" EQ ""
  Echo "Fetching ${UHCROOTURL}${UHCARCHIVE}"
EndIf
"$AGET" TIMEOUTSECONDS=10 QUIET "${UHCROOTURL}${UHCARCHIVE}" T:
Set AGETRC=$RC
If VAL $AGETRC GE 20
  ; FAIL means that there is a local error, like missing library - no point
  ; in trying another mirror, aget will print whatever issue it has.
  Skip CLEANUP
EndIf
If VAL $AGETRC GE 10
  Set UHCCOUNTER "`Eval "$UHCCOUNTER + 1"`"
  Get >NIL: "UHCMIRROR$UHCCOUNTER"
  If WARN
    Echo "Couldn't download the needed file, all mirrors appears to be down!*n*nTry again later!*n"
    Skip CLEANUP
  EndIf
  Echo "Using next mirror"
  Skip MIRRORLOOP BACK
EndIf

Which LhA >NIL:
If WARN
  Echo "*nLhA not found, downloading ${UHCROOTURL}lha.run"
  "$AGET" QUIET ${UHCROOTURL}lha.run T:
  T:lha.run lha_68k T: >NIL:
  Delete T:lha.run >NIL:
  Ask "Do you want to install the 68k version of LhA 2.15 to C: (y/n)?"
  If WARN
    Copy T:lha_68k C:LhA >NIL:
    Delete T:lha_68k >NIL:
    Set LHA C:LhA
  Else
    Set LHA T:lha_68k
  EndIf
Else
  Set LHA "`Which LhA`"
EndIf

If "<SILENT>" EQ ""
  Echo "Extracting ${UHCARCHIVE} to $DEST"
EndIf
Version >NIL: "$LHA"
If WARN
  ; UNIX lha does not have a version string, common in AROS
  "$LHA" "xfw=$DEST" "T:${UHCARCHIVE}" >NIL:
  If WARN
    Echo "*nFailed to extract ${UHCARCHIVE} to $DEST"
    Echo "Make sure your destination volume is not full and that you have a"
    Echo "proper version of Lha in your path."
    Skip CLEANUP
  EndIf
Else
  ; Amiga LhA
  Stack 8192
  "$LHA" x -mq "T:${UHCARCHIVE}" "$DEST"
  If WARN
    Echo "*nFailed to extract ${UHCARCHIVE} to $DEST"
    Echo "Make sure your destination volume is not full and that you have a"
    Echo "proper version of Lha in your path."
    Skip CLEANUP
  EndIf
EndIf

SetENV UHCBIN "$DEST"
Copy ENV:UHCBIN ENVARC:

If "<SILENT>" EQ ""
  Echo "*nWill now add a few lines to your S:User-Startup to set up assigns and"
  Ask "paths to UHC Tools. Proceed (y/n)?"
  If NOT WARN
    Execute ${UHCBIN}UHC-Startup
    Echo "*nRemember to add paths manually!"
    Skip UHCENV
  EndIf
EndIf

Echo ";BEGIN UHC" >T:User-Startup.UHCPart
Echo "  If EXISTS *"*${UHCBIN}UHC-Startup*"" >>T:User-Startup.UHCPart
Echo "    Execute *"*${UHCBIN}UHC-Startup*"" >>T:User-Startup.UHCPart
Echo "  EndIf" >>T:User-Startup.UHCPart
Echo ";END UHC*n" >>T:User-Startup.UHCPart

Execute T:User-Startup.UHCPart

Echo NOLINE >>S:User-Startup
Copy S:User-Startup "${UHCBIN}User-Startup.org"
Copy T:User-Startup.UHCPart S:User-Startup
Type "${UHCBIN}User-Startup.org" >>S:User-Startup

Lab UHCENV
If NOT EXISTS ENV:UHC
  MakeDir ENV:UHC
EndIf
SetENV UHC/TEMPDIR "T:"
SetENV UHC/SHOWARCHIVER "0"
SetENV UHC/SHOWPROGRESS "1"
SetENV UHC/SEARCHURL "uhc.megaburken.net/uhcsearch"
SetENV UHC/READER type
SetENV UHC/AMINETMIRROR de.aminet.net/aminet/
SetENV UHC/ARCH "$OSTAG"
If NOT "<BRANCH>" EQ ""
  SetENV UHC/BRANCH "<BRANCH>"
EndIf
If NOT EXISTS ENVARC:UHC
  MakeDir ENVARC:UHC
EndIf
Copy ENV:UHC/#? ENVARC:UHC/ ALL QUIET >NIL:

Protect UHC:S/#? +s >NIL:
If "${OSTAG}" NOT EQ "os4"
  Copy UHC:S/uhcupdate S: >NIL:
Else
  Copy UHC:S/uhcupdate S:Shell/ >NIL:
EndIf
Delete T:${UHCARCHIVE} >NIL:

If "<SILENT>" EQ ""
  Echo "*nWe're all done! Run *"uhcupdate*" to update all utilities in the future!*n"
EndIf

Lab CLEANUP
If EXISTS "T:$UHCARCHIVE"
  Delete "T:$UHCARCHIVE" >NIL:
EndIf
If EXISTS "T:aget"
  Delete "T:aget" >NIL:
EndIf
If EXISTS "T:lha_68k"
  Delete "T:lha_68k" >NIL:
EndIf
If EXISTS "$SHOWCONFIGFILE"
  Delete "$SHOWCONFIGFILE" >NIL:
EndIf
UnSet DEST
UnSet LHA
UnSet AGETRC
UnSet AGET
UnSet SHOWCONFIGFILE
UnSet OSTAG
UnSet UHCROOTURL
UnSet UHCARCHIVE
UnSet UHCCOUNTER
UnSet UHCMIRROR0
Unset UHCMIRROR1
UnSet UHCMIRROR2
UnSet UHCMIRROR3
