.key EMU68DEVICE/K,EMU68PATH/K
.bra {
.ket }

STACK 80000

FAILAT 21

ASSIGN >NIL: AmiSSL: EXISTS 
IF WARN
  ECHO "AmiSSL not found!"
  ECHO ""
  ECHO "This window will close in 3 seconds"
  Wait 3
  EndCLI
ENDIF

ASSIGN Emu68-Updater: ""

IF NOT EXISTS ENV:PistormVariant
	SYS:C/EMU68INFO variant >NIL:
	IF WARN
		ECHO "It seems you're not running Emu68, this window will close in 3 seconds"
		SKIP Cleanup
	ELSE
		SYS:C/EMU68INFO variant >ENV:PistormVariant
	ENDIF
ENDIF

ECHO "Welcome to Emu68 & VideoCore Updater!"
ECHO ""
ECHO "This script will update your current Emu68 version and VideoCore version to the latest official release available on Github."
ECHO ""

ASSIGN >NIL: {EMU68DEVICE} EXISTS
IF WARN 
  C:Mount {EMU68DEVICE} >NIL:
    IF WARN
      ECHO "Cannot find/mount the device named {EMU68DEVICE}!"
      ECHO "Please check your EMU68DEVICE value by opening the Emu68-Updater file with an editor."
      ECHO ""
      SKIP VC4_Update
	ENDIF
ENDIF

IF EXISTS "{EMU68DEVICE}{EMU68PATH}Emu68-$PistormVariant"
    ECHO "Found {EMU68DEVICE}{EMU68PATH}Emu68-$PistormVariant"
    ECHO ""
ELSE
    ECHO "Cannot find Emu68-$PistormVariant in {EMU68DEVICE}{EMU68PATH}!"
    ECHO "Please check your EMU68PATH value by opening the Emu68-Updater file with an editor and confirming it matches with the path defined in {EMU68DEVICE}config.txt (kernel=<pathtoemu68file>) on your SD card."
    ECHO ""
    SKIP VC4_Update
ENDIF

IF NOT EXISTS "{EMU68DEVICE}{EMU68PATH}Backup_Emu68"
  MAKEDIR "{EMU68DEVICE}{EMU68PATH}Backup_Emu68" >NIL:
ENDIF

c:AreWeOnline
IF WARN
  ECHO "System is currently offline, please enable your internet connection then try again."
  ECHO ""
  ECHO "This window will close in 3 seconds."
  SKIP Cleanup
ENDIF

ECHO "Downloading Emu68 JSON file..."
ECHO ""

c:aget https://api.github.com/repos/michalsc/Emu68/releases TO RAM:releases.json >NIL:
IF WARN
   ECHO "Unable to connect to Github, waiting 5 seconds before retrying..."
   WAIT 5
   ECHO ""
   ECHO "Second attempt at downloading Emu68 JSON file..."
   ECHO ""
   c:aget https://api.github.com/repos/michalsc/Emu68/releases TO RAM:releases.json >NIL:
   IF WARN
     ECHO "It seems the system is currently unable to connect to Github, please try again later."
	 ECHO ""
	 ECHO "This window will close in 3 seconds."
	 SKIP Cleanup
   ENDIF
ENDIF

RX Emu68-Updater:tag.rexx >NIL:

ECHO "Downloading latest Emu68-$PistormVariant release..."
ECHO ""

c:aget https://github.com/michalsc/Emu68/releases/download/$TagENV/Emu68-$PistormVariant.zip TO RAM:Emu68-$PistormVariant.zip >NIL:
IF WARN
   ECHO "Unable to connect to Github, waiting 5 seconds before retrying..."
   WAIT 5
   ECHO ""
   ECHO "Second attempt at downloading latest Emu68-$PistormVariant release..."
   ECHO ""
   c:aget https://github.com/michalsc/Emu68/releases/download/$TagENV/Emu68-$PistormVariant.zip TO RAM:Emu68-$PistormVariant.zip >NIL:
   IF WARN
     ECHO "It seems the system is currently unable to connect to Github, please try again later."
	 ECHO ""
	 ECHO "This window will close in 3 seconds."
	 SKIP Cleanup
   ENDIF
ENDIF

ECHO "Unpacking..."
ECHO ""

c:UnZip -o RAM:Emu68-$PistormVariant.zip -d RAM:Emu68upd_temp >NIL:

ECHO "Starting Emu68 update process..."
ECHO ""

SET OldEmu68Version `Version "{EMU68DEVICE}{EMU68PATH}Emu68-$PistormVariant"`
SET NewEmu68Version `Version RAM:Emu68upd_temp/Emu68-$PistormVariant`
ECHO "Version on your Amiga: $OldEmu68Version "
ECHO "Version found online: $NewEmu68Version. "

RX Emu68-Updater:VersionChecker.rexx "$OldEmu68Version" "$NewEmu68Version" >NIL:

IF NOT WARN
  ASK "New $NewEmu68Version version found, do you want to update your current $OldEmu68Version version? Y/N"
    IF WARN
	  IF EXISTS "{EMU68DEVICE}{EMU68PATH}Backup_Emu68/Emu68-$PistormVariant_old"
	    DELETE "{EMU68DEVICE}{EMU68PATH}Backup_Emu68/Emu68-$PistormVariant_old" FORCE QUIET >NIL:
	  ENDIF
	  COPY "{EMU68DEVICE}{EMU68PATH}Emu68-$PistormVariant" TO "{EMU68DEVICE}{EMU68PATH}Backup_Emu68/Emu68-$PistormVariant_old" FORCE CLONE QUIET >NIL:
	  COPY "RAM:Emu68upd_temp/Emu68-$PistormVariant" TO "{EMU68DEVICE}{EMU68PATH}Emu68-$PistormVariant" FORCE CLONE QUIET >NIL:
	  Set EMU68UPD 1
	  ECHO ""
	ELSE
	  ECHO ""
	  SKIP VC4_Update
	ENDIF
ELSE
  ECHO "Your version of Emu68 is already up to date."
  ECHO ""
  SKIP VC4_Update
ENDIF

LAB VC4_Update

IF NOT EXISTS "LIBS:Picasso96/Backup_Videocore"
  MAKEDIR "LIBS:Picasso96/Backup_Videocore" >NIL:
ENDIF

c:AreWeOnline
IF WARN
  ECHO "System is currently offline, please enable your internet connection then try again."
  SKIP END
ENDIF

ECHO "Downloading Emu68-tools JSON file..."
ECHO ""

c:aget https://api.github.com/repos/michalsc/Emu68-tools/releases TO RAM:releases.json >NIL:
IF WARN
   ECHO "Unable to connect to Github, waiting 5 seconds before retrying..."
   WAIT 5
   ECHO ""
   ECHO "Second attempt at downloading Emu68-Tools JSON file..."
   ECHO ""
   c:aget https://api.github.com/repos/michalsc/Emu68-tools/releases TO RAM:releases.json >NIL:
   IF WARN
     ECHO "It seems the system is currently unable to connect to Github, please try again later."
	 SKIP END
   ENDIF
ENDIF

RX Emu68-Updater:tag.rexx >NIL:

ECHO "Downloading latest Emu68-tools release..."
ECHO ""

c:aget https://github.com/michalsc/Emu68-tools/releases/download/$TagENV/Emu68-tools.zip TO RAM:Emu68-tools.zip >NIL:
IF WARN
   ECHO "Unable to connect to Github, waiting 5 seconds before retrying..."
   WAIT 5
   ECHO ""
   ECHO "Second attempt at downloading latest Emu68-Tools release..."
   ECHO ""
   c:aget https://github.com/michalsc/Emu68-tools/releases/download/$TagENV/Emu68-tools.zip TO RAM:Emu68-tools.zip >NIL:
   IF WARN
     ECHO "It seems the system is currently unable to connect to Github, please try again later."
	 SKIP END
   ENDIF
ENDIF

ECHO "Unpacking..."
ECHO ""

c:UnZip -o RAM:Emu68-tools.zip -d RAM:Emu68tools_temp >NIL:

ECHO "Starting VideoCore update process..."
ECHO ""

SET OldVC4Version `Version LIBS:Picasso96/VideoCore.card`
SET NewVC4Version `Version RAM:Emu68tools_temp/VideoCore/VideoCore.card`

