#!/bin/bash

# Fix Steam Launch Options Override Issue
# Ensures STL configuration takes precedence over Steam launch options

set -e

GAME_ID="1144200"
STL_CONFIG="$HOME/.config/steamtinkerlaunch/gamecfgs/$GAME_ID.conf"

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m'

log_info() { echo -e "${BLUE}[INFO]${NC} $1"; }
log_success() { echo -e "${GREEN}[SUCCESS]${NC} $1"; }
log_warning() { echo -e "${YELLOW}[WARNING]${NC} $1"; }
log_error() { echo -e "${RED}[ERROR]${NC} $1"; }

check_steam_launch_options() {
    log_info "=== Steam Launch Options Check ==="
    
    # Check if Steam has launch options set
    echo "Please check your Steam launch options for Ready or Not:"
    echo "1. Right-click Ready or Not in Steam"
    echo "2. Properties → General → Launch Options"
    echo "3. If there are any launch options, CLEAR THEM"
    echo "4. The field should be completely empty"
    echo
    log_warning "Steam launch options are overriding STL configuration!"
    log_info "Current Steam command includes: -dx12"
    log_info "Our STL config should use: -noeac -dx11"
    echo
}

update_stl_config() {
    log_info "=== Updating STL Configuration for Override Fix ==="
    
    if [ ! -f "$STL_CONFIG" ]; then
        log_error "STL config not found: $STL_CONFIG"
        return 1
    fi
    
    # Enable argument sorting to override Steam's arguments
    log_info "Enabling argument sorting to override Steam launch options..."
    sed -i 's/SORTGARGS="0"/SORTGARGS="1"/' "$STL_CONFIG"
    
    # Ensure our game arguments are clear
    log_info "Setting game arguments to override Steam's -dx12..."
    sed -i 's/GAMEARGS="[^"]*"/GAMEARGS="-noeac -dx11"/' "$STL_CONFIG"
    
    # Make sure hardcoded args can be overridden
    sed -i 's/HARDARGS="[^"]*"/HARDARGS=""/' "$STL_CONFIG"
    
    log_success "STL configuration updated to override Steam arguments"
}

create_steam_instruction_script() {
    log_info "=== Creating Steam Configuration Helper ==="
    
    cat > /tmp/clear-steam-launch-options.txt << 'EOF'
IMPORTANT: Clear Steam Launch Options for Ready or Not

Steam is currently overriding our STL configuration with its own launch options.
The game is trying to launch with "-dx12" instead of our configured "-noeac -dx11".

To fix this:

1. Open Steam
2. Go to your Library
3. Right-click "Ready or Not"
4. Select "Properties..."
5. In the General tab, find "Launch Options"
6. DELETE everything in the Launch Options field
7. The field should be completely EMPTY
8. Close the Properties window
9. Launch the game again

Why this happens:
- Steam launch options take precedence over STL configuration
- The "-dx12" argument is causing crashes with our current setup
- STL needs full control over launch arguments for proper configuration

After clearing Steam's launch options, STL will use:
- Game Arguments: -noeac -dx11
- DirectX Version: 11 (more stable)
- Anti-Cheat: Disabled
- Renderer: WineD3D (for stability)
EOF
    
    log_success "Instructions saved to /tmp/clear-steam-launch-options.txt"
}

verify_config_changes() {
    log_info "=== Verifying Configuration Changes ==="
    
    if grep -q 'SORTGARGS="1"' "$STL_CONFIG"; then
        log_success "Argument sorting enabled"
    else
        log_warning "Argument sorting not enabled"
    fi
    
    local gameargs=$(grep "GAMEARGS=" "$STL_CONFIG" | cut -d'"' -f2)
    log_info "Current STL game arguments: $gameargs"
    
    if [[ "$gameargs" == "-noeac -dx11" ]]; then
        log_success "Game arguments are correct"
    else
        log_warning "Game arguments may need adjustment"
    fi
}

show_testing_steps() {
    echo
    log_info "=== Next Steps ==="
    echo "1. Clear Steam launch options (see /tmp/clear-steam-launch-options.txt)"
    echo "2. Launch Ready or Not through Steam"
    echo "3. Check STL log to verify our arguments are used:"
    echo "   tail -f ~/.config/steamtinkerlaunch/logs/steamtinkerlaunch/id/1144200.log"
    echo "4. Look for: 'Final game command line arguments: -noeac -dx11'"
    echo "5. If still using -dx12, Steam launch options weren't cleared"
    echo
    log_warning "Game must launch without Steam launch options interfering!"
}

main() {
    log_info "Fixing Steam launch options override issue..."
    echo
    
    check_steam_launch_options
    update_stl_config
    create_steam_instruction_script
    verify_config_changes
    show_testing_steps
}

main "$@"