#!/bin/sh
set +o noclobber

# Export ICUBE_RESOLVE_ID immediately so it's inherited by all child processes
# and can be verified by safe_kill_installer via /proc/<pid>/environ.
# This works regardless of how the script is invoked (direct, heredoc, pipe).
[ -n "$ICUBE_RESOLVE_ID" ] && export ICUBE_RESOLVE_ID

echo "${SCRIPT_ID}: [step] trae_start"
check_region() {
    local region=""
    local country_code=""
    local retry_count=0
    local max_retries=50

    local endpoints_env="${TRAE_LOGIN_GUIDANCE_ENDPOINTS}"
    if [ -z "$endpoints_env" ]; then
        print_install_results_and_exit 1006 "TRAE_LOGIN_GUIDANCE_ENDPOINTS is required"
    fi
    local endpoints
    endpoints=$(echo "$endpoints_env" | tr ',' ' ')

    local pids=""
    local files=""
    for ep in $endpoints; do
        tf=$(mktemp)
        files="$files $tf"
        curl -sf --connect-timeout 20 -X POST "$ep" > "$tf" & pids="$pids $!"
    done

    while [ $retry_count -lt $max_retries ]; do
        for f in $files; do
            if [ -s "$f" ]; then
                for pid in $pids; do kill "$pid" 2>/dev/null; done
                region=$(cat "$f" | grep -o '"Region":"[^"]*"' | sed 's/"Region":"\([^\"]*\)"/\1/')
                country_code=$(cat "$f" | grep -o '"CountryCode":"[^"]*"' | sed 's/"CountryCode":"\([^\"]*\)"/\1/')
                break 2
            fi
        done
        all_done=true
        for pid in $pids; do
            if kill -0 "$pid" 2>/dev/null; then all_done=false; break; fi
        done
        if [ "$all_done" = "true" ]; then
            break
        fi
        sleep 0.1
        retry_count=$((retry_count + 1))
    done

    for f in $files; do rm -f "$f"; done

    export TRAE_DETECT_COUNTRY_CODE=$(echo "$country_code" | tr -d '[:space:]')
    echo "$region" | tr -d '[:space:]'
}

# Initialize retry count environment variables
export DOWNLOAD_RETRY_COUNT=0
export EXTRACT_RETRY_COUNT=0
TRAE_TMP="${XDG_RUNTIME_DIR:-/tmp}"
download_tool=''
download_time=0

# Print installation results and exit
print_install_results_and_exit() {
    echo "${SCRIPT_ID}: start"
    echo "exitCode==$1=="
    echo "errorMessage==$2=="
    echo "listeningOn==$LISTENING_ON=="
    echo "connectionToken==$SERVER_CONNECTION_TOKEN=="
    echo "logFile==$SERVER_LOGFILE=="
    echo "osReleaseId==$OS_RELEASE_ID=="
    echo "arch==$ARCH=="
    echo "platform==$PLATFORM=="
    echo "tmpDir==$TRAE_TMP=="
    echo "downloadTool==$download_tool=="
    echo "downloadTime==${download_time}s=="
    echo "httpsProxy==${HTTPS_PROXY:-${https_proxy:-${ALL_PROXY}}}=="
    echo "regionDetectJson==$TRAE_REGION_DETECT_JSON=="
    echo "downloadRetryCount==$DOWNLOAD_RETRY_COUNT=="
    echo "extractRetryCount==$EXTRACT_RETRY_COUNT=="
    echo "${SCRIPT_ID}: end"
    exit $1
}

# Create install directory
if [ ! -d "$SERVER_DIR" ]; then
    mkdir -p "$SERVER_DIR"
    if [ $? -gt 0 ]; then
        echo "Error creating server install directory"
        print_install_results_and_exit 1001 "Error creating install directory: $SERVER_DIR"
    fi
fi
echo "${SCRIPT_ID}: [step] server_dir"


# Adjust download prefix based on region
REGION=$(check_region)
if [ "$REGION" = "CN" ] || [ "$REGION" = "US" ] || [ "$REGION" = "SG" ] || [ "$REGION" = "USTTP" ]; then
    SG_BASE=""
    US_BASE=""
    CN_BASE=""
    USTTP_BASE=""
    INTERNAL_USTTP_BASE=""

    prefixes_env="${TRAE_CDN_PREFIXES}"
    if [ -n "$prefixes_env" ]; then
        for entry in $(echo "$prefixes_env" | tr ',' ' '); do
            key=$(echo "$entry" | cut -d'=' -f1)
            val=$(echo "$entry" | cut -d'=' -f2-)
            case "$key" in
                SG) SG_BASE="$val" ;;
                US) US_BASE="$val" ;;
                CN) CN_BASE="$val" ;;
                USTTP) USTTP_BASE="$val" ;;
                internalUSTTP) INTERNAL_USTTP_BASE="$val" ;;
            esac
        done
    fi

    # Check if current SERVER_DOWNLOAD_PREFIX is using one of the public CDNs
    IS_PUBLIC_CDN=false
    for base in "$SG_BASE" "$US_BASE" "$CN_BASE" "$USTTP_BASE"; do
        if [ -n "$base" ]; then
            echo "$SERVER_DOWNLOAD_PREFIX" | grep -q "$base"
            if [ $? -eq 0 ]; then
                IS_PUBLIC_CDN=true
                break
            fi
        fi
    done

    # Only replace CDN if current prefix is using a public CDN
    if [ "$IS_PUBLIC_CDN" = "true" ]; then
        # Extract the path suffix (keep everything after /pkg/server/releases/)
        PATH_SUFFIX=""
        # Extract path suffix in sh-compatible way
        echo "$SERVER_DOWNLOAD_PREFIX" | grep -q "/pkg/server/releases/"
        if [ $? -eq 0 ]; then
            PATH_SUFFIX=$(echo "$SERVER_DOWNLOAD_PREFIX" | sed -n 's/.*\(\/pkg\/server\/releases\/.*\)/\1/p')
        fi

        # Select base by region and rebuild prefix (no hardcoded domains)
        SELECTED_BASE=""
        if [ "$REGION" = "SG" ]; then
            SELECTED_BASE="$SG_BASE"
        elif [ "$REGION" = "US" ]; then
            SELECTED_BASE="$US_BASE"
        elif [ "$REGION" = "CN" ]; then
            SELECTED_BASE="$CN_BASE"
        elif [ "$REGION" = "USTTP" ]; then
            SELECTED_BASE="$USTTP_BASE"
        fi

        if [ "$IDE_REGION" = "usttp" ] && [ -n "$INTERNAL_USTTP_BASE" ]; then
            SELECTED_BASE="$INTERNAL_USTTP_BASE"
        fi

        if [ -n "$SELECTED_BASE" ]; then
            SERVER_DOWNLOAD_PREFIX="${SELECTED_BASE}${PATH_SUFFIX}"
            case "$SERVER_DOWNLOAD_PREFIX" in
                */) ;;
                *) SERVER_DOWNLOAD_PREFIX="${SERVER_DOWNLOAD_PREFIX}/" ;;
            esac
        fi
    fi
