#!/bin/bash
set -euo pipefail

# Detect musl (Alpine) vs glibc — NVM doesn't support musl, so use apk for Alpine
if ldd --version 2>&1 | grep -qi musl || [ -f /etc/alpine-release ]; then
    echo "Detected Alpine/musl — installing Node.js via apk"
    apk add --no-cache curl bash nodejs npm ripgrep

    {% if version %}
    npm install -g @openai/codex@{{ version }}
    {% else %}
    npm install -g @openai/codex@latest
    {% endif %}
else
    # glibc system — use NVM
    # Install curl
    if command -v apt-get &>/dev/null; then
        apt-get update && apt-get install -y curl ripgrep
    elif command -v yum &>/dev/null; then
        yum install -y curl ripgrep
    elif command -v apk &>/dev/null; then
        apk add --no-cache curl bash ripgrep
    else
        echo "Warning: No known package manager found, assuming curl is available" >&2
    fi

    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.2/install.sh | bash

    export NVM_DIR="$HOME/.nvm"
    # Source nvm with || true to handle nvm.sh's internal non-zero returns
    \. "$NVM_DIR/nvm.sh" || true
    # Verify NVM loaded successfully
    command -v nvm &>/dev/null || { echo "Error: NVM failed to load" >&2; exit 1; }

    nvm install 22
    nvm alias default 22
    npm -v

    {% if version %}
    npm install -g @openai/codex@{{ version }}
    {% else %}
    npm install -g @openai/codex@latest
    {% endif %}
fi

# Symlink node/codex to /usr/local/bin so they're always on PATH
# regardless of whether nvm.sh is sourced in later sessions.
# Skip if already in /usr/local/bin (e.g. Alpine's apk puts them there).
for bin in node codex; do
    BIN_PATH="$(which "$bin")"
    if [ "$BIN_PATH" != "/usr/local/bin/$bin" ]; then
        ln -sf "$BIN_PATH" "/usr/local/bin/$bin"
    fi
done

codex --version
