#!/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 "       Computer Chronicles"
echo "            Page One"
echo ""
echo "Press 1 for S01E21: Computer Games (1984)"
echo "Press 2 for S02E03: Computer Games (1985)"
echo "Press 3 for S02E04: IBM Compatible Computers (1985)"
echo "Press 4 for S03E08: Modems and Bulletin Boards (1986)"
echo "Press 5 for S04E03: MIDI Music (1987)"
echo "Press 6 for S05E08: Computer Games: Part 1 (1988)"
echo "Press 7 for S05E09: Computer Games: Part 2 (1988)"
echo "Press 8 for S05E16: Shareware (1988)"
echo "Press 9 for S08E11: Video Game Consoles (1990)"
echo "Press N to go to the Next Page"
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;;
        [5] ) errorlevel=5
                break;;
        [6] ) errorlevel=6
                break;;
        [7] ) errorlevel=7
                break;;
        [8] ) errorlevel=8
                break;;
        [9] ) errorlevel=9
                break;;
        [Nn]* ) errorlevel=10
                break;;
        *     ) printf "Invalid input.\n";;
    esac
done

[ $errorlevel == '10' ] && goto main2 && [[ $0 != $BASH_SOURCE ]] && return
if [ $errorlevel == '9' ]
then
                            title=S08E11
    			goto play && [[ $0 != $BASH_SOURCE ]] && return
fi
if [ $errorlevel == '8' ]
then
                            title=S05E16
    			goto play && [[ $0 != $BASH_SOURCE ]] && return
fi
if [ $errorlevel == '7' ]
then
                            title=S05E09
    			goto play && [[ $0 != $BASH_SOURCE ]] && return
fi
if [ $errorlevel == '6' ]
then
                            title=S05E08
    			goto play && [[ $0 != $BASH_SOURCE ]] && return
fi
if [ $errorlevel == '5' ]
then
                            title=S04E03
    			goto play && [[ $0 != $BASH_SOURCE ]] && return
fi
if [ $errorlevel == '4' ]
then
                            title=S03E08
    			goto play && [[ $0 != $BASH_SOURCE ]] && return
fi
if [ $errorlevel == '3' ]
then
                            title=S02E04
    			goto play && [[ $0 != $BASH_SOURCE ]] && return
fi
if [ $errorlevel == '2' ]
then
                            title=S02E03
    			goto play && [[ $0 != $BASH_SOURCE ]] && return
fi
if [ $errorlevel == '1' ]
then
                            title=S01E21
    			goto play && [[ $0 != $BASH_SOURCE ]] && return
fi

: main2
clear
echo ""
echo "       Computer Chronicles"
echo "            Page Two"
echo ""
echo "Press 1 for S08E21: CD-ROM Software (1991)"
echo "Press 2 for S08E23: Flight Simulators (1991)"
echo "Press 3 for S08E24: Consumer Electronics Show (1991)"
echo "Press 4 for S08E29: Software in Schools (1991)"
echo "Press 5 for S09E08: Computer Games (1991)"
echo "Press 6 for S09E17: Build Your Own PC (1992)"
echo "Press 7 for S09E22: MIDI Music (1992)"
echo "Press 8 for S09E25: CD-ROM Software (1992)"
echo "Press 9 for S10E22: CD-ROM Software (1993)"
echo "Press N to go to the Next Page"
echo "Press P to go to the Previous Page"
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;;
        [5] ) errorlevel=5
                break;;
        [6] ) errorlevel=6
                break;;
        [7] ) errorlevel=7
                break;;
        [8] ) errorlevel=8
                break;;
        [9] ) errorlevel=9
                break;;
        [Nn]* ) errorlevel=10
                break;;
        [Pp]* ) errorlevel=11
                break;;
        *     ) printf "Invalid input.\n";;
    esac
done

[ $errorlevel == '11' ] && goto main && [[ $0 != $BASH_SOURCE ]] && return
[ $errorlevel == '10' ] && goto main3 && [[ $0 != $BASH_SOURCE ]] && return
if [ $errorlevel == '9' ]
then
                            title=S10E22
    			goto play && [[ $0 != $BASH_SOURCE ]] && return
fi
if [ $errorlevel == '8' ]
then
                            title=S09E25
    			goto play && [[ $0 != $BASH_SOURCE ]] && return
fi
if [ $errorlevel == '7' ]
then
                            title=S09E22
    			goto play && [[ $0 != $BASH_SOURCE ]] && return
fi
if [ $errorlevel == '6' ]
then
                            title=S09E17
    			goto play && [[ $0 != $BASH_SOURCE ]] && return
fi
if [ $errorlevel == '5' ]
then
                            title=S09E08
    			goto play && [[ $0 != $BASH_SOURCE ]] && return
fi
if [ $errorlevel == '4' ]
then
                            title=S08E29
    			goto play && [[ $0 != $BASH_SOURCE ]] && return
fi
if [ $errorlevel == '3' ]
then
                            title=S08E24
    			goto play && [[ $0 != $BASH_SOURCE ]] && return
fi
if [ $errorlevel == '2' ]
then
                            title=S08E23
    			goto play && [[ $0 != $BASH_SOURCE ]] && return
fi
if [ $errorlevel == '1' ]
then
                            title=S08E21
    			goto play && [[ $0 != $BASH_SOURCE ]] && return
fi

: main3
clear
echo ""
echo "       Computer Chronicles"
echo "           Page Three"
echo ""
echo "Press 1 for S11E05: DOS 6.2 (1993)"
echo "Press 2 for S13E17: Computers and Kids (1996)"
echo "Press 3 for S13E39: E3 Special (1996)"
echo "Press 4 for S14E15: Greatest Games (1996)"
echo "Press 5 for S14E19: Sports Simulation Software (1996)"
echo "Press P to go to the Previous Page"
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;;
        [5] ) errorlevel=5
                break;;
        [Pp]* ) errorlevel=6
                break;;
        *     ) printf "Invalid input.\n";;
    esac
done

[ $errorlevel == '6' ] && goto main2 && [[ $0 != $BASH_SOURCE ]] && return
if [ $errorlevel == '5' ]
then
                            title=S14E19
    			goto play && [[ $0 != $BASH_SOURCE ]] && return
fi
if [ $errorlevel == '4' ]
then
                            title=S14E15
    			goto play && [[ $0 != $BASH_SOURCE ]] && return
fi
if [ $errorlevel == '3' ]
then
                            title=S13E39
    			goto play && [[ $0 != $BASH_SOURCE ]] && return
fi
if [ $errorlevel == '2' ]
then
                            title=S13E17
    			goto play && [[ $0 != $BASH_SOURCE ]] && return
fi
if [ $errorlevel == '1' ]
then
                            title=S11E05
    			goto play && [[ $0 != $BASH_SOURCE ]] && return
fi

: play
cd ../../..
flatpak run com.retro_exo.vlc --width="1920" "${var}/${title}.mp4"
goto quit && [[ $0 != $BASH_SOURCE ]] && return

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