#!/usr/bin/env bash
if [[ "$LD_PRELOAD" =~ "gameoverlayrenderer" ]]
then
    LD_PRELOAD=""
fi
[[ $0 == $BASH_SOURCE ]] && cd "$( dirname "$0")"
scriptDir="$(cd "$( dirname "$BASH_SOURCE")" && pwd)"
[ $# -gt 0 ] && parameterone="$1"
[ $# -gt 1 ] && parametertwo="$2"
[ $# -gt 2 ] && parameterthree="$3"
[ $# -gt 3 ] && parameterfour="$4"

if [[ "$OSTYPE" == "darwin"* ]]
then
    source "$scriptDir/$(basename -- "${BASH_SOURCE%.bsh}.msh")"
    exit 0
fi

if [ "${BASH_VERSINFO:-0}" -lt 5 ]
then
    printf "\n\e[1;31;47mThe version of bash currently running is too old.\e[0m\n\n"
    printf "Please run the \e[1;33;40minstall_dependencies.command\e[0m script.\n"
    printf "Then, follow the instructions to install the required dependencies.\n"
    read -s -n 1 -p "Press any key to abort."
    printf "\n\n"
    exit 0
fi

function goto
{
    shortcutName=$1
    newPosition=$(sed -n -e "/: $shortcutName$/{:a;n;p;ba};" "$scriptDir/$(basename -- "$BASH_SOURCE")" )
    eval "$newPosition"
    exit
}
alias :="goto"

function dynchoice
{
    local choices="$1"
    local textpmt="$2"
    local numofchoices="${#choices}"
    local choicesu="${choices^^}"
    local choicesl="${choices,,}"
    if [ "$numofchoices" -eq 10 ]
    then
        while true
        do
            read -p "$textpmt " choice
            case $choice in
                [${choicesu:0:1}${choicesl:0:1}]* ) errorlevel=1
                        break;;
                [${choicesu:1:1}${choicesl:1:1}]* ) errorlevel=2
                        break;;
                [${choicesu:2:1}${choicesl:2:1}]* ) errorlevel=3
                        break;;
                [${choicesu:3:1}${choicesl:3:1}]* ) errorlevel=4
                        break;;
                [${choicesu:4:1}${choicesl:4:1}]* ) errorlevel=5
                        break;;
                [${choicesu:5:1}${choicesl:5:1}]* ) errorlevel=6
                        break;;
                [${choicesu:6:1}${choicesl:6:1}]* ) errorlevel=7
                        break;;
                [${choicesu:7:1}${choicesl:7:1}]* ) errorlevel=8
                        break;;
                [${choicesu:8:1}${choicesl:8:1}]* ) errorlevel=9
                        break;;
                [${choicesu:9:1}${choicesl:9:1}]* ) errorlevel=10
                        break;;
                *     ) printf "Invalid input.\n";;
            esac
        done
    elif [ "$numofchoices" -eq 9 ]
    then
        while true
        do
            read -p "$textpmt " choice
            case $choice in
                [${choicesu:0:1}${choicesl:0:1}]* ) errorlevel=1
                        break;;
                [${choicesu:1:1}${choicesl:1:1}]* ) errorlevel=2
                        break;;
                [${choicesu:2:1}${choicesl:2:1}]* ) errorlevel=3
                        break;;
                [${choicesu:3:1}${choicesl:3:1}]* ) errorlevel=4
                        break;;
                [${choicesu:4:1}${choicesl:4:1}]* ) errorlevel=5
                        break;;
                [${choicesu:5:1}${choicesl:5:1}]* ) errorlevel=6
                        break;;
                [${choicesu:6:1}${choicesl:6:1}]* ) errorlevel=7
                        break;;
                [${choicesu:7:1}${choicesl:7:1}]* ) errorlevel=8
                        break;;
                [${choicesu:8:1}${choicesl:8:1}]* ) errorlevel=9
                        break;;
                *     ) printf "Invalid input.\n";;
            esac
        done
    elif [ "$numofchoices" -eq 8 ]
    then
        while true
        do
            read -p "$textpmt " choice
            case $choice in
                [${choicesu:0:1}${choicesl:0:1}]* ) errorlevel=1
                        break;;
                [${choicesu:1:1}${choicesl:1:1}]* ) errorlevel=2
                        break;;
                [${choicesu:2:1}${choicesl:2:1}]* ) errorlevel=3
                        break;;
                [${choicesu:3:1}${choicesl:3:1}]* ) errorlevel=4
                        break;;
                [${choicesu:4:1}${choicesl:4:1}]* ) errorlevel=5
                        break;;
                [${choicesu:5:1}${choicesl:5:1}]* ) errorlevel=6
                        break;;
                [${choicesu:6:1}${choicesl:6:1}]* ) errorlevel=7
                        break;;
                [${choicesu:7:1}${choicesl:7:1}]* ) errorlevel=8
                        break;;
                *     ) printf "Invalid input.\n";;
            esac
        done
    elif [ "$numofchoices" -eq 7 ]
    then
        while true
        do
            read -p "$textpmt " choice
            case $choice in
                [${choicesu:0:1}${choicesl:0:1}]* ) errorlevel=1
                        break;;
                [${choicesu:1:1}${choicesl:1:1}]* ) errorlevel=2
                        break;;
                [${choicesu:2:1}${choicesl:2:1}]* ) errorlevel=3
                        break;;
                [${choicesu:3:1}${choicesl:3:1}]* ) errorlevel=4
                        break;;
                [${choicesu:4:1}${choicesl:4:1}]* ) errorlevel=5
                        break;;
                [${choicesu:5:1}${choicesl:5:1}]* ) errorlevel=6
                        break;;
                [${choicesu:6:1}${choicesl:6:1}]* ) errorlevel=7
                        break;;
                *     ) printf "Invalid input.\n";;
            esac
        done
    elif [ "$numofchoices" -eq 6 ]
    then
        while true
        do
            read -p "$textpmt " choice
            case $choice in
                [${choicesu:0:1}${choicesl:0:1}]* ) errorlevel=1
                        break;;
                [${choicesu:1:1}${choicesl:1:1}]* ) errorlevel=2
                        break;;
                [${choicesu:2:1}${choicesl:2:1}]* ) errorlevel=3
                        break;;
                [${choicesu:3:1}${choicesl:3:1}]* ) errorlevel=4
                        break;;
                [${choicesu:4:1}${choicesl:4:1}]* ) errorlevel=5
                        break;;
                [${choicesu:5:1}${choicesl:5:1}]* ) errorlevel=6
                        break;;
                *     ) printf "Invalid input.\n";;
            esac
        done
    elif [ "$numofchoices" -eq 5 ]
    then
        while true
        do
            read -p "$textpmt " choice
            case $choice in
                [${choicesu:0:1}${choicesl:0:1}]* ) errorlevel=1
                        break;;
                [${choicesu:1:1}${choicesl:1:1}]* ) errorlevel=2
                        break;;
                [${choicesu:2:1}${choicesl:2:1}]* ) errorlevel=3
                        break;;
                [${choicesu:3:1}${choicesl:3:1}]* ) errorlevel=4
                        break;;
                [${choicesu:4:1}${choicesl:4:1}]* ) errorlevel=5
                        break;;
                *     ) printf "Invalid input.\n";;
            esac
        done
    elif [ "$numofchoices" -eq 4 ]
    then
        while true
        do
            read -p "$textpmt " choice
            case $choice in
                [${choicesu:0:1}${choicesl:0:1}]* ) errorlevel=1
                        break;;
                [${choicesu:1:1}${choicesl:1:1}]* ) errorlevel=2
                        break;;
                [${choicesu:2:1}${choicesl:2:1}]* ) errorlevel=3
                        break;;
                [${choicesu:3:1}${choicesl:3:1}]* ) errorlevel=4
                        break;;
                *     ) printf "Invalid input.\n";;
            esac
        done
    elif [ "$numofchoices" -eq 3 ]
    then
        while true
        do
            read -p "$textpmt " choice
            case $choice in
                [${choicesu:0:1}${choicesl:0:1}]* ) errorlevel=1
                        break;;
                [${choicesu:1:1}${choicesl:1:1}]* ) errorlevel=2
                        break;;
                [${choicesu:2:1}${choicesl:2:1}]* ) errorlevel=3
                        break;;
                *     ) printf "Invalid input.\n";;
            esac
        done
    elif [ "$numofchoices" -eq 2 ]
    then
        while true
        do
            read -p "$textpmt " choice
            case $choice in
                [${choicesu:0:1}${choicesl:0:1}]* ) errorlevel=1
                        break;;
                [${choicesu:1:1}${choicesl:1:1}]* ) errorlevel=2
                        break;;
                *     ) printf "Invalid input.\n";;
            esac
        done
    else
        printf "\n\e[1;31;47mError in dynamic case statement\041\041\041\e[0m"
        printf "\n\e[1;31;47mPlease report this to the team.\e[0m\n\n"
        read -s -n 1 -p "Press any key to abort."
        printf "\n\n"
        exit 0
    fi
    return
}