fi
export TRAE_DETECT_REGION=$REGION
export TRAE_DETECT_COUNTRY_CODE=${TRAE_DETECT_COUNTRY_CODE}
echo "${SCRIPT_ID}: [step] check_region"

# Get remote version info
VERSION_FILE_URL="${SERVER_DOWNLOAD_PREFIX}version"
LOCAL_VERSION_FILE="$SERVER_DIR/version"

if [ -z "$REMOTE_VERSION" ]; then
    if [ -n "$(which curl)" ]; then
        REMOTE_VERSION=$(curl -f --retry 3 --connect-timeout 10 --location --silent $VERSION_FILE_URL)
    elif [ -n "$(which wget)" ]; then
        REMOTE_VERSION=$(wget --tries=3 --timeout=10 -qO- $VERSION_FILE_URL)
    fi
fi
echo "${SCRIPT_ID}: [step] get_remote_version"


COMPRESSION_EXT="tar.gz"
if [ "$ICUBE_REMOTE_USE_XZ_COMPRESSION" = "true" ]; then
    COMPRESSION_EXT="tar.xz"
fi

# Handle SERVER_PACKAGE_NAME
SERVER_PACKAGE_NAME=$(echo "$SERVER_PACKAGE_NAME" | sed 's/\.tar\.gz$//')  # First remove trailing .tar.gz
SERVER_PACKAGE_NAME=$(echo "$SERVER_PACKAGE_NAME" | sed 's/\.tar\.xz$//')  # Also remove possible .tar.xz suffix
SERVER_PACKAGE_NAME=$(echo "$SERVER_PACKAGE_NAME" | sed 's/ /%20/g')
if [ -n "$REMOTE_VERSION" ]; then
    REMOTE_VERSION_ESCAPED=$(echo "$REMOTE_VERSION" | sed 's/ /%20/g')
    SERVER_PACKAGE_NAME="${SERVER_PACKAGE_NAME}-${REMOTE_VERSION_ESCAPED}.${COMPRESSION_EXT}"
else
    SERVER_PACKAGE_NAME="${SERVER_PACKAGE_NAME}.${COMPRESSION_EXT}"
fi

NODE_DNS_RESULT_ORDER=""
if [ "$ICUBE_REMOTE_AIPREFER_IPV6" = "true" ]; then
    NODE_DNS_RESULT_ORDER="--dns-result-order=ipv6first"
fi

if [ -z "$MANAGER_DATA_DIR" ]; then
    MANAGER_DATA_DIR="${SERVER_DATA_DIR}"
fi
if [ ! -d "$MANAGER_DATA_DIR" ]; then
    mkdir -p "$MANAGER_DATA_DIR"
    if [ $? -gt 0 ]; then
        echo "Error creating MANAGER_DATA_DIR directory"
        print_install_results_and_exit 1002 "Error creating $MANAGER_DATA_DIR directory"
    fi
fi

# --- Server lock mechanism ---
# Prevents race conditions when multiple IDE windows try to download/start the server simultaneously.
# Uses mkdir-based lock (atomic on all POSIX filesystems).
# Lock is scoped by BOTH commitId and machineId so that different versions can install concurrently
# (they use different SERVER_DIRs and don't conflict).
COMMIT_SUFFIX_FOR_LOCK="${DISTRO_COMMIT:+-${DISTRO_COMMIT%${DISTRO_COMMIT#????????}}}"
MACHINE_ID_SUFFIX_FOR_LOCK="${ICUBE_MACHINE_ID:+-${ICUBE_MACHINE_ID%${ICUBE_MACHINE_ID#????????}}}"
SERVER_LOCK_DIR="$SERVER_DATA_DIR/.resolve-lock${COMMIT_SUFFIX_FOR_LOCK}${MACHINE_ID_SUFFIX_FOR_LOCK}"
SERVER_LOCK_TIMEOUT=1800
SERVER_LOCK_HEARTBEAT_STALE=30
SERVER_LOCK_ACQUIRED=false
SERVER_LOCK_HEARTBEAT_PID=""

# ============================================================
# Safely kill a process only if it's our installer (verified via ICUBE_RESOLVE_ID
# environment variable). Prevents killing an unrelated process if the PID was
# recycled, and correctly identifies installers executed via heredoc/pipe
# (e.g. 'curl | bash' or 'bash -s -- < <(EOF)') whose cmdline doesn't contain
# the script name.
# ============================================================
safe_kill_installer() {
    local target_pid="$1"
    [ -z "$target_pid" ] && return 1
    [[ ! "$target_pid" =~ ^[0-9]+$ ]] && return 1

    # First check: if PID is already dead, return success — the process is gone,
    # so the lock is definitely stale and we can safely delete it.
    if ! kill -0 "$target_pid" 2>/dev/null; then
        return 0
    fi

    # Second check: ICUBE_RESOLVE_ID in /proc/<pid>/environ must match.
    # This works for all execution modes (direct script, heredoc, pipe)
    # because the environment is inherited regardless of how bash is invoked.
    if [ -n "$ICUBE_RESOLVE_ID" ]; then
        local env_match
        env_match=$(cat "/proc/$target_pid/environ" 2>/dev/null | tr '\0' '\n' | grep "^ICUBE_RESOLVE_ID=$ICUBE_RESOLVE_ID$")
        if [ -n "$env_match" ]; then
            kill "$target_pid" 2>/dev/null
            return 0
        fi
    fi
    return 1
}

# Check if the server is already running with a valid port.
# Returns 0 (true) if ready, 1 (false) if not.
is_server_running_with_port() {
    if [ -f "$SERVER_PIDFILE" ] && [ -f "$SERVER_TOKENFILE" ] && [ -f "$SERVER_LOGFILE" ]; then
        local check_pid=$(cat "$SERVER_PIDFILE" 2>/dev/null)
        if [ -n "$check_pid" ] && kill -0 "$check_pid" 2>/dev/null; then
            local check_port=$(grep -E 'Extension host agent listening on .+' "$SERVER_LOGFILE" 2>/dev/null | tail -1 | sed 's/.*Extension host agent listening on //' | sed 's/.*://' | tr -d '[:space:]')
            if [ -n "$check_port" ]; then
                echo "Server running (pid=$check_pid, port=$check_port)"
                return 0
            fi
        fi
    fi
    return 1
}

