#!/usr/bin/env bash


# Stop the screen reader if this closes for any reason
trap 'sudo -n /usr/bin/systemctl stop fenrirscreenreader.service' SIGINT SIGTERM SIGHUP EXIT

# Start Fenrir for interaction with the terminal
sudo -n /usr/bin/systemctl start fenrirscreenreader.service

# Clear the screen before loading
clear

case "$GAME" in
    "Alter Aeon")
        pushd ~/.local/games/tintin-alteraeon
        git pull -q
        tt++ aa.tin
        popd
    ;;
    "BPG")
        pushd ~/.local/games/bpg
        git pull -q
        ./bpg.sh
        popd
    ;;
    "Empire MUD")
        pushd ~/.local/games/tintin-empiremud
        git pull -q
        tt++ em.tin
        popd
    ;;
    "End of Time")
        pushd ~/.local/games/tintin-endoftime
        git pull -q
        tt++ eot.tin
        popd
    ;;
    "Kallisti MUD")
        pushd ~/.local/games/tintin-kallisti-pack
        git pull -q
        tt++ kallisti.tin
        popd
    ;;
    "RS Games")
        pushd ~/.local/games/tintin-rsgames
        git pull -q
        tt++ rs.tin
        popd
    ;;
    "Slay the Text")
        pushd ~/.local/games/slaythetext
        git pull -q
        python3 main.py
        popd
    ;;
    "Stationfall")
        frotz ~/.local/frotz/Stationfall/stationfall-r107-s870430.z3
    ;;
    "Planetfall")
        frotz ~/.local/frotz/Planetfall/planetfall-r39-s880501.z3
    ;;
    "The Hitchhiker's Guide to the Galaxy")
        frotz ~/.local/frotz/"The Hitchhiker's Guide to the Galaxy/HITCHHIK.DAT"
    ;;
    "Zork 1") ;&
    "Zork 2") ;&
    "Zork 3")
        frotz ~/.local/frotz/${GAME/ /}/DATA/ZORK${GAME##* }.DAT
    ;;
    "IRC")
        if ! ping -c1 stormux.org &> /dev/null ; then
            spd-say -Cw "No internet connection detected. Please go to the system menu and select configure internet."
            exit 0
        fi
        configFile="$HOME/.irssi/config"
        validNickRegex='^[[:alnum:]_-]*$'
        # Check if nick and user_name are still set to "stormux"
        if grep -qE '^\s*nick = "stormux";' "$configFile" && grep -qE '^\s*user_name = "stormux";' "$configFile"; then
            echo "First time connection setup:"
        echo
            echo "Let's get you set up with your own nick."
            echo "This is the name other people will see when you join, chat, leave, etc."
        echo
            while : ; do
                echo "Enter your desired IRC nick:"
                read -re newNick
                if [[ $newNick =~ $validNickRegex ]]; then
                    # Update nick and user_name in config
                    sed -i "s/^\(\s*nick = \)\"stormux\";/\1\"$newNick\";/" "$configFile"
                    sed -i "s/^\(\s*user_name = \)\"stormux\";/\1\"$newNick\";/" "$configFile"
                    echo "Configuration updated. Launching irssi..."
                    break
                else
                    echo "Invalid IRC nickname. Must start with a letter and contain only letters, numbers, and these symbols: - [ ] \\ \` ^ { } _"
                fi
            done
        fi
        # Launch irssi
        /usr/bin/irssi
    ;;
    "Network Configuration")
        count=0
        while [[ $count -lt 10 ]]; do
            sleep 1
            if echo "setting set focus#highlight=True" | socat - UNIX-CLIENT:/tmp/fenrirscreenreader-deamon.sock 2> /dev/null ; then
                break
            fi
        done
        nmtui-connect
    ;;
    "Install to Disk")
        /usr/local/bin/install_to_disk.sh
    ;;
    "Set Timezone")
        sudo /usr/local/bin/set-timezone.sh
    ;;
    *".md") /usr/bin/markdown -toc "$GAME" | /usr/bin/w3m -T text/html ;;
    "/usr/bin/"*) $GAME ;;
esac
