#!/usr/bin/env bash
#
# e2e-local.sh — graft this repo's drive9-server onto mem9-ai/drive9 e2e.
#
# Builds bin/drive9-server here, then runs drive9's scripts/e2e-local.sh
# (TiDB + server + smoke-all) with DRIVE9_SERVER_BIN pointing at that binary.
#
# Usage:
#   make e2e-local
#   bash scripts/e2e-local.sh
#   bash scripts/e2e-local.sh --keep-server
#   bash scripts/e2e-local.sh --no-build
#   DRIVE9_UPSTREAM_DIR=/path/to/drive9 bash scripts/e2e-local.sh
#   RUN_FUSE_SMOKE=0 bash scripts/e2e-local.sh
#
# Upstream tree:
#   DRIVE9_UPSTREAM_DIR   existing drive9 checkout (skips clone)
#   DRIVE9_UPSTREAM_REPO  default https://github.com/mem9-ai/drive9.git
#   DRIVE9_UPSTREAM_REF   optional git branch or tag to clone (default branch)
#   DRIVE9_GITHUB_TOKEN   optional; used as x-access-token for HTTPS clone
#
# Defaults if unset (drive9's own local-e2e leaves tokens/sse off):
#   RUN_JOURNAL_SMOKE=1
#   RUN_POSIX_SMOKE=1
#   RUN_TOKENS_SMOKE=1
#   RUN_SSE_SMOKE=1
#   RUN_FUSE_SMOKE             unset → drive9 default (1). Set 0 to skip.
#
# Remaining flags (--keep-server, --keep-db, RUN_API_ONLY, …) are forwarded
# to drive9's scripts/e2e-local.sh.
#
# Compatible with macOS bash 3.2.

set -euo pipefail

ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ROOT"

SERVER_BIN="${DRIVE9_SERVER_BIN:-$ROOT/bin/drive9-server}"
UPSTREAM_REPO="${DRIVE9_UPSTREAM_REPO:-https://github.com/mem9-ai/drive9.git}"
UPSTREAM_REF="${DRIVE9_UPSTREAM_REF:-}"
DO_BUILD=1
DRIVE9_DIR=""
CLONED_DIR=""
FORWARD=()

while [ $# -gt 0 ]; do
  case "$1" in
    --no-build) DO_BUILD=0; shift ;;
    -h|--help)
      sed -n '2,/^set -euo pipefail$/p' "$0" | sed '/^set -euo pipefail$/d; s/^# \{0,1\}//'
      exit 0 ;;
    *) FORWARD+=("$1"); shift ;;
  esac
done

cleanup() {
  if [ -n "$CLONED_DIR" ]; then
    rm -rf "$CLONED_DIR" || true
  fi
}
trap cleanup EXIT
trap 'cleanup; exit 130' INT TERM

log() { printf '\n>>> %s\n' "$*"; }
have() { command -v "$1" >/dev/null 2>&1; }

need() {
  if ! have "$1"; then
    echo "missing required command: $1" >&2
    exit 1
  fi
}

clone_url() {
  local token="${DRIVE9_GITHUB_TOKEN:-${GITHUB_TOKEN:-}}"
  case "$UPSTREAM_REPO" in
    https://github.com/*)
      if [ -n "$token" ]; then
        printf 'https://x-access-token:%s@github.com/%s' "$token" "${UPSTREAM_REPO#https://github.com/}"
        return
      fi
      ;;
  esac
  printf '%s' "$UPSTREAM_REPO"
}

build_server() {
  if [ -n "${DRIVE9_SERVER_BIN:-}" ]; then
    if [ ! -x "$SERVER_BIN" ]; then
      echo "DRIVE9_SERVER_BIN is not executable: $SERVER_BIN" >&2
      exit 1
    fi
    log "using DRIVE9_SERVER_BIN=$SERVER_BIN"
    return
  fi
  if [ "$DO_BUILD" -eq 0 ] && [ -x "$SERVER_BIN" ]; then
    log "reusing existing $SERVER_BIN (--no-build)"
    return
  fi
  log "building drive9-server"
  make build-server
  SERVER_BIN="$ROOT/bin/drive9-server"
}

ensure_drive9() {
  if [ -n "${DRIVE9_UPSTREAM_DIR:-}" ]; then
    if [ ! -x "$DRIVE9_UPSTREAM_DIR/scripts/e2e-local.sh" ]; then
      echo "DRIVE9_UPSTREAM_DIR=$DRIVE9_UPSTREAM_DIR is missing scripts/e2e-local.sh" >&2
      exit 1
    fi
    DRIVE9_DIR="$DRIVE9_UPSTREAM_DIR"
    log "using drive9 checkout $DRIVE9_DIR"
    return
  fi
  need git
  CLONED_DIR="$(mktemp -d -t drive9-e2e-upstream-XXXXXX)"
  DRIVE9_DIR="$CLONED_DIR"
  if [ -n "$UPSTREAM_REF" ]; then
    log "cloning $UPSTREAM_REPO (ref $UPSTREAM_REF)"
    git clone --depth 1 --branch "$UPSTREAM_REF" "$(clone_url)" "$DRIVE9_DIR"
  else
    log "cloning $UPSTREAM_REPO (default branch)"
    git clone --depth 1 "$(clone_url)" "$DRIVE9_DIR"
  fi
}

: "${RUN_JOURNAL_SMOKE:=1}"
: "${RUN_POSIX_SMOKE:=1}"
: "${RUN_TOKENS_SMOKE:=1}"
: "${RUN_SSE_SMOKE:=1}"
export RUN_JOURNAL_SMOKE RUN_POSIX_SMOKE RUN_TOKENS_SMOKE RUN_SSE_SMOKE

need curl
need python3
need jq

build_server
if [ ! -x "$SERVER_BIN" ]; then
  echo "drive9-server is not executable: $SERVER_BIN" >&2
  exit 1
fi
export DRIVE9_SERVER_BIN="$SERVER_BIN"

ensure_drive9
if have git && [ -d "$DRIVE9_DIR/.git" ]; then
  log "drive9 HEAD $(git -C "$DRIVE9_DIR" rev-parse HEAD) $(git -C "$DRIVE9_DIR" log -1 --format=%s)"
fi

log "running drive9 scripts/e2e-local.sh --no-build (DRIVE9_SERVER_BIN=$DRIVE9_SERVER_BIN RUN_TOKENS_SMOKE=$RUN_TOKENS_SMOKE RUN_SSE_SMOKE=$RUN_SSE_SMOKE RUN_JOURNAL_SMOKE=$RUN_JOURNAL_SMOKE RUN_POSIX_SMOKE=$RUN_POSIX_SMOKE RUN_FUSE_SMOKE=${RUN_FUSE_SMOKE:-1})"
if [ "${#FORWARD[@]}" -gt 0 ]; then
  bash "$DRIVE9_DIR/scripts/e2e-local.sh" --no-build "${FORWARD[@]}"
else
  bash "$DRIVE9_DIR/scripts/e2e-local.sh" --no-build
fi