depcheck=flatpak
missingDependencies=no
if [ $depcheck == "flatpak" ]
then
    ! [[ `which flatpak` ]] && missingDependencies=yes
    ! [[ `flatpak list 2>/dev/null | grep "retro_exo.vlc"` ]] && missingDependencies=yes
elif [ $depcheck == "native" ]
then
    ! [[ `which dosbox-074r3-1` ]] && missingDependencies=yes
    ! [[ `which dosbox-ece-r4301` ]] && missingDependencies=yes
    ! [[ `which dosbox-ece-r4358` ]] && missingDependencies=yes
    ! [[ `which dosbox-ece-r4482` ]] && missingDependencies=yes
    ! [[ `which dosbox-gridc-4-3-1` ]] && missingDependencies=yes
    ! [[ `which dosbox-staging-082-0` ]] && missingDependencies=yes
    ! [[ `which dosbox-staging-081-2` ]] && missingDependencies=yes
    ! [[ `which dosbox-x-08220` ]] && missingDependencies=yes
    ! [[ `which dosbox-x-20240701` ]] && missingDependencies=yes
    ! [[ `which gzdoom-4-11-3` ]] && missingDependencies=yes
    ! [[ `which scummvm-2-2-0` ]] && missingDependencies=yes
    ! [[ `which scummvm-2-3-0-git15811-gf97bfb7ce1` ]] && missingDependencies=yes
    ! [[ `which aria2c` ]] && missingDependencies=yes
    ! [[ `which vlc` ]] && missingDependencies=yes
    ! [[ `which wine` ]] && missingDependencies=yes