acquire_server_lock() {
    local start_time=$(date +%s)

    # ============================================================
    # Pre-check: If the lock already exists and is our own stale lock
    # from a previous attempt in the same resolve session.
    # Break it immediately without waiting for the loop.
    # We must verify via /proc/<pid>/environ that the process is
    # truly our installer before killing, to handle edge cases
    # where the script is executed via heredoc/pipe and cmdline
    # doesn't contain the script name.
    # ============================================================
    if [ -d "$SERVER_LOCK_DIR" ]; then
        if [ -n "$ICUBE_RESOLVE_ID" ] && [ -f "$SERVER_LOCK_DIR/resolveId" ]; then
            local pre_resolve_id=$(cat "$SERVER_LOCK_DIR/resolveId" 2>/dev/null)
            if [ "$pre_resolve_id" = "$ICUBE_RESOLVE_ID" ]; then
                echo "Pre-check: Lock is our own stale lock (resolve ID $ICUBE_RESOLVE_ID)"
                local killed=false
                if [ -f "$SERVER_LOCK_DIR/pid" ]; then
                    local pre_pid=$(cat "$SERVER_LOCK_DIR/pid" 2>/dev/null)
                    if safe_kill_installer "$pre_pid"; then
                        echo "Successfully killed stale installer process $pre_pid"
                        killed=true
                    else
                        echo "Could not verify stale installer process $pre_pid (may be running via heredoc/pipe without env var), will let heartbeat/timeout handle it"
                    fi
                fi
                if [ "$killed" = true ] || [ ! -f "$SERVER_LOCK_DIR/pid" ]; then
                    rm -rf "$SERVER_LOCK_DIR"
                fi
            fi
        fi
    fi

    # ============================================================
    # Clean up all stale lock directories in SERVER_DATA_DIR.
    # Skip locks that are still actively held (heartbeat fresh or PID alive).
    # ============================================================
    for lock_dir in "$SERVER_DATA_DIR"/.resolve-lock-*; do
        [ -d "$lock_dir" ] || continue
        [ "$lock_dir" = "$SERVER_LOCK_DIR" ] && continue

        # Skip if heartbeat is fresh (holder is still working)
        if [ -f "$lock_dir/heartbeat" ]; then
            local hb_time=$(cat "$lock_dir/heartbeat" 2>/dev/null)
            if [ -n "$hb_time" ]; then
                local now=$(date +%s)
                if [ $((now - hb_time)) -le $SERVER_LOCK_HEARTBEAT_STALE ]; then
                    continue
                fi
            fi
        fi

        # Skip if PID is still alive
        if [ -f "$lock_dir/pid" ]; then
            local lock_pid=$(cat "$lock_dir/pid" 2>/dev/null)
            if [ -n "$lock_pid" ] && kill -0 "$lock_pid" 2>/dev/null; then
                continue
            fi
        fi

        # Stale lock — clean it up
        echo "Cleaning up stale lock directory: $lock_dir"
        rm -rf "$lock_dir"
    done

    while ! mkdir "$SERVER_LOCK_DIR" 2>/dev/null; do
        # ============================================================
        # Priority 1: Check if we can skip installation entirely
        # (other worker already finished the job)
        # ============================================================
        if [ -f "$SERVER_SCRIPT" ] && [ -f "$LOCAL_VERSION_FILE" ]; then
            local installed_ver=$(cat "$LOCAL_VERSION_FILE" 2>/dev/null)
            if [ -n "$REMOTE_VERSION" ] && [ "$installed_ver" = "$REMOTE_VERSION" ]; then
                echo "Server binary installed by another session while waiting for lock, proceeding"
                return 1
            fi
        fi
        if is_server_running_with_port; then
            echo "Server started by another session while waiting for lock"
            return 1
        fi

        # ============================================================
        # Priority 2: Check if lock holder is definitely dead
        # ============================================================
        if [ -f "$SERVER_LOCK_DIR/pid" ]; then
            local lock_pid=$(cat "$SERVER_LOCK_DIR/pid" 2>/dev/null)
            if [ -n "$lock_pid" ] && ! kill -0 "$lock_pid" 2>/dev/null; then
                echo "Lock holder (pid $lock_pid) is dead, breaking lock"
                rm -rf "$SERVER_LOCK_DIR"
                continue
            fi

            # ============================================================
            # Priority 2.5: Heartbeat stale — lock holder's shell died but
            # PID was recycled. The heartbeat file stops being updated when
            # the background writer is killed (SSH disconnect kills shell).
            # ============================================================
            if [ -f "$SERVER_LOCK_DIR/heartbeat" ]; then
                local hb_time=""
                hb_time=$(cat "$SERVER_LOCK_DIR/heartbeat" 2>/dev/null)
                if [ -n "$hb_time" ]; then
                    local current_time_hb=$(date +%s)
                    if [ $((current_time_hb - hb_time)) -gt $SERVER_LOCK_HEARTBEAT_STALE ]; then
                        echo "Lock holder (pid $lock_pid) heartbeat stale (>${SERVER_LOCK_HEARTBEAT_STALE}s), breaking lock"
                        rm -rf "$SERVER_LOCK_DIR"
                        continue
                    fi
                fi
            fi

            # ============================================================
            # Priority 2.6: Same resolve ID — this is our own stale lock
            # from a previous attempt in the same resolve session. The same
            # resolve session cannot have two install scripts running.
            # We must verify via /proc/<pid>/environ that the process is
            # truly our installer before killing, to handle edge cases
            # where the script is executed via heredoc/pipe and cmdline
            # doesn't contain the script name.
            # ============================================================
            if [ -n "$ICUBE_RESOLVE_ID" ] && [ -f "$SERVER_LOCK_DIR/resolveId" ]; then
                local lock_resolve_id=$(cat "$SERVER_LOCK_DIR/resolveId" 2>/dev/null)
                if [ "$lock_resolve_id" = "$ICUBE_RESOLVE_ID" ]; then
                    echo "Lock held by same resolve ID ($ICUBE_RESOLVE_ID), breaking stale lock from previous attempt"
                    if safe_kill_installer "$lock_pid"; then
                        echo "Successfully killed stale installer process $lock_pid"
                        rm -rf "$SERVER_LOCK_DIR"
                        continue
                    else
                        echo "Could not verify stale installer process $lock_pid (may be running via heredoc/pipe without env var), will continue waiting"
                    fi
                fi
            fi

            # ============================================================
            # Priority 3: PID appears alive — but could be recycled.
            # Only break lock if:
            #   (a) lock is old AND server artifacts are ready (holder finished), OR
            #   (b) lock is old AND no artifacts exist (holder is stuck/dead with recycled PID)
            # Do NOT break lock just because of timeout if holder is still working.
            # ============================================================
            local lock_time_pid=""
            if stat -c %Y "$SERVER_LOCK_DIR" >/dev/null 2>&1; then
                lock_time_pid=$(stat -c %Y "$SERVER_LOCK_DIR")
            elif stat -f %m "$SERVER_LOCK_DIR" >/dev/null 2>&1; then
                lock_time_pid=$(stat -f %m "$SERVER_LOCK_DIR")
            fi
            if [ -n "$lock_time_pid" ]; then
                local current_time_pid=$(date +%s)
                if [ $((current_time_pid - lock_time_pid)) -gt $SERVER_LOCK_TIMEOUT ]; then
                    # Case (a): artifacts ready — holder finished, safe to break
                    if [ -f "$SERVER_SCRIPT" ] && [ -f "$LOCAL_VERSION_FILE" ]; then
                        local stale_ver=$(cat "$LOCAL_VERSION_FILE" 2>/dev/null)
                        if [ -n "$REMOTE_VERSION" ] && [ "$stale_ver" = "$REMOTE_VERSION" ]; then
                            echo "Lock with pid $lock_pid stale (age > ${SERVER_LOCK_TIMEOUT}s) and server artifacts ready, breaking it"
                            rm -rf "$SERVER_LOCK_DIR"
                            continue
                        fi
                    fi
                    # Case (b): no artifacts after timeout — holder likely dead with recycled PID
                    echo "Lock with pid $lock_pid appears stale (age > ${SERVER_LOCK_TIMEOUT}s, pid likely recycled), breaking it"
                    rm -rf "$SERVER_LOCK_DIR"
                    continue
                fi
            fi
        else
            # Lock dir exists but no pid file — check resolveId first, then stale by age
            if [ -n "$ICUBE_RESOLVE_ID" ] && [ -f "$SERVER_LOCK_DIR/resolveId" ]; then
                local lock_resolve_id_nopid=$(cat "$SERVER_LOCK_DIR/resolveId" 2>/dev/null)
                if [ "$lock_resolve_id_nopid" = "$ICUBE_RESOLVE_ID" ]; then
                    echo "Lock (no pid) held by same resolve ID ($ICUBE_RESOLVE_ID), breaking stale lock"
                    rm -rf "$SERVER_LOCK_DIR"
                    continue
                fi
            fi
            local lock_time=""
            if stat -c %Y "$SERVER_LOCK_DIR" >/dev/null 2>&1; then
                lock_time=$(stat -c %Y "$SERVER_LOCK_DIR")
            elif stat -f %m "$SERVER_LOCK_DIR" >/dev/null 2>&1; then
                lock_time=$(stat -f %m "$SERVER_LOCK_DIR")
            fi
            if [ -n "$lock_time" ]; then
                local current_time=$(date +%s)
                if [ $((current_time - lock_time)) -gt $SERVER_LOCK_TIMEOUT ]; then
                    echo "Lock without pid appears stale (age > ${SERVER_LOCK_TIMEOUT}s), breaking it"
                    rm -rf "$SERVER_LOCK_DIR"
                    continue
                fi
            fi
        fi

        local elapsed=$(($(date +%s) - start_time))
        if [ $elapsed -gt $SERVER_LOCK_TIMEOUT ]; then
            echo "Warning: Failed to acquire server lock after ${SERVER_LOCK_TIMEOUT}s, proceeding anyway"
            return 1
        fi
        echo "Another resolve session is installing/starting the server, waiting... (${elapsed}s)"
        sleep 2
        # Do NOT check is_server_running_with_port here — it only verifies PID+port in log
        # which can be stale (e.g. "Last EH closed, shutting down"). Let the loop re-attempt
        # mkdir; once lock is free, the caller goes through the full health validation path.
    done
    echo "$ICUBE_RESOLVE_ID" > "$SERVER_LOCK_DIR/resolveId"
    echo $$ > "$SERVER_LOCK_DIR/pid"
    date +%s > "$SERVER_LOCK_DIR/heartbeat"
    (while [ -d "$SERVER_LOCK_DIR" ] && kill -0 $$ 2>/dev/null; do date +%s > "$SERVER_LOCK_DIR/heartbeat" 2>/dev/null; sleep 5; done) &
    SERVER_LOCK_HEARTBEAT_PID=$!
    SERVER_LOCK_ACQUIRED=true
    return 0
}

