IF EQU "$DISK_TYPE$" ""
    GETDISKTYPE "DISK_TYPE"
ENDIF

IF EQU "$MEDIA_TYPE$" ""
    IF EQU "$DISK_TYPE$" "PS1_CD"
        SET "MEDIA_TYPE" "CD"
    ELSEIF EQU "$DISK_TYPE$" "PS2_CD"
        SET "MEDIA_TYPE" "CD"
    ELSEIF EQU "$DISK_TYPE$" "CDDA"
        SET "MEDIA_TYPE" "CD"
    ELSEIF EQU "$DISK_TYPE$" "PS2_DVD"
        SET "MEDIA_TYPE" "DVD"
    ELSEIF EQU "$DISK_TYPE$" "DVDV"
        SET "MEDIA_TYPE" "DVD"
    ELSE
        ECHO "Unknown disk type: $DISK_TYPE$!"
        RETURN "-1"
    ENDIF
ENDIF

IF EQU "$DISK_CONTENT$" ""
    IFFAIL PARSECNF "cdrom0:\SYSTEM.CNF" "BOOT_TYPE" "BOOT_NAME"
        IF EQU "$MEDIA_TYPE$" "CD"
            IF EQU "$DISK_TYPE$" "CDDA"
                SET "DISK_CONTENT" "CDDA"
            ELSEIF EXISTS "cdrom0:\PSX.EXE"
                SET "DISK_CONTENT" "PS1"
                SET "BOOT_TYPE" "PS1"
                SET "BOOT_NAME" "SLUS12345"
            ELSEIF EXISTS "cdrom0:\PS2.ELF"
                SET "DISK_CONTENT" "PS2"
                SET "BOOT_TYPE" "PS2"
                SET "BOOT_NAME" "cdrom0:\PS2.ELF"
            ELSE
                ECHO "Unknown CD content!"
                RETURN "-1"
            ENDIF
        ELSE
            IF EXISTS "cdrom0:\VIDEO_TS"
                SET "DISK_CONTENT" "DVDV"
            ELSE
                ECHO "Unknown DVD content!"
                RETURN "-1"
            ENDIF
        ENDIF
    ELSE
        IF EQU "$MEDIA_TYPE$" "CD"
            IF EQU "$BOOT_TYPE$" "PS1"
                SET "DISK_CONTENT" "PS1"
            ELSEIF EQU "$BOOT_TYPE$" "PS2"
                SET "DISK_CONTENT" "PS2"
            ELSE
                ECHO "Unknown CD content!"
                RETURN "-1"
            ENDIF
        ELSE
            IF EQU "$BOOT_TYPE$" "PS2"
                SET "DISK_CONTENT" "PS2"
            ELSE
                ECHO "Unknown DVD content!"
                RETURN "-1"
            ENDIF
        ENDIF
    ENDIF
ENDIF

IF EQU "$ARG1$" "BOOT"
    IF EQU "$DISK_CONTENT$" "PS1"
        GOTO "boot_PS1"
    ELSEIF EQU "$DISK_CONTENT$" "PS2"
        GOTO "boot_PS2"
    ELSEIF EQU "$DISK_CONTENT$" "DVDV"
        GOTO "boot_DVDV"
    ELSEIF EQU "$DISK_CONTENT$" "CDDA"
        GOTO "boot_CDDA"
    ELSE
        ECHO "Unknown disk content: $DISK_CONTENT$!"
        RETURN "-1"
    ENDIF
ELSEIF EQU "$ARG1$" "MENU"
    IF EQU "$DISK_CONTENT$" "PS1"
        GOTO "menu_PS1"
    ELSEIF EQU "$DISK_CONTENT$" "PS2"
        GOTO "menu_PS2"
    ELSEIF EQU "$DISK_CONTENT$" "DVDV"
        GOTO "menu_DVDV"
    ELSEIF EQU "$DISK_CONTENT$" "CDDA"
        GOTO "menu_CDDA"
    ELSE
        ECHO "Unknown disk content: $DISK_CONTENT$!"
        RETURN "-1"
    ENDIF
ELSE
    GOTO "$ARG1$"
    RETURN "-1"
ENDIF

###############################################################################
###############################################################################

###############################################################################
#                           Audio CD bootstrap                                #
###############################################################################

:boot_CDDA
:menu_CDDA
ECHO "Booting Audio CD..."

# Default to using Browser to play CD Audio
LOADEXEC "EEELF" "rom0:OSDSYS" "BootOpening"

###############################################################################
###############################################################################

###############################################################################
#                           DVD Video bootstrap                               #
###############################################################################

:boot_DVDV
:menu_DVDV

ECHO "Booting DVD Video disc..."

# Disable disc auth and cycle the tray if disc isn't already authenticated as DVD Video
IF NEQ "$DISK_TYPE$" "DVDV"
    SETAUTH "OFF"
    CYCLETRAY "WAIT"
ENDIF

# Check both memory card slots for official DVD player upgrade
IF EXISTS "mc0:/BAEXEC-DVDPLAYER/dvdplayer.elf"
    SET "MC_ELF" "mc0:/BAEXEC-DVDPLAYER/dvdplayer.elf"

ELSEIF EXISTS "mc0:/BEEXEC-DVDPLAYER/dvdplayer.elf"
    SET "MC_ELF" "mc0:/BEEXEC-DVDPLAYER/dvdplayer.elf"

ELSEIF EXISTS "mc0:/BIEXEC-DVDPLAYER/dvdplayer.elf"
    SET "MC_ELF" "mc0:/BIEXEC-DVDPLAYER/dvdplayer.elf"