else
    missingDependencies=yes
fi
! [[ `which curl` ]] && missingDependencies=yes
! [[ `which python3` ]] && missingDependencies=yes
! [[ `which sed` ]] && missingDependencies=yes
! [[ `which unzip` ]] && missingDependencies=yes
! [[ `which wget` ]] && missingDependencies=yes

if [ $missingDependencies == "yes" ]
then
    printf "\n\e[1;31;47mOne or more dependencies are missing.\e[0m\n\n"
    printf "Please run the \e[1;33;40minstall_dependencies.command\e[0m script.\n"
    printf "Then, follow the instructions to install the required dependencies.\n"
    read -s -n 1 -p "Press any key to abort."
    printf "\n\n"
    exit 0
fi

var="${PWD}"
: main
clear
echo ""
echo "          G4: Icons"
echo ""
echo "Press 1 to goto Season 1"
echo "Press 2 to goto Season 2"
echo "Press 3 to goto Season 3"
echo "Press 4 to goto Season 4"
echo ""
echo "Selected episodes which contain content related to"
echo "MS-DOS and PC gaming during that era have been"
echo "included."
echo ""
while true
do
    read -p "Please Choose: " choice
    case $choice in
        [1] ) errorlevel=1
                break;;
        [2] ) errorlevel=2
                break;;
        [3] ) errorlevel=3
                break;;
        [4] ) errorlevel=4
                break;;
        *     ) printf "Invalid input.\n";;
    esac