release_server_lock() {
    if [ "$SERVER_LOCK_ACQUIRED" = true ]; then
        if [ -n "$SERVER_LOCK_HEARTBEAT_PID" ]; then
            kill "$SERVER_LOCK_HEARTBEAT_PID" 2>/dev/null
            wait "$SERVER_LOCK_HEARTBEAT_PID" 2>/dev/null
            SERVER_LOCK_HEARTBEAT_PID=""
        fi
        rm -rf "$SERVER_LOCK_DIR"
        SERVER_LOCK_ACQUIRED=false
    fi
}

find_server_process_by_machine_id() {
    local candidates
    candidates="$(ps -o pid,args -A 2>/dev/null | grep "$SERVER_SCRIPT" | grep -v grep)"
    if [ -z "$ICUBE_MACHINE_ID" ] || [ ! -d "/proc" ]; then
        echo "$candidates"
        return
    fi
    echo "$candidates" | while IFS= read -r line; do
        local pid
        pid="$(echo "$line" | awk '{print $1}')"
        if [ -n "$pid" ] && cat "/proc/$pid/environ" 2>/dev/null | tr '\0' '\n' | grep -q "^ICUBE_MACHINE_ID=$ICUBE_MACHINE_ID$"; then
            echo "$line"
        fi
    done
}

# --- Quick pre-lock check ---
# If the server is already running with a valid port, token, AND matching version,
# return immediately without acquiring the lock.
if [ -f "$SERVER_PIDFILE" ] && [ -f "$SERVER_TOKENFILE" ] && [ -f "$SERVER_LOGFILE" ]; then
    QUICK_PID="$(cat "$SERVER_PIDFILE" 2>/dev/null)"
    if [ -n "$QUICK_PID" ] && kill -0 "$QUICK_PID" 2>/dev/null; then
        # Verify version matches (skip quick path if upgrade needed)
        QUICK_VERSION_OK=true
        if [ -n "$REMOTE_VERSION" ] && [ -f "$LOCAL_VERSION_FILE" ]; then
            QUICK_LOCAL_VER="$(cat "$LOCAL_VERSION_FILE" 2>/dev/null)"
            if [ "$QUICK_LOCAL_VER" != "$REMOTE_VERSION" ]; then
                QUICK_VERSION_OK=false
            fi
        elif [ -n "$REMOTE_VERSION" ] && [ ! -f "$LOCAL_VERSION_FILE" ]; then
            QUICK_VERSION_OK=false
        fi

        if [ "$QUICK_VERSION_OK" = "true" ]; then
            QUICK_PORT="$(grep -E 'Extension host agent listening on .+' "$SERVER_LOGFILE" | tail -1 | sed 's/.*Extension host agent listening on //' | sed 's/.*://' | tr -d '[:space:]')"
            QUICK_TOKEN="$(cat "$SERVER_TOKENFILE" 2>/dev/null)"
            if [ -n "$QUICK_PORT" ] && [ -n "$QUICK_TOKEN" ]; then
                # Verify machineId matches (if available)
                QUICK_MATCH=true
                if [ -n "$ICUBE_MACHINE_ID" ] && [ -d "/proc" ]; then
                    if ! cat "/proc/$QUICK_PID/environ" 2>/dev/null | tr '\0' '\n' | grep -q "^ICUBE_MACHINE_ID=$ICUBE_MACHINE_ID$"; then
                        QUICK_MATCH=false
                    fi
                fi
                if [ "$QUICK_MATCH" = "true" ]; then
                    echo "Server already running (pid=$QUICK_PID, port=$QUICK_PORT). Skipping lock."
                    LISTENING_ON="$QUICK_PORT"
                    SERVER_CONNECTION_TOKEN="$QUICK_TOKEN"
                    print_install_results_and_exit 0
                fi
            fi
        fi
    fi
