#!/usr/bin/env bash
# New replica only. Never replaces a container, stops a process or changes power.
set -euo pipefail
mode=${1:-small}
case "$mode" in small) context=8192; slots=1;; full) context=786432; slots=3;; *) exit 2;; esac
: "${ATLAS_GPU_UUID:?Set the GPU UUID of the replica}"
: "${ATLAS_MODEL_DIR:?Set the directory containing the verified GGUF}"
image=${ATLAS_LLAMA_IMAGE:-lomelidev/llama.cpp@sha256:437ae974458c543cfa8c3566de5146e2b253d0e17ed8bf8fcfd64c250cc9e5bc}
test -f "$ATLAS_MODEL_DIR/Ornith-1.5-9B-Q4_K_L.gguf"
used=$(nvidia-smi -i "$ATLAS_GPU_UUID" --query-gpu=memory.used --format=csv,noheader,nounits)
if [[ ! "$used" =~ ^[0-9]+$ ]] || (( used > 1024 )); then
  echo 'GPU ocupada o consulta inválida; no se inicia otro modelo.' >&2; exit 1
fi
if ss -Hln 'sport = :8120' | grep -q .; then
  echo 'Puerto 8120 ocupado; no se cambia el servicio existente.' >&2; exit 1
fi
if docker container inspect ornith-replica >/dev/null 2>&1; then
  echo 'Ya existe ornith-replica; inspeccionarlo antes de crear otro.' >&2; exit 1
fi
docker image inspect "$image" >/dev/null
docker run -d --name ornith-replica --gpus "device=$ATLAS_GPU_UUID" --ipc=host \
  -p 127.0.0.1:8120:8080 -v "$ATLAS_MODEL_DIR:/models:ro" "$image" \
  -m /models/Ornith-1.5-9B-Q4_K_L.gguf -a ornith-local \
  -ngl 999 -fa on -ctk q4_0 -ctv q4_0 -c "$context" -np "$slots" \
  --no-kv-unified --cont-batching --no-mmproj --n-predict 4096 \
  -b 4096 -ub 2048 --cache-ram 32768 --cache-prompt --ctx-checkpoints 4 \
  --jinja --metrics --host 0.0.0.0 --port 8080 \
  --temp 0.6 --top-p 0.95 --top-k 20 --min-p 0
