#!/bin/bash

# Force Steam Tinker Launch to recognize Ready or Not
# Run this if STL isn't automatically detecting the game

set -e

GAME_ID="1144200"
STL_CONFIG_DIR="$HOME/.config/steamtinkerlaunch"
GAME_CONFIG_DIR="$STL_CONFIG_DIR/gamecfgs"

# 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 if STL is installed
if ! command -v steamtinkerlaunch >/dev/null 2>&1; then
    log_error "Steam Tinker Launch not found!"
    exit 1
fi

log_info "Forcing Steam Tinker Launch to recognize Ready or Not (ID: $GAME_ID)..."

# Method 1: Use STL's built-in compat add
log_info "Adding game to STL compatibility database..."
if steamtinkerlaunch compat add "$GAME_ID"; then
    log_success "Game added to STL database"
else
    log_warning "STL compat add failed, trying manual approach..."
fi

# Method 2: Ensure config file is in the right place
log_info "Verifying configuration file placement..."
if [ -f "/mnt/NV2/Development/claude-home/gaming/examples/ready-or-not/1144200-nvidia-1440p.conf" ]; then
    cp "/mnt/NV2/Development/claude-home/gaming/examples/ready-or-not/1144200-nvidia-1440p.conf" "$GAME_CONFIG_DIR/$GAME_ID.conf"
    log_success "Configuration file copied to STL directory"
else
    log_error "Source configuration file not found!"
    exit 1
fi

# Method 3: Create STL database entry manually if needed
log_info "Ensuring STL database knows about Ready or Not..."
if [ -d "$STL_CONFIG_DIR" ]; then
    # Create a simple database entry
    echo "1144200|Ready Or Not|ReadyOrNot.exe" >> "$STL_CONFIG_DIR/gameids.txt" 2>/dev/null || true
    log_success "Game entry added to local database"
fi

# Method 4: Test STL GUI recognition
log_info "Testing STL menu launch..."
echo "You can test if STL recognizes the game by running:"
echo "steamtinkerlaunch $GAME_ID"
echo
echo "Or launch Ready or Not through Steam and look for the STL menu."

log_success "Force detection completed!"
log_info "Next steps:"
echo "1. Launch Ready or Not through Steam"
echo "2. Look for the Steam Tinker Launch configuration menu"
echo "3. If STL menu doesn't appear, try setting compatibility tool again in Steam"
echo "4. Check ~/.config/steamtinkerlaunch/steamtinkerlaunch.log for errors"