; Startup Sequence for HstWB Installer
; ------------------------------------
;
; Date: 2021-12-03
; Author: Henrik Noerfjand Stengaard
;
; AmigaDOS script for HstWB Installer self 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 build self install mode is not running in an emulator
Version >NIL: uaehf.device
IF NOT $RC EQ 0
  echo "*e[1mError: HstWB Installer requires build self install mode*N"
  echo "is running 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 build self 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" >>SYSTEMDIR: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" >>SYSTEMDIR: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:" >>SYSTEMDIR: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" >>SYSTEMDIR: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" >>SYSTEMDIR: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


; set amiga os 3.1 to 0, if DF0: volume name is 'Workbench3.2' or 'Workbench3.1.4'
set volumename "`DF0:C/Which DF0:`"
IF "$volumename" EQ "Workbench3.2:"
  set amigaos "3.2"

  IF NOT EXISTS "SYSTEMDIR:Prefs"
    MakePath >NIL: "SYSTEMDIR:Prefs"
  ENDIF
  Echo NOLINE "" >SYSTEMDIR:Prefs/Amiga-OS-32
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'" >>SYSTEMDIR: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

; goto start install, if amiga os 3.9 is 1
IF "$amigaos" EQ "3.9"
  SKIP startinstall
ENDIF

; goto start install, if amiga os 3.1 is 1
IF "$amigaos" EQ "3.1"
  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" >>SYSTEMDIR: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:" >>SYSTEMDIR: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:" >>SYSTEMDIR: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" >>SYSTEMDIR: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 libs
Copy >NIL: "DF1:Libs/Workbench.library" "SYSTEMDIR:Libs"

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


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

echo "Done"

; fail, if assign hstwb installer doesn't exist
IF NOT EXISTS S:Assign-HstWB-Installer
  echo "" >INSTALLDIR:Error
  echo "Error: assign hstwb installer doesn't exist!"
  SKIP end
ENDIF
execute S:Assign-HstWB-Installer

; load commands resident 
Resident >NIL: "INSTALLDIR:C/MakePath" PURE 
Resident >NIL: "INSTALLDIR:C/Reboot" PURE 
Resident >NIL: "INSTALLDIR:C/TotalReset" PURE
Resident >NIL: "INSTALLDIR:C/UAEquit" PURE
Resident >NIL: "INSTALLDIR:C/LhA" PURE
Resident >NIL: "INSTALLDIR:C/UNLZX" PURE
Resident >NIL: "INSTALLDIR:C/UnZip" PURE


echo ""
echo "*e[1mBuilding self install*e[0m"
echo "Copying boot scripts and tools..."
copy >NIL: "INSTALLDIR:Boot-SelfInstall/" TO "SYSTEMDIR:" ALL

; delete all from prefs env archive directory, if it doesn't exist. otherwise create it
IF EXISTS "SYSTEMDIR:Prefs/Env-Archive"
  Delete >NIL: "SYSTEMDIR:Prefs/Env-Archive/#?" ALL
ELSE
  MakePath >NIL: "SYSTEMDIR:Prefs/Env-Archive"
ENDIF

; copy prefs env archive directory to system dir
copy >NIL: "INSTALLDIR:Prefs/Env-Archive" "SYSTEMDIR:Prefs/Env-Archive" ALL

; delete all from install directory, if it doesn't exist. otherwise create it
IF EXISTS "HSTWBINSTALLERDIR:Install/"
  Delete >NIL: "HSTWBINSTALLERDIR:Install/#?" ALL
ELSE
  MakePath >NIL: "HSTWBINSTALLERDIR:Install"
ENDIF

; copy self install scripts and tools
echo "Copying self install scripts and tools..."
copy >NIL: "INSTALLDIR:Install-SelfInstall" TO "HSTWBINSTALLERDIR:Install/" ALL

; delete all from packages directory, if it doesn't exist. otherwise create it
IF EXISTS "HSTWBINSTALLERDIR:Packages/"
  Delete >NIL: "HSTWBINSTALLERDIR:Packages/#?" ALL
ELSE
  MakePath >NIL: "HSTWBINSTALLERDIR:Packages"
ENDIF

; copy Packages
echo "Copying packages..."
copy >NIL: "PACKAGESDIR:" TO "HSTWBINSTALLERDIR:Packages/" ALL

; delete all from licenses directory, if it doesn't exist. otherwise create it
IF EXISTS "HSTWBINSTALLERDIR:Licenses/"
  Delete >NIL: "HSTWBINSTALLERDIR:Licenses/#?" ALL
ELSE
  MakePath >NIL: "HSTWBINSTALLERDIR:Licenses"
ENDIF

; copy licenses
echo "Copying licenses..."
copy >NIL: "INSTALLDIR:Licenses" TO "HSTWBINSTALLERDIR:Licenses/" ALL

; create install complete prefs
echo "" >INSTALLDIR:Prefs/Install-Complete

; done
echo "Done"
echo ""
echo "Installation is now complete."
echo ""
echo "Please let the system wait 10 seconds before continuing"
echo "to allow file system to write changes to disk." 
echo ""
echo "Continue will quit UAE in 10 seconds."
echo ""
ask "Press ENTER to continue"
echo ""


; end
; ---
LAB end
echo "UAE will quit in 10 seconds..."
wait 10
uaequit >NIL: