; Startup Sequence for HstWB Installer
; ------------------------------------
;
; Date: 2021-11-02
; Author: Henrik Noerfjand Stengaard
;
; AmigaDOS script for HstWB Installer install


; add alias
Alias cls "Echo NOLINE *"*E[0;0H*E[J*" "

; set failat to 21
FailAt 21

; set amiga os to 3.1
set amigaos "3.1"

; fail, if exec.library is less than v40
Version >NIL: exec.library 40
IF WARN
  echo "*e[1mError: HstWB Installer requires minimum Kickstart 3.1 rom!*e[0m"
  SKIP fail
ENDIF

; fail, if install mode is not running in an emulator
Version >NIL: uaehf.device
IF NOT $RC EQ 0
  echo "*e[1mError: HstWB Installer requires install mode is running*N"
  echo "in an emulator!*e[0m"
  SKIP fail
ENDIF

; fail, if install mode is using scsi.device (scsi.device is not compatible with large harddisks)
GiggleDisk >NIL: device=scsi.device
IF $RC EQ 0
  echo "*e[1mError: HstWB Installer requires install mode is using*N"
  echo "scsi.device and not UAE HD controller (uaehf.device)!*e[0m"
  SKIP fail
ENDIF

; set stack
stack 16384

; add assigns from S:AssignList using AssPlus
MakePath >NIL: RAM:Env
AssPlus >NIL:

; create temp systemdir directory, if it doesn't exist
IF NOT EXISTS "SYSTEMDIR:Temp/"
  MakePath >NIL: "SYSTEMDIR:Temp"
ENDIF

; copy hstwb installer log to system dir, if it exists
IF EXISTS INSTALLDIR:hstwb-installer.log
  Copy >NIL: INSTALLDIR:hstwb-installer.log SYSTEMDIR:hstwb-installer.log
ENDIF


; startup message
; ---------------
cls 
echo "*e[32m" NOLINE
echo "HstWB Installer v[$VersionText]"
echo "*e[0m*e[1m" NOLINE
echo "-----------------[$VersionDashes]"
echo "*e[0m" NOLINE
echo ""
echo "HstWB Installer is now running install."
echo ""
echo "*e[1mLoading Amiga OS system files:*e[0m"
echo "Amiga OS 3.9 cd-rom in CD0:..."

; set failat to 255 for diskindrive to fail silently
failat 255

; goto find amiga os disk, if CD0: device is not present
DiskInDrive >NIL: CD0:
IF NOT $RC EQ 0 VAL
  ; log
  echo "CD0: device is not present" >>INSTALLDIR:Temp/Amiga-OS-System-Files.log

  ; set failat to 21
  failat 21

  SKIP amigaosdisk
ENDIF

; goto find amiga os disk, if AmigaOS3.9: device is not present
DiskInDrive >NIL: AmigaOS3.9:
IF NOT $RC EQ 0 VAL
  ; log
  echo "Amiga OS 3.9 cd-rom is not present in CD0: device" >>INSTALLDIR:Temp/Amiga-OS-System-Files.log

  ; set failat to 21
  failat 21

  SKIP amigaosdisk
ENDIF

; set failat to 21
failat 21

; goto find amiga os disk, if AmigaOS3.9:EMERGENCY-BOOT doesn't exist
IF NOT EXISTS "AmigaOS3.9:EMERGENCY-BOOT/C"
  SKIP amigaosdisk
ENDIF

; load system files from amiga os 3.9 emergency boot cd-rom
set systemfilesdir "AmigaOS3.9:EMERGENCY-BOOT/C"
set amigaos "3.9"
SKIP loadsystemfiles


; amiga os disk
; -------------
LAB amigaosdisk

; log
echo "Amiga OS 3.2, 3.1.4 or 3.1 Workbench disk in DF0:" >>INSTALLDIR:Temp/Amiga-OS-System-Files.log
echo "Amiga OS 3.2, 3.1.4 or 3.1 Workbench disk in DF0:..."

; set failat to 255 for diskindrive to fail silently
failat 255

; fail, if DF0: device is not present
DiskInDrive >NIL: DF0:
IF NOT $RC EQ 0 VAL
  ; log
  echo "Device DF0: is not present" >>INSTALLDIR:Temp/Amiga-OS-System-Files.log

  echo "*e[1mERROR: Device DF0: is not present!*e[0m"

  ; set failat to 21
  failat 21

  SKIP fail
ENDIF

; set failat to 21
failat 21

; fail, if which doesn't exist in device DF0:
IF NOT EXISTS "DF0:C/Which"
  ; log
  echo "Amiga OS 3.2, 3.1.4 or 3.1 Workbench disk in floppy device DF0: is not valid" >>INSTALLDIR:Temp/Amiga-OS-System-Files.log

  echo "*e[1mERROR: Amiga OS 3.2, 3.1.4 or 3.1 Workbench disk in floppy device"
  echo "DF0: is not valid!*e[0m"

  SKIP fail
ENDIF

; get df0: volume name
set volumename "`DF0:C/Which DF0:`"


; set amiga os to 3.2, if DF0: volume name is 'Workbench3.2'
IF "$volumename" EQ "Workbench3.2:"
  set amigaos "3.2"
ENDIF

; set amiga os to 3.1.4, if DF0: volume name is 'Workbench3.1.4'
IF "$volumename" EQ "Workbench3.1.4:"
  set amigaos "3.1.4"
ENDIF


; load system files from amiga os 3.2, 3.1.4 or 3.1 workbench disk in DF0: device
set systemfilesdir "DF0:C"


; load system files
; -----------------
LAB loadsystemfiles

; log
echo "Loading Amiga OS system files from '$systemfilesdir'" >>INSTALLDIR:Temp/Amiga-OS-System-Files.log

; load amiga os system files resident
resident "$systemfilesdir/Assign" PURE
resident "$systemfilesdir/Copy" PURE
resident "$systemfilesdir/Delete" PURE
resident "$systemfilesdir/DiskChange" PURE
resident "$systemfilesdir/Eval" PURE

IF EXISTS "$systemfilesdir/Execute"
  resident "$systemfilesdir/Execute" PURE
ENDIF

resident "$systemfilesdir/List" PURE
resident "$systemfilesdir/MakeDir" PURE
resident "$systemfilesdir/Mount" PURE
resident "$systemfilesdir/Protect" PURE
resident "$systemfilesdir/Rename" PURE
resident "$systemfilesdir/RequestChoice" PURE
resident "$systemfilesdir/Search" PURE 
resident "$systemfilesdir/Type" PURE
resident "$systemfilesdir/Version" PURE
resident "$systemfilesdir/Wait" PURE 
resident "$systemfilesdir/Which" PURE 

MakeDir RAM:Clipboards RAM:ENV/Sys
SetEnv TZ MST7

; delete systemdir prefs, if it doesnt exist
IF EXISTS "SYSTEMDIR:Prefs"
  Delete >NIL: "SYSTEMDIR:Prefs" ALL FORCE
ENDIF

; create systemdir prefs
MakePath >NIL: "SYSTEMDIR:Prefs"

; create amiga os 3.9 prefs and skip to start install, if amiga os is 3.9
IF "$amigaos" EQ "3.9"
  Echo NOLINE "" >INSTALLDIR:Prefs/Amiga-OS-39
  Echo NOLINE "" >INSTALLDIR:Prefs/Amiga-OS-390-CD
  Echo NOLINE "" >SYSTEMDIR:Prefs/Amiga-OS-39
  SKIP startinstall
ENDIF

; create amiga os 3.2 prefs, if amiga os is 3.2
IF "$amigaos" EQ "3.2"
  Echo NOLINE "" >INSTALLDIR:Prefs/Amiga-OS-32
  Echo NOLINE "" >SYSTEMDIR:Prefs/Amiga-OS-32
ENDIF

; create amiga os 3.1.4 prefs, if amiga os is 3.1.4
IF "$amigaos" EQ "3.1.4"
  Echo NOLINE "" >INSTALLDIR:Prefs/Amiga-OS-314
  Echo NOLINE "" >SYSTEMDIR:Prefs/Amiga-OS-314
ENDIF

; create amiga os 3.1 prefs and skip to start install, if amiga os is 3.1
IF "$amigaos" EQ "3.1"
  Echo NOLINE "" >INSTALLDIR:Prefs/Amiga-OS-31
  Echo NOLINE "" >SYSTEMDIR:Prefs/Amiga-OS-31

  SKIP startinstall
ENDIF

; set failat to 255 for diskindrive to fail silently
failat 255

; fail, if DF1: device is not present
DiskInDrive >NIL: DF1:
IF NOT $RC EQ 0 VAL
  ; log
  echo "Device DF1: is not present" >>INSTALLDIR:Temp/Amiga-OS-System-Files.log

  echo "*e[1mERROR: Device DF1: is not present!*e[0m"

  ; set failat to 21
  failat 21

  SKIP fail
ENDIF


set installdisk "0"
IF "`Which DF1:`" EQ "Install3.2:"
  set installdisk "1"
  echo "Amiga OS 3.2 Install disk in DF1:" >>INSTALLDIR:Temp/Amiga-OS-System-Files.log
  echo "Amiga OS 3.2 Install disk in DF1:..."

  IF EXISTS "DF1:C/Execute"
    resident "DF1:C/Execute" PURE
  ENDIF
ELSE
  IF "`Which DF1:`" EQ "Install3.1.4:"
    set installdisk "1"
    echo "Amiga OS 3.1.4 Install disk in DF1:" >>INSTALLDIR:Temp/Amiga-OS-System-Files.log
    echo "Amiga OS 3.1.4 Install disk in DF1:..."
  ENDIF
ENDIF

; fail, if floppy disk in device DF1: doesn't have volume name 'Install3.2' or 'Install3.1.4'
IF "$installdisk" EQ "0" VAL
  ; log
  echo "Amiga OS 3.2 or 3.1.4 Install disk in floppy device DF1: is not valid" >>INSTALLDIR:Temp/Amiga-OS-System-Files.log

  echo "*e[1mERROR: Amiga OS 3.2, 3.1.4 Install disk in floppy device"
  echo "DF1: is not valid!*e[0m"

  SKIP fail
ENDIF


; create sys libs directory, if it doesn't exist
IF NOT EXISTS "SYSTEMDIR:Libs"
  MakePath >NIL: "SYSTEMDIR:Libs"
ENDIF

; copy workbench.library to systemdir and installdir libs
Copy >NIL: "DF1:Libs/Workbench.library" "SYSTEMDIR:Libs"
Copy >NIL: "SYSTEMDIR:Libs/Workbench.library" "INSTALLDIR:Libs"

; copy icon.library to systemdir and installdir libs
Copy >NIL: "DF1:Libs/Icon.library" "SYSTEMDIR:Libs"
Copy >NIL: "SYSTEMDIR:Libs/Icon.library" "INSTALLDIR:Libs"


; start install
; -------------
LAB startinstall

echo "Done"
echo ""
ask "Press ENTER to continue"

; log
echo "" >>SYS:hstwb-installer.log
type "INSTALLDIR:Temp/Amiga-OS-System-Files.log" >>SYS:hstwb-installer.log

; con shell for amiga os 3.2 and kingcon shell for amiga os 3.9, 3.1.4 and 3.1
IF "$amigaos" EQ "3.2"
  ; run start-selfinstall in new con shell
  NewShell WINDOW "CON:0/0/640/300/HstWB Installer v[$VersionText]/AUTO" FROM "INSTALLDIR:S/Start-Install" >NIL:
ELSE
  ; mount kingcon
  Mount KCON: from INSTALLDIR:Devs/Mountlist-KingCON
  Mount KRAW: from INSTALLDIR:Devs/Mountlist-KingCON

  ; run start-selfinstall in new kingcon shell
  NewShell WINDOW "KCON:0/0/640/300/HstWB Installer v[$VersionText]/AUTO" FROM "INSTALLDIR:S/Start-Install" >NIL:
ENDIF

EndShell

SKIP end


; fail
; ----
LAB fail
echo ""
echo "Installation failed."
quit


; end
; ---
LAB end
