# Shared EverQuest install root detection. Source from other ui/*.sh scripts.
find_game_path() {
    if [[ -n "${EQ_GAME_PATH:-}" && -f "${EQ_GAME_PATH}/eqgame.exe" ]]; then
        echo "${EQ_GAME_PATH}"
        return 0
    fi

    local steam_linux="${HOME}/.local/share/Steam/steamapps/common/Everquest F2P"
    if [[ -f "${steam_linux}/eqgame.exe" ]]; then
        echo "${steam_linux}"
        return 0
    fi

    if [[ -f "/mnt/d/EverQuest/eqgame.exe" ]]; then
        echo "/mnt/d/EverQuest"
        return 0
    fi

    local game_location
    game_location=$(find /mnt -type f -name eqgame.exe ! -path '*RECYCLE.BIN*' 2>/dev/null | head -n1)
    if [[ -n "${game_location}" ]]; then
        dirname "${game_location}"
        return 0
    fi

    return 1
}

resolve_game_path() {
    local game_path="${EQ_GAME_PATH:-}"
    if [[ -z "${game_path}" ]] || [[ ! -f "${game_path}/eqgame.exe" ]]; then
        if ! game_path=$(find_game_path); then
            echo "Error: EverQuest was not found! Set EQ_GAME_PATH to your install root." >&2
            return 1
        fi
        echo "Using GAME_PATH=${game_path}" >&2
    fi
    echo "${game_path}"
}
