#!/usr/bin/env bash
# Shared setup for my-deepseek scripts. Paths are relative to this directory.
set -euo pipefail
BASE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
set -a
# shellcheck disable=SC1091
. "$BASE/deepseek.env"
set +a
export OLLAMA_MODELS="$BASE/models"
export PATH="$BASE/runtime/bin:$PATH"
OLLAMA="$BASE/runtime/bin/ollama"
PIDFILE="$BASE/run/ollama.pid"
LOG="$BASE/logs/server.log"
URL="http://${OLLAMA_HOST/0.0.0.0/127.0.0.1}"
mkdir -p "$BASE/logs" "$BASE/run" "$BASE/models"

is_running() { [[ -f $PIDFILE ]] && kill -0 "$(cat "$PIDFILE")" 2>/dev/null; }

wait_ready() {
  for _ in $(seq 1 60); do
    curl -fsS "$URL/api/version" >/dev/null 2>&1 && return 0
    sleep 1
  done
  echo "Server did not become ready; see $LOG" >&2
  return 1
}