ELSEIF EXISTS "mc1:/BAEXEC-DVDPLAYER/dvdplayer.elf"
    SET "MC_ELF" "mc1:/BAEXEC-DVDPLAYER/dvdplayer.elf"

ELSEIF EXISTS "mc1:/BEEXEC-DVDPLAYER/dvdplayer.elf"
    SET "MC_ELF" "mc1:/BEEXEC-DVDPLAYER/dvdplayer.elf"

ELSEIF EXISTS "mc1:/BIEXEC-DVDPLAYER/dvdplayer.elf"
    SET "MC_ELF" "mc1:/BIEXEC-DVDPLAYER/dvdplayer.elf"

ELSE
    ECHO "Loading DVD player from ROM"

    POKEW "0x1000E010" "0x00000020"

    GETDVDREG "DVD_REGION"

    IF NEQ "$DVD_REGION$" ""
        ECHO "Loading on v12+ region $DVD_REGION$"
        LOADEXEC "EEELF" "moduleload2 rom1:UDNL rom1:DVDCNF" "-k rom1:EROMDRV$DVD_REGION$" "-m erom0:UDFIO" "-x erom0:DVDPL$DVD_REGION$"
    ELSE
        ECHO "Loading on v1-v11"
        LOADEXEC "EEELF" "moduleload2 rom1:UDNL rom1:DVDCNF" "-k rom1:EROMDRV" "-m erom0:UDFIO" "-x erom0:DVDELF"
    ENDIF
ENDIF

ECHO "DVD Player Upgrade found at $MC_ELF$, loading..."
LOADEXEC "EEELF" "moduleload" "-m rom0:SIO2MAN" "-m rom0:MCMAN" "-m rom0:MCSERV" "-x $MC_ELF$"

###############################################################################
###############################################################################

###############################################################################
#                           PS1 Disc bootstrap                                #
###############################################################################

:boot_PS1
:menu_PS1

IF ISIN "PSX.EXE" "$BOOT_NAME$"
    SET "IS_HOMEBREW" "1"
ELSE
    SET "IS_HOMEBREW" "1"
ENDIF

ECHO "Booting PS1 disc..."

# Enable PS1 BIOS patches
SETBIOS "PS1"

# Load the CC1.0 SRAM with PS1LOGO.BIN
IFFAIL LOADSRAM "$PWD$/PS1LOGO.BIN"
    RETURN "-1"
ENDIF

IF EQU "$IS_HOMEBREW$" "1"
    # Enable PS1 disc authentication for homebrew discs.
    SETAUTH "PS1"
ELSE
    # Disable PS1 disc authentication for non-homebrew discs.
    SETAUTH "OFF"
ENDIF

# cycle the tray if disc isn't already authenticated as PS1
IF NEQ "$DISK_TYPE$" "PS1_CD"
    CYCLETRAY "WAIT"
ENDIF

LOADEXEC "EEELF" "rom0:PS1DRV" "$BOOT_NAME$"

###############################################################################
###############################################################################

###############################################################################
#                           PS2 Disc bootstrap                                #
###############################################################################

:boot_PS2

SET "MODE" "$ARG2$"

IF ISIN "PS2.ELF" "$BOOT_NAME$"
    SET "IS_HOMEBREW" "1"
ELSE
    SET "IS_HOMEBREW" "1"
ENDIF

IF EQU "$MODE$" "SLEEP"
    ECHO "Booting PS2 disc in Sleep Mode..."

    # Disable disc authentication
    SETAUTH "OFF"

    # Shutdown CC1.0 hardware interface and ModuleManager software
    SHUTDOWN "ALL"

    # Cycle the tray to cause the disc to be rechecked for disc authentication.
    CYCLETRAY "WAIT"
ELSE
    IF EQU "$MODE$" "MINIMAL"
        # Shutdown ModuleManager software
        SHUTDOWN "MM"
    ENDIF

    ECHO "Booting PS2 disc with enhancements..."

    IF EQU "$IS_HOMEBREW$" "1"
        # Enable PS2 disc authentication for homebrew discs.
        SETAUTH "PS2"
    ELSE
        # Disable PS2 disc authentication for non-homebrew discs.
        SETAUTH "OFF"
        CYCLETRAY "WAIT"
    ENDIF

    # cycle the tray if disc isn't already authenticated as PS2
    IF NEQ "$DISK_TYPE$" "PS2_DVD"
        IF NEQ "$DISK_TYPE$" "PS2_CD"
            CYCLETRAY "WAIT"
        ENDIF
    ENDIF
ENDIF

ECHO "Executing $BOOT_NAME$..."

# Reset PS2 auth, improves performance when used with cheat devices.
SETAUTH "PS2"

# Execute the main executable of the disc.
LOADEXEC "EEELF" "$BOOT_NAME$;1"

###############################################################################
###############################################################################

###############################################################################
#                           PS2 Disc Boot Menu                                #
###############################################################################

:menu_PS2

# Set the menu title
SET "BM.MENU_TITLE" "PS2 Disc Boot Menu"

# CLEARMENU clears all items from the menu item list
CLEARMENU

# ADDMENUITEM adds an entry to the menu item list
ADDMENUITEM "Boot in Enhanced Mode" "Boot the PS2 disc with currently selected enhancement enabled." "boot_PS2"
ADDMENUITEM "Boot in Sleep Mode" "Boot the PS2 disc with CC1.0 and all enhancements disabled." "boot_PS2" "SLEEP"
ADDMENUITEM "Boot in Minimal Mode" "Boot the PS2 disc with minimal enhancements enabled." "boot_PS2" "MINIMAL"

RETURN "0"

###############################################################################
###############################################################################