fi

# Acquire lock before checking/downloading/starting server
if ! acquire_server_lock; then
    # Lock not acquired (timeout or server/binary appeared while waiting).
    # If server is already running with valid port, return immediately.
    if [ -f "$SERVER_PIDFILE" ] && [ -f "$SERVER_TOKENFILE" ] && [ -f "$SERVER_LOGFILE" ]; then
        SKIP_PID="$(cat "$SERVER_PIDFILE" 2>/dev/null)"
        if [ -n "$SKIP_PID" ] && kill -0 "$SKIP_PID" 2>/dev/null; then
            LISTENING_ON="$(grep -E 'Extension host agent listening on .+' "$SERVER_LOGFILE" 2>/dev/null | tail -1 | sed 's/.*Extension host agent listening on //' | sed 's/.*://' | tr -d '[:space:]')"
            SERVER_CONNECTION_TOKEN="$(cat "$SERVER_TOKENFILE" 2>/dev/null)"
            if [ -n "$LISTENING_ON" ] && [ -n "$SERVER_CONNECTION_TOKEN" ]; then
                echo "Server ready (started by another session). pid=$SKIP_PID, port=$LISTENING_ON"
                print_install_results_and_exit 0
            fi
        fi
    fi
    # Server not running yet — check if binary is at least installed with correct version.
    NEED_INSTALL=true
    if [ -f "$SERVER_SCRIPT" ] && [ -f "$LOCAL_VERSION_FILE" ]; then
        LOCAL_VERSION=$(cat "$LOCAL_VERSION_FILE")
        if [ -n "$REMOTE_VERSION" ] && [ "$LOCAL_VERSION" = "$REMOTE_VERSION" ]; then
            NEED_INSTALL=false
        fi
    fi
    if [ "$NEED_INSTALL" = true ]; then
        echo "Error: Cannot acquire server lock and server binary is not ready"
        print_install_results_and_exit 1000 "Failed to acquire server lock: $SERVER_LOCK_DIR"
    fi
fi