;IF "$OldVC4Version" EQ "$NewVC4Version"
;  ECHO "Your version of VideoCore is already up to date."
;  SKIP END
;ENDIF

ECHO "Version on your Amiga: $OldVC4Version "
ECHO "Version found online: $NewVC4Version "
RX Emu68-Updater:VersionChecker.rexx "$OldVC4Version" "$NewVC4Version" >NIL:

IF NOT WARN
  ASK "New $NewVC4Version version found, do you want to update your current $OldVC4Version version? Y/N"
  ;ASK "Do you want to update to the latest version of VideoCore? Y/N"
    IF WARN
	  IF EXISTS "LIBS:Picasso96/Backup_Videocore/VideoCore.card_old"
	    DELETE "LIBS:Picasso96/Backup_Videocore/VideoCore.card_old" FORCE QUIET >NIL:
	  ENDIF
	  COPY "LIBS:Picasso96/VideoCore.card" TO "LIBS:Picasso96/Backup_Videocore/VideoCore.card_old" FORCE CLONE QUIET >NIL:
	  COPY "RAM:Emu68tools_temp/VideoCore/VideoCore.card" TO "LIBS:Picasso96/VideoCore.card" FORCE CLONE QUIET >NIL:
	  ; VC4_LEGACY_ID is only needed for Aminet shareware rtg.library (v40).
	  ; iComp/modern rtg.library (v41+) uses a different board registration
	  ; API and VC4_LEGACY_ID causes incorrect mode registration.
	  Version LIBS:Picasso96/rtg.library VERSION 41 >NIL:
	  IF WARN
	    ; Shareware rtg.library v40 — ensure VC4_LEGACY_ID is present
	    Emu68-Updater:TTToFile SYS:Devs/Monitors/VideoCore.info TO RAM:T/VC4TT
	    Search RAM:VC4TT "VC4_LEGACY_ID" >NIL:
	    IF WARN
	      ECHO "VC4_LEGACY_ID" >> RAM:T/VC4TT
	      Emu68-Updater:FileToTT RAM:T/VC4TT TO SYS:Devs/Monitors/VideoCore.info
	    ENDIF
	    DELETE RAM:T/VC4TT FORCE QUIET >NIL:
	  ENDIF
	  Set VC4UPD 1
	ELSE
	  SKIP END
	ENDIF
ELSE
  ECHO "Your version of VideoCore is already up to date."
  SKIP END
ENDIF

LAB END

IF $VC4UPD EQ 1
  ECHO ""
  ASK "Update completed successfully! A Hard Reset is needed for the changes to take effect, do you want to do it now? Y/N"
    IF WARN
	  ECHO ""
	  ECHO "Rebooting..."
	  WAIT 3
      c:EMU68INFO HARDRESET
    ELSE
      ECHO ""
      ECHO "This window will close in 3 seconds."
	  SKIP Cleanup
    ENDIF
ELSE
  IF $EMU68UPD EQ 1
    ECHO ""
    ASK "Update completed successfully! A Hard Reset is needed for the changes to take effect, do you want to do it now? Y/N"
      IF WARN
	    ECHO ""
	    ECHO "Rebooting..."
	    WAIT 3
        c:EMU68INFO HARDRESET
      ELSE
        ECHO ""
        ECHO "This window will close in 3 seconds."
	    SKIP Cleanup
      ENDIF
  ELSE
    ECHO ""
    ECHO "This window will close in 3 seconds."
	SKIP Cleanup
  ENDIF
ENDIF

LAB Cleanup

DELETE RAM:releases.json FORCE QUIET >NIL:
DELETE RAM:Emu68-$PistormVariant.zip FORCE QUIET >NIL:
DELETE RAM:Emu68-tools.zip FORCE QUIET >NIL:
DELETE RAM:Emu68upd_temp/ ALL FORCE QUIET >NIL:
DELETE RAM:Emu68tools_temp/ ALL FORCE QUIET >NIL:

UNSET OldEmu68Version
UNSET NewEmu68Version
UNSET OldVC4Version
UNSET NewVC4Version
UNSET VC4UPD
UNSET EMU68UPD
UNSETENV TagENV

WAIT 3

ASSIGN Emu68-Updater: REMOVE

FAILAT 10

ENDCLI
