#!/bin/bash
# Run a disposable test instance of the LDR dev server.
#
# Points LDR_DATA_DIR at a temp directory so encrypted user DBs, the auth
# DB, library, logs, research_outputs etc. land outside your real data dir
# (~/.local/share/local-deep-research) and can be wiped in one shot.

set -eu

usage() {
    cat <<'EOF'
Run a disposable test instance of the LDR dev server.

Points LDR_DATA_DIR at a temp directory so encrypted user DBs, the auth
DB, library, logs, research_outputs etc. land outside your real data dir
(~/.local/share/local-deep-research) and can be wiped in one shot.

Usage:
  scripts/dev/run_test_instance.sh           # start (existing test data kept)
  scripts/dev/run_test_instance.sh --reset   # wipe the test data dir first
  scripts/dev/run_test_instance.sh --debug   # start with DEBUG logging

--reset and --debug can be combined. Override the location
(otherwise defaults to /tmp/ldr-test):
  LDR_DATA_DIR=$HOME/ldr-test scripts/dev/run_test_instance.sh
EOF
}

reset=0
restart=restart_server.sh
for arg in "$@"; do
    case "$arg" in
        --reset)   reset=1 ;;
        --debug)   restart=restart_server_debug.sh ;;
        -h|--help) usage; exit 0 ;;
        *)         echo "Unknown argument: $arg" >&2; usage >&2; exit 2 ;;
    esac
done

: "${LDR_DATA_DIR:=/tmp/ldr-test}"
export LDR_DATA_DIR

if [ "$reset" -eq 1 ]; then
    # Canonicalize first (-m so a not-yet-created dir still resolves) so a
    # symlinked or relative LDR_DATA_DIR can't slip a real location past the
    # guard, then refuse paths that obviously aren't a disposable test dir.
    # Keeps a misconfigured LDR_DATA_DIR from turning --reset into an
    # `rm -rf` of something real.
    real=$(realpath -m -- "$LDR_DATA_DIR")
    home=$(realpath -m -- "${HOME:-/nonexistent}")
    case "$real" in
        "/"|"$home"|"$home/.local/share/local-deep-research"|"/tmp")
            echo "Refusing to --reset LDR_DATA_DIR='$LDR_DATA_DIR' -> '$real' (looks unsafe)." >&2
            exit 1
            ;;
    esac
    echo "Resetting test data dir: $real"
    rm -rf -- "$real"
fi

mkdir -p "$LDR_DATA_DIR"
echo "Using LDR_DATA_DIR=$LDR_DATA_DIR"

exec "$(dirname "$0")/$restart"