# Determine whether reinstall is needed
NEED_INSTALL=true
if [ -f "$SERVER_SCRIPT" ]; then
    if [ -f "$LOCAL_VERSION_FILE" ]; then
        LOCAL_VERSION=$(cat "$LOCAL_VERSION_FILE")
        if [ -n "$REMOTE_VERSION" ] && [ "$LOCAL_VERSION" = "$REMOTE_VERSION" ]; then
            NEED_INSTALL=false
        else
            rm -rf "$SERVER_DIR"/*
        fi
    else
        rm -rf "$SERVER_DIR"/*
    fi
fi

TAR_NAME="vscode-server.$$.${COMPRESSION_EXT}"
cleanup() {
    [ -f "$TAR_NAME" ] && rm -f "$TAR_NAME" "$TAR_NAME.md5"
    rm -rf "$SERVER_DIR/.fetch-tmp"
    release_server_lock
}
trap cleanup EXIT

# Install server
if [ "$NEED_INSTALL" = true ]; then
    echo "${SCRIPT_ID}: [step] need_install_true"

    cd "$SERVER_DIR" > /dev/null
    OLDPWD_SAVED=$PWD


    # Download and extract server package
    SERVER_DOWNLOAD_URL="${SERVER_DOWNLOAD_PREFIX}${SERVER_PACKAGE_NAME}"
    max_download_retries=3
    max_extract_retries=3
    max_total_retries=3
    total_retry_count=0

    start_time=$(date +%s)
    # Outer loop including download and extraction
    while [ $total_retry_count -lt $max_total_retries ]; do
        # Download process
        download_retry_count=0
        download_success=false
        fetch_failed=false

        while [ $download_retry_count -lt $max_download_retries ]; do
            if [ -z "$NO_USE_FETCH" ] && [ "$fetch_failed" != "true" ] && [ $download_retry_count -eq 0 ]; then
                download_tool=fetch
                try_download_using_fetch "$TAR_NAME.md5" "$SERVER_DOWNLOAD_URL.md5" || true
                try_download_using_fetch "$TAR_NAME" "$SERVER_DOWNLOAD_URL"
                download_result=$?
                if [ $download_result -eq 0 ] && [ -f "$TAR_NAME" ]; then
                    download_success=true
                    break
                else
                    fetch_failed=true
                fi
            fi
            if [ -n "$(which wget)" ]; then
                download_tool=wget
                wget --tries=3 --timeout=10 --continue --no-verbose -O "$TAR_NAME.md5" "$SERVER_DOWNLOAD_URL.md5" || true
                wget --tries=3 --timeout=10 --continue --no-verbose -O "$TAR_NAME" "$SERVER_DOWNLOAD_URL"
                download_result=$?
            elif [ -n "$(which curl)" ]; then
                download_tool=curl
                curl -f --retry 3 --connect-timeout 10 --location --show-error --silent --output "$TAR_NAME.md5" "$SERVER_DOWNLOAD_URL.md5" || true
                curl -f --retry 3 --connect-timeout 10 --location --show-error --silent --output "$TAR_NAME" "$SERVER_DOWNLOAD_URL"
                download_result=$?
            else
                echo "Error no tool to download server binary"
                print_install_results_and_exit 2001 "Lost tool to download server, please install curl or wget"
            fi

            if [ $download_result -eq 0 ] && [ -f "$TAR_NAME" ]; then
                download_success=true
                break
            else
                if [ "$COMPRESSION_EXT" = "tar.xz" ]; then
                    echo "Warning: tar.xz compression is not supported on this system. Falling back to tar.gz."
                    COMPRESSION_EXT="tar.gz"
                    SERVER_PACKAGE_NAME="${SERVER_PACKAGE_NAME%.tar.xz}.${COMPRESSION_EXT}"
                    SERVER_DOWNLOAD_URL="${SERVER_DOWNLOAD_PREFIX}${SERVER_PACKAGE_NAME}"
                    TAR_NAME="${TAR_NAME%.tar.xz}.${COMPRESSION_EXT}"
                fi
            fi

            download_retry_count=$((download_retry_count+1))
            export DOWNLOAD_RETRY_COUNT=$download_retry_count
            echo "Download failed, retry attempt $download_retry_count..."
            sleep 1
        done

        if [ "$download_success" != "true" ]; then
            echo "Error: Download failed after $download_retry_count retry attempts"
            print_install_results_and_exit 2001 "Download server failed after $download_retry_count retry attempts"
        fi

        # Output file size and MD5 info
        if [ -f "$TAR_NAME" ]; then
            file_size=$(du "$TAR_NAME" | cut -f1)
            if [ -n "$(which md5sum)" ]; then
                file_md5=$(md5sum "$TAR_NAME" | cut -d' ' -f1)
            elif [ -n "$(which md5)" ]; then
                file_md5=$(md5 -q "$TAR_NAME")
            else
                file_md5=""
                echo "Warning: Unable to calculate MD5, system lacks md5sum or md5 tools"
            fi
            echo "Download completed: $TAR_NAME (Size: $file_size, MD5: $file_md5)"

            # compare md5
            if [ -n "$file_md5" ] && [ -f "$TAR_NAME.md5" ]; then
                expected_md5=$(cat "$TAR_NAME.md5")
                if [ -n "$expected_md5" ] && [ "$file_md5" != "$expected_md5" ]; then
                    echo "Warning: MD5 checksum mismatch for $TAR_NAME"
                    echo "Expected: $expected_md5"
                    echo "Got: $file_md5"
                    rm -rf "$TAR_NAME" "$TAR_NAME.md5" || echo "Warning: Failed to remove $TAR_NAME"
                    total_retry_count=$((total_retry_count+1))
                    continue
                fi
            fi

        fi

        echo "${SCRIPT_ID}: [step] server_download"

        # Extraction process
        extract_retry_count=0
        extract_success=false

        while [ $extract_retry_count -lt $max_extract_retries ]; do
            tar -xf "$TAR_NAME" --strip-components 1
            if [ $? -eq 0 ]; then
                extract_success=true
                break
            fi

            extract_retry_count=$((extract_retry_count+1))
            export EXTRACT_RETRY_COUNT=$extract_retry_count
            echo "Extraction failed, retry attempt $extract_retry_count..."
            sleep 1
        done

        if [ "$extract_success" = "true" ]; then
            # Extraction succeeded, exit outer loop
            echo "${SCRIPT_ID}: [step] server_extract"
            break
        else
            # Extraction failed, increase total retries
            total_retry_count=$((total_retry_count+1))
            echo "Extraction failed after $extract_retry_count attempts, redownloading... (attempt $total_retry_count of $max_total_retries)"

            # Remove potentially corrupted files
            rm -f "$TAR_NAME" "$TAR_NAME.md5" || echo "Warning: Failed to remove $TAR_NAME"

            if [ "$COMPRESSION_EXT" = "tar.xz" ]; then
                echo "Warning: tar.xz compression is not supported on this system. Falling back to tar.gz."
                COMPRESSION_EXT="tar.gz"
                SERVER_PACKAGE_NAME="${SERVER_PACKAGE_NAME%.tar.xz}.${COMPRESSION_EXT}"
                SERVER_DOWNLOAD_URL="${SERVER_DOWNLOAD_PREFIX}${SERVER_PACKAGE_NAME}"
                TAR_NAME="${TAR_NAME%.tar.xz}.${COMPRESSION_EXT}"
            fi

            # Exit if max retries reached
            if [ $total_retry_count -ge $max_total_retries ]; then
                echo "Error: Failed to extract server contents after $total_retry_count total attempts"
                print_install_results_and_exit 2002 "Unzip server failed, check tar version or disk space"
            fi

            # Retry after short delay
            sleep 2
        fi
    done

    # If we exhausted retries without successful extraction, fail explicitly.
    if [ "$extract_success" != "true" ] && [ "$download_success" != "true" ]; then
        echo "Error: Download/extract failed after $total_retry_count total retry attempts (MD5 mismatch or network corruption)"
        print_install_results_and_exit 2001 "Download server failed: MD5 checksum mismatch after $total_retry_count retry attempts"
    elif [ "$extract_success" != "true" ]; then
        echo "Error: Extraction failed after $total_retry_count total retry attempts"
        print_install_results_and_exit 2002 "Unzip server failed after $total_retry_count retry attempts"
    fi

    download_time=$(($(date +%s)-start_time))
    echo "server_download_time=$download_time"

    # Write version info
    if [ -n "$REMOTE_VERSION" ]; then
        echo "$REMOTE_VERSION" > "$LOCAL_VERSION_FILE"
    fi

    rm -f "$TAR_NAME" "$TAR_NAME.md5" || echo "Warning: Failed to remove $TAR_NAME"
    cd "$OLDPWD_SAVED" > /dev/null
fi

# Check if server is already running
if [ -f "$SERVER_PIDFILE" ]; then
    SERVER_PID="$(cat "$SERVER_PIDFILE")"
    SERVER_RUNNING_PROCESS="$(ps -o pid,args -p "$SERVER_PID" | grep "$SERVER_SCRIPT")"
    if [ -n "$SERVER_RUNNING_PROCESS" ] && [ -n "$ICUBE_MACHINE_ID" ] && [ -d "/proc" ]; then
        if ! cat "/proc/$SERVER_PID/environ" 2>/dev/null | tr '\0' '\n' | grep -q "^ICUBE_MACHINE_ID=$ICUBE_MACHINE_ID$"; then
            SERVER_RUNNING_PROCESS=""
        fi
    fi
else
    SERVER_RUNNING_PROCESS="$(find_server_process_by_machine_id)"
fi

# If server process is found, verify it is still listening on the expected port.
# The server may have --remote-auto-shutdown-without-delay enabled and stopped
# accepting connections while the process is still alive during shutdown.
if [ -n "$SERVER_RUNNING_PROCESS" ] && [ -f "$SERVER_LOGFILE" ]; then
    OLD_LISTENING_ON="$(cat "$SERVER_LOGFILE" | grep -E 'Extension host agent listening on .+' | tail -1 | sed 's/.*Extension host agent listening on //' | tr -d '[:space:]')"
    # Extract host and port from the log line.
    # Formats: "[::1]:PORT", "HOST:PORT", or just "PORT"
    OLD_LISTENING_HOST=""
    OLD_LISTENING_PORT=""
    case "$OLD_LISTENING_ON" in
        \[*\]:*)
            # IPv6 bracketed: [::1]:PORT
            OLD_LISTENING_HOST="$(echo "$OLD_LISTENING_ON" | sed 's/^\[\([^]]*\)\].*/\1/')"
            OLD_LISTENING_PORT="$(echo "$OLD_LISTENING_ON" | sed 's/.*\]://')"
            ;;
        *:*)
            # HOST:PORT (IPv4 or hostname)
            OLD_LISTENING_HOST="$(echo "$OLD_LISTENING_ON" | sed 's/:[^:]*$//')"
            OLD_LISTENING_PORT="$(echo "$OLD_LISTENING_ON" | sed 's/.*://')"
            ;;
        *)
            # Just PORT
            OLD_LISTENING_PORT="$OLD_LISTENING_ON"
            ;;
    esac
    # For curl/wget: derive the connectable host. Use the actual host from log,
    # falling back to TRAE_SERVER_HOST or 127.0.0.1.
    # If host is 0.0.0.0, use 127.0.0.1 instead (can't connect to 0.0.0.0 directly).
    if [ -n "$OLD_LISTENING_HOST" ] && [ "$OLD_LISTENING_HOST" != "0.0.0.0" ]; then
        SERVER_HOST_CHECK="$OLD_LISTENING_HOST"
    else
        SERVER_HOST_CHECK="${TRAE_SERVER_HOST:-127.0.0.1}"
    fi
    # IPv6 addresses need brackets in URLs
    case "$SERVER_HOST_CHECK" in
        *:*) SERVER_HOST_CHECK="[${SERVER_HOST_CHECK}]" ;;
    esac

    # Derive a killable PID: prefer SERVER_PID (from pidfile), fallback to extracting from SERVER_RUNNING_PROCESS
    KILL_PID="$SERVER_PID"
    if [ -z "$KILL_PID" ] && [ -n "$SERVER_RUNNING_PROCESS" ]; then
        KILL_PID="$(echo "$SERVER_RUNNING_PROCESS" | awk '{print $1}')"
    fi

    if [ -n "$OLD_LISTENING_PORT" ]; then
        PORT_ALIVE=false
        # Check if the port is still listening using available tools
        if command -v ss >/dev/null 2>&1; then
            if ss -tlnH "sport = :${OLD_LISTENING_PORT}" 2>/dev/null | grep -q "${OLD_LISTENING_PORT}"; then
                PORT_ALIVE=true
            fi
        elif command -v netstat >/dev/null 2>&1; then
            if netstat -tln 2>/dev/null | grep -q ":${OLD_LISTENING_PORT} "; then
                PORT_ALIVE=true
            fi
        elif command -v nc >/dev/null 2>&1; then
            if nc -z "$SERVER_HOST_CHECK" "$OLD_LISTENING_PORT" 2>/dev/null; then
                PORT_ALIVE=true
            fi
        elif command -v curl >/dev/null 2>&1; then
            if curl -s --connect-timeout 2 "http://${SERVER_HOST_CHECK}:${OLD_LISTENING_PORT}" >/dev/null 2>&1; then
                PORT_ALIVE=true
            fi
        else
            # No tool available to check, assume alive to avoid false restarts
            PORT_ALIVE=true
        fi

        if [ "$PORT_ALIVE" != "true" ]; then
            echo "Warning: Server process exists but port ${OLD_LISTENING_PORT} is not reachable. Restarting server."
            if [ -n "$KILL_PID" ]; then
                kill "$KILL_PID" 2>/dev/null
                sleep 1
                if ps -p "$KILL_PID" >/dev/null 2>&1; then
                    kill -9 "$KILL_PID" 2>/dev/null
                fi
            fi
            SERVER_RUNNING_PROCESS=""
        else
            # Port is open at TCP level, but check if server is in shutdown/zombie state.
            # The server with --remote-auto-shutdown-without-delay may still have the port open
            # but no longer processes new connections after "Last EH closed, shutting down".
            if grep -q "Last EH closed, shutting down" "$SERVER_LOGFILE" 2>/dev/null; then
                # Check if shutdown message appears AFTER the listening message
                LAST_LISTEN_LINE=$(grep -n "Extension host agent listening on" "$SERVER_LOGFILE" | tail -1 | cut -d: -f1)
                LAST_SHUTDOWN_LINE=$(grep -n "Last EH closed, shutting down" "$SERVER_LOGFILE" | tail -1 | cut -d: -f1)
                if [ -n "$LAST_LISTEN_LINE" ] && [ -n "$LAST_SHUTDOWN_LINE" ] && [ "$LAST_SHUTDOWN_LINE" -gt "$LAST_LISTEN_LINE" ]; then
                    echo "Warning: Server is in zombie state (shutdown after listening). Port ${OLD_LISTENING_PORT} is open but server won't accept new connections. Restarting."
                    if [ -n "$KILL_PID" ]; then
                        kill "$KILL_PID" 2>/dev/null
                        sleep 1
                        if ps -p "$KILL_PID" >/dev/null 2>&1; then
                            kill -9 "$KILL_PID" 2>/dev/null
                        fi
                    fi
                    SERVER_RUNNING_PROCESS=""
                fi
            fi
        fi
    fi
