#!/usr/bin/env bash

# Function to clean up on exit
cleanup() {
    sudo systemctl stop fenrirscreenreader.service 2>/dev/null || true
}

# Set up trap for cleanup
trap cleanup EXIT

if [[ -r "/home/stormux/.local/games/Upheaval/Upheaval_Command_Line" ]]; then
    pushd "/home/stormux/.local/games/Upheaval/" > /dev/null
    
    # Comprehensive terminal reset
    reset
    clear
    stty sane
    stty echo
    stty icanon
    stty -raw
    stty -cbreak
    
    # Start screen reader
    sudo systemctl start fenrirscreenreader.service
    
    # Give screen reader time to initialize
    sleep 1
    
    # Run the game with proper terminal handling
    # Use exec to replace the shell process entirely
    exec stdbuf -i0 -o0 -e0 FEXLoader ./Upheaval_Command_Line 2>&1 | grep -v '^vc4: driver missing$'
    popd > /dev/null
fi