done

[ $errorlevel == '4' ] && goto s4 && [[ $0 != $BASH_SOURCE ]] && return
[ $errorlevel == '3' ] && goto s3 && [[ $0 != $BASH_SOURCE ]] && return
[ $errorlevel == '2' ] && goto s2 && [[ $0 != $BASH_SOURCE ]] && return
[ $errorlevel == '1' ] && goto s1 && [[ $0 != $BASH_SOURCE ]] && return

: s1
clear
echo ""
echo "          Season One"
echo ""
echo "Press 1 for Episode 09: Will Wright"
echo "Press 2 for Episode 12: Sid Meier"
echo "Press 3 for Episode 14: Richard Garriot"
echo "Press 4 for Episode 15: Bruce Shelley"
echo "Press 5 to Go Back"
echo ""
while true
do
    read -p "Please Choose: " choice
    case $choice in
        [1] ) errorlevel=1
                break;;
        [2] ) errorlevel=2
                break;;
        [3] ) errorlevel=3
                break;;
        [4] ) errorlevel=4
                break;;
        [5] ) errorlevel=5
                break;;
        *     ) printf "Invalid input.\n";;
    esac
done

[ $errorlevel == '5' ] && goto main && [[ $0 != $BASH_SOURCE ]] && return
[ $errorlevel == '4' ] && goto ep15 && [[ $0 != $BASH_SOURCE ]] && return
[ $errorlevel == '3' ] && goto ep14 && [[ $0 != $BASH_SOURCE ]] && return
[ $errorlevel == '2' ] && goto ep12 && [[ $0 != $BASH_SOURCE ]] && return
[ $errorlevel == '1' ] && goto ep09 && [[ $0 != $BASH_SOURCE ]] && return

: s2
clear
echo ""
echo "          Season Two"
echo ""
echo "Press 1 for Episode 28: LucasArts"
echo "Press 2 for Episode 30: Warren Spector"
echo "Press 3 for Episode 31: PC Gaming"
echo "Press 4 to Go Back"
echo ""
while true
do
    read -p "Please Choose: " choice
    case $choice in
        [1] ) errorlevel=1
                break;;
        [2] ) errorlevel=2
                break;;
        [3] ) errorlevel=3
                break;;
        [4] ) errorlevel=4
                break;;
        *     ) printf "Invalid input.\n";;
    esac
done

[ $errorlevel == '4' ] && goto main && [[ $0 != $BASH_SOURCE ]] && return
[ $errorlevel == '3' ] && goto ep31 && [[ $0 != $BASH_SOURCE ]] && return
[ $errorlevel == '2' ] && goto ep30 && [[ $0 != $BASH_SOURCE ]] && return
[ $errorlevel == '1' ] && goto ep28 && [[ $0 != $BASH_SOURCE ]] && return

: s3
clear
echo ""
echo "          Season Three"
echo ""
echo "Press 1 for Episode 43: Peter Molyneux"
echo "Press 2 for Episode 45: Electronic Arts"
echo "Press 3 for Episode 49: DOOM"
echo "Press 4 for Episode 53: Prince of Persia"
echo "Press 5 to Go Back"
echo ""
while true
do
    read -p "Please Choose: " choice
    case $choice in
        [1] ) errorlevel=1
                break;;
        [2] ) errorlevel=2
                break;;
        [3] ) errorlevel=3
                break;;
        [4] ) errorlevel=4
                break;;
        [5] ) errorlevel=5
                break;;
        *     ) printf "Invalid input.\n";;
    esac
done

[ $errorlevel == '5' ] && goto main && [[ $0 != $BASH_SOURCE ]] && return
[ $errorlevel == '4' ] && goto ep53 && [[ $0 != $BASH_SOURCE ]] && return
[ $errorlevel == '3' ] && goto ep49 && [[ $0 != $BASH_SOURCE ]] && return
[ $errorlevel == '2' ] && goto ep45 && [[ $0 != $BASH_SOURCE ]] && return
[ $errorlevel == '1' ] && goto ep43 && [[ $0 != $BASH_SOURCE ]] && return

: s4
clear
echo ""
echo "          Season Four"
echo ""
echo "Press 1 for Episode 63: Tim Schaffer"
echo "Press 2 to Go Back"
echo ""
while true
do
    read -p "Please Choose: " choice
    case $choice in
        [1] ) errorlevel=1
                break;;
        [2] ) errorlevel=2
                break;;
        *     ) printf "Invalid input.\n";;
    esac
done

[ $errorlevel == '2' ] && goto main && [[ $0 != $BASH_SOURCE ]] && return
[ $errorlevel == '1' ] && goto ep63 && [[ $0 != $BASH_SOURCE ]] && return

: ep09
cd ../../..
flatpak run com.retro_exo.vlc --width="1920" "${var}/G4 Icons Episode 09 Will Wright.mp4"
goto quit && [[ $0 != $BASH_SOURCE ]] && return

: ep12
cd ../../..
flatpak run com.retro_exo.vlc --width="1920" "${var}/G4 Icons Episode 12 Sid Meier.mp4"
goto quit && [[ $0 != $BASH_SOURCE ]] && return

: ep14
cd ../../..
flatpak run com.retro_exo.vlc --width="1920" "${var}/G4 Icons Episode 14 Richard Garriott.mp4"
goto quit && [[ $0 != $BASH_SOURCE ]] && return

: ep15
cd ../../..
flatpak run com.retro_exo.vlc --width="1920" "${var}/G4 Icons Episode 15 Bruce Shelley.mp4"
goto quit && [[ $0 != $BASH_SOURCE ]] && return

: ep28
cd ../../..
flatpak run com.retro_exo.vlc --width="1920" "${var}/G4 Icons Episode 28 LucasArts.mp4"
goto quit && [[ $0 != $BASH_SOURCE ]] && return

: ep30
cd ../../..
flatpak run com.retro_exo.vlc --width="1920" "${var}/G4 Icons Episode 30 Warren Spector.mp4"
goto quit && [[ $0 != $BASH_SOURCE ]] && return

: ep31
cd ../../..
flatpak run com.retro_exo.vlc --width="1920" "${var}/G4 Icons Episode 31 PC Gaming.mp4"
goto quit && [[ $0 != $BASH_SOURCE ]] && return

: ep43
cd ../../..
flatpak run com.retro_exo.vlc --width="1920" "${var}/G4 Icons Episode 43 Peter Molyneux.mp4"
goto quit && [[ $0 != $BASH_SOURCE ]] && return

: ep45
cd ../../..
flatpak run com.retro_exo.vlc --width="1920" "${var}/G4 Icons Episode 45 Electronic Arts.mp4"
goto quit && [[ $0 != $BASH_SOURCE ]] && return

: ep49
cd ../../..
flatpak run com.retro_exo.vlc --width="1920" "${var}/G4 Icons Episode 49 Doom.mp4"
goto quit && [[ $0 != $BASH_SOURCE ]] && return

: ep53
cd ../../..
flatpak run com.retro_exo.vlc --width="1920" "${var}/G4 Icons Episode 53 Prince of Persia.mp4"
goto quit && [[ $0 != $BASH_SOURCE ]] && return

: ep63
cd ../../..
flatpak run com.retro_exo.vlc --width="1920" "${var}/G4 Icons Episode 63 Tim Shafer (2005).mp4"
goto quit && [[ $0 != $BASH_SOURCE ]] && return


: quit
[[ $0 != $BASH_SOURCE ]] && return