fi

# If server process is alive and port is reachable, verify it's healthy and matches our expected version.
# Use /version endpoint (no auth needed) — returns the server's commitId.
# If it doesn't respond or returns a different commitId, the server is stale/unhealthy.
if [ -n "$SERVER_RUNNING_PROCESS" ] && [ -f "$SERVER_TOKENFILE" ] && [ -n "$OLD_LISTENING_PORT" ] && [ "$PORT_ALIVE" = "true" ]; then
    VERSION_CHECK_URL="http://${SERVER_HOST_CHECK}:${OLD_LISTENING_PORT}/version"
    SERVER_REPORTED_VERSION=""
    if command -v curl >/dev/null 2>&1; then
        SERVER_REPORTED_VERSION=$(curl -sf --connect-timeout 3 "$VERSION_CHECK_URL" 2>/dev/null)
    elif command -v wget >/dev/null 2>&1; then
        SERVER_REPORTED_VERSION=$(wget -qO- --timeout=3 "$VERSION_CHECK_URL" 2>/dev/null)
    fi
    SERVER_REPORTED_VERSION=$(echo "$SERVER_REPORTED_VERSION" | tr -d '[:space:]')

    SHOULD_RESTART=false
    if [ -z "$SERVER_REPORTED_VERSION" ]; then
        echo "Warning: Server not responding to /version health check. Restarting server."
        SHOULD_RESTART=true
    fi

    if [ "$SHOULD_RESTART" = "true" ]; then
        if [ -n "$KILL_PID" ]; then
            kill "$KILL_PID" 2>/dev/null
            sleep 1
            if ps -p "$KILL_PID" >/dev/null 2>&1; then
                kill -9 "$KILL_PID" 2>/dev/null
            fi
        fi
        rm -f "$SERVER_LOGFILE" "$SERVER_TOKENFILE" "$SERVER_PIDFILE"
        SERVER_RUNNING_PROCESS=""
    fi
fi

TOKEN_FILE_EXISTS=false
if [ -f "$SERVER_TOKENFILE" ]; then
    TOKEN_FILE_EXISTS=true
fi

