#!/usr/bin/env bash
# Start the Ollama server in the background. Never downloads anything.
. "$(dirname "$0")/common.sh"

if is_running; then echo "Already running (pid $(cat "$PIDFILE")) at $URL"; exit 0; fi
if curl -fsS "$URL/api/version" >/dev/null 2>&1; then
  echo "Port ${OLLAMA_HOST##*:} is used by another server; change OLLAMA_HOST in deepseek.env" >&2; exit 1
fi
if [[ ! -d "$OLLAMA_MODELS/manifests/registry.ollama.ai/library/${MODEL%%:*}" ]]; then
  echo "Model $MODEL not found in $OLLAMA_MODELS (run ./download.sh while online, or copy models/)" >&2; exit 1
fi

nohup setsid "$OLLAMA" serve >>"$LOG" 2>&1 </dev/null &
echo $! >"$PIDFILE"
wait_ready
echo "my-deepseek running (pid $(cat "$PIDFILE")) at $OLLAMA_HOST, model $MODEL"
