#!/usr/bin/env bash
# CodeLynq runner installer for Ubuntu and macOS.
#
#   curl -fsSL https://get.codelynq.com/runner | bash
#
# Installs the runner into an isolated virtualenv under ~/.codelynq-runner,
# links the `codelynq-runner` command into ~/.local/bin, then launches the
# interactive `init` (config + optional boot service). Pass --token / --url to
# run unattended.
set -euo pipefail

REPO_RAW="${CODELYNQ_RUNNER_SRC:-https://get.codelynq.com}"
INSTALL_DIR="${CODELYNQ_RUNNER_HOME:-$HOME/.codelynq-runner}"
BIN_DIR="$HOME/.local/bin"
TOKEN="" ; URL="" ; NONINTERACTIVE=0

while [ $# -gt 0 ]; do
  case "$1" in
    --token) TOKEN="$2"; shift 2 ;;
    --url) URL="$2"; shift 2 ;;
    --token=*) TOKEN="${1#*=}"; shift ;;
    --url=*) URL="${1#*=}"; shift ;;
    --yes|-y) NONINTERACTIVE=1; shift ;;
    *) echo "unknown option: $1" >&2; exit 2 ;;
  esac
done

say() { printf '\033[1;36m==>\033[0m %s\n' "$1"; }
err() { printf '\033[1;31mError:\033[0m %s\n' "$1" >&2; }

OS="$(uname -s)"
case "$OS" in
  Linux|Darwin) ;;
  *) err "Unsupported OS: $OS (Ubuntu and macOS only)"; exit 1 ;;
esac

# Python 3.9+
PY=""
for c in python3 python; do
  if command -v "$c" >/dev/null 2>&1 && "$c" -c 'import sys; sys.exit(0 if sys.version_info>=(3,9) else 1)' 2>/dev/null; then
    PY="$c"; break
  fi
done
if [ -z "$PY" ]; then
  err "Python 3.9+ is required. Install it (macOS: brew install python; Ubuntu: sudo apt install python3 python3-venv) and re-run."
  exit 1
fi
say "Using $($PY --version)"

# Fetch package sources into the install dir
say "Downloading runner into $INSTALL_DIR"
mkdir -p "$INSTALL_DIR/codelynq_runner"
for f in __init__.py __main__.py cli.py config.py core.py environment.py service.py; do
  curl -fsSL "$REPO_RAW/codelynq_runner/$f" -o "$INSTALL_DIR/codelynq_runner/$f"
done
curl -fsSL "$REPO_RAW/pyproject.toml" -o "$INSTALL_DIR/pyproject.toml"
[ -f "$REPO_RAW/README.md" ] && true
curl -fsSL "$REPO_RAW/README.md" -o "$INSTALL_DIR/README.md" 2>/dev/null || echo "CodeLynq runner" > "$INSTALL_DIR/README.md"

# Isolated venv + install
say "Creating virtualenv"
if ! "$PY" -m venv "$INSTALL_DIR/venv" 2>/tmp/clr-venv-err; then
  cat /tmp/clr-venv-err >&2 || true
  PYVER="$("$PY" -c 'import sys;print(f"{sys.version_info.major}.{sys.version_info.minor}")')"
  err "Could not create a virtualenv. Install the venv module and re-run:"
  if [ "$OS" = "Linux" ]; then
    echo "  sudo apt-get install -y python${PYVER}-venv   # Debian/Ubuntu" >&2
    echo "  (or: sudo dnf install python3-virtualenv)" >&2
  else
    echo "  Reinstall Python from python.org or 'brew install python' (venv is bundled)." >&2
  fi
  exit 1
fi
"$INSTALL_DIR/venv/bin/pip" install --quiet --upgrade pip >/dev/null 2>&1 || true
say "Installing codelynq-runner"
"$INSTALL_DIR/venv/bin/pip" install --quiet "$INSTALL_DIR"

# Link the CLI onto PATH
mkdir -p "$BIN_DIR"
ln -sf "$INSTALL_DIR/venv/bin/codelynq-runner" "$BIN_DIR/codelynq-runner"
say "Linked codelynq-runner -> $BIN_DIR"

case ":$PATH:" in
  *":$BIN_DIR:"*) ;;
  *) printf '\033[1;33mNote:\033[0m add %s to your PATH:\n  export PATH="%s:$PATH"\n' "$BIN_DIR" "$BIN_DIR" ;;
esac

# Hand off to init
INIT_ARGS=()
[ -n "$URL" ] && INIT_ARGS+=(--url "$URL")
[ -n "$TOKEN" ] && INIT_ARGS+=(--token "$TOKEN")
if [ "$NONINTERACTIVE" = "1" ]; then
  INIT_ARGS+=(--service)
fi

say "Starting setup"
if [ -t 0 ] || [ "$NONINTERACTIVE" = "1" ]; then
  exec "$BIN_DIR/codelynq-runner" init "${INIT_ARGS[@]}"
else
  echo "Installed. Finish setup with:  codelynq-runner init"
fi