if [ -n "$SERVER_RUNNING_PROCESS" ] && [ "$TOKEN_FILE_EXISTS" != true ]; then
    # Server is running but token file was deleted (e.g. by zombie-kill cleanup).
    # Try to recover the token from the process's command line instead of killing.
    RECOVERED_TOKEN=""
    RECOVER_PID="${SERVER_PID:-$(echo "$SERVER_RUNNING_PROCESS" | awk '{print $1}')}"
    if [ -n "$RECOVER_PID" ] && [ -f "/proc/$RECOVER_PID/cmdline" ]; then
        # /proc/PID/cmdline uses NUL separators; tr converts to newlines for grep
        # First try --connection-token-file (our default), then --connection-token (inline)
        TOKEN_FILE_PATH=$(tr '\0' '\n' < "/proc/$RECOVER_PID/cmdline" 2>/dev/null | grep -A1 '^--connection-token-file$' | tail -1)
        if [ -n "$TOKEN_FILE_PATH" ] && [ -f "$TOKEN_FILE_PATH" ]; then
            RECOVERED_TOKEN=$(cat "$TOKEN_FILE_PATH" 2>/dev/null)
        fi
        if [ -z "$RECOVERED_TOKEN" ]; then
            # Try inline --connection-token (value is the next argument)
            RECOVERED_TOKEN=$(tr '\0' '\n' < "/proc/$RECOVER_PID/cmdline" 2>/dev/null | grep -A1 '^--connection-token$' | tail -1)
        fi
    fi

    if [ -n "$RECOVERED_TOKEN" ]; then
        echo "Recovered connection token from running server process, recreating token file."
        echo "$RECOVERED_TOKEN" > "$SERVER_TOKENFILE"
        TOKEN_FILE_EXISTS=true
    else
        echo "Warning: Server is running but token file is missing and cannot recover token. Stopping server."
        KILL_PID="${SERVER_PID:-$(echo "$SERVER_RUNNING_PROCESS" | awk '{print $1}')}"
        if [ -n "$KILL_PID" ]; then
            kill "$KILL_PID" 2>/dev/null
        fi
        i=1
        while [ $i -le 10 ]; do
            if [ -z "$(ps -o pid= -p "$KILL_PID" 2>/dev/null)" ]; then
                break
            fi
            sleep 0.5
            i=$((i + 1))
        done
        if [ -n "$(ps -o pid= -p "$KILL_PID" 2>/dev/null)" ]; then
            echo "Warning: Server did not stop gracefully, forcing termination."
            kill -9 "$KILL_PID" 2>/dev/null
        fi
        SERVER_RUNNING_PROCESS=""
    fi
fi

if [ -z "$SERVER_RUNNING_PROCESS" ]; then
    # Clean up old files
    rm -f "$SERVER_LOGFILE" "$SERVER_TOKENFILE"

    # Create new token
    touch "$SERVER_TOKENFILE"
    chmod 600 "$SERVER_TOKENFILE"
    if [ -f "/proc/sys/kernel/random/uuid" ]; then
        SERVER_CONNECTION_TOKEN="$(cat /proc/sys/kernel/random/uuid)"
    else
        # Generate UUID on systems without /proc/sys/kernel/random/uuid
        SERVER_CONNECTION_TOKEN="$(uuidgen 2>/dev/null || date +%s%N)"
    fi
    echo "$SERVER_CONNECTION_TOKEN" > "$SERVER_TOKENFILE"
    if [ $? -ne 0 ] || [ ! -s "$SERVER_TOKENFILE" ]; then
        echo "Error: Failed to write connection token to file"
        release_server_lock
        print_install_results_and_exit 1005 "Failed to write to file: $SERVER_TOKENFILE"
    fi

    if [ -f "$SERVER_DATA_DIR/pre-server.sh" ]; then
        echo "Sourcing pre-server.sh..."
        source "$SERVER_DATA_DIR/pre-server.sh"
    fi

    # Start server
    SERVER_HOST="${TRAE_SERVER_HOST:-127.0.0.1}"
    SHUTDOWN_EXTRA=""
    if [ "$TRAE_SHUTDOWN_WITHOUT_DELAY" = "true" ]; then
        SHUTDOWN_EXTRA="--remote-auto-shutdown-without-delay"
    fi
    ICUBE_RUN_IN_REMOTE='true' vscode_base_dir="$SERVER_DATA_DIR" "$SERVER_DIR/node" $NODE_DNS_RESULT_ORDER "$SERVER_SCRIPT" --start-server --host=$SERVER_HOST $SERVER_LISTEN_FLAG $SERVER_INITIAL_EXTENSIONS --connection-token-file "$SERVER_TOKENFILE" --telemetry-level all --enable-remote-auto-shutdown $SHUTDOWN_EXTRA --accept-server-license-terms --default-folder ~ --server-data-dir "$MANAGER_DATA_DIR" --logsPath "$MANAGER_LOGS_DIR" > "$SERVER_LOGFILE" 2>&1 &
    echo $! > "$SERVER_PIDFILE"
fi

# Get token
if [ -f "$SERVER_TOKENFILE" ]; then
    SERVER_CONNECTION_TOKEN="$(cat "$SERVER_TOKENFILE")"
else
    echo "Error server token file not found $SERVER_TOKENFILE"
    release_server_lock
    print_install_results_and_exit 1004 "Server token file not found $SERVER_TOKENFILE"
fi

# Release lock now - the critical section (check/start server + write PID/token) is done.
# The port-wait loop below doesn't need mutual exclusion; other windows can safely
# acquire the lock, see the PID file, and also wait for the port independently.
release_server_lock

# Wait for server to start and get port info
i=1
while [ $i -le 20 ]; do
    if [ -f "$SERVER_LOGFILE" ]; then
        # Detect 'Trae server version too old' error
        if cat "$SERVER_LOGFILE" | grep -q "Current trae appVersion is too old"; then
            echo "ERROR: Current trae appVersion is too old"
            print_install_results_and_exit 3003 "Current trae appVersion is too old"
        fi

        LISTENING_ON="$(cat "$SERVER_LOGFILE" | grep -E 'Extension host agent listening on .+' | sed 's/Extension host agent listening on //')"
        if [ -n "$LISTENING_ON" ]; then
            if [ -f "/tmp/trae-region-detect.json" ]; then
                TRAE_REGION_DETECT_JSON="$(cat /tmp/trae-region-detect.json)"
            fi
            break
        fi
    fi
    sleep 0.5
    i=$((i + 1))
done

if ! [ -f "$SERVER_LOGFILE" ]; then
    echo "Error server log file not found $SERVER_LOGFILE"
    print_install_results_and_exit 1003 "Server log file not found $SERVER_LOGFILE"
fi

if [ -z "$LISTENING_ON" ]; then
    echo "Error server did not start successfully"
    print_install_results_and_exit 3001 "Server did not start successfully"
fi

# Complete server setup
print_install_results_and_exit 0
