#!/usr/bin/env bash
set -euo pipefail

LLAMA_SERVER="${LLAMA_SERVER:-/root/model-spikes/llama.cpp/build/bin/llama-server}"
MODEL_DIR="${TIMMY_MODEL_DIR:-/root/model-spikes/models/smolvlm2-2.2b}"
MODEL="${TIMMY_MODEL_FILE:-$MODEL_DIR/SmolVLM2-2.2B-Instruct-Q4_K_M.gguf}"
MMPROJ="${TIMMY_MMPROJ_FILE:-$MODEL_DIR/mmproj-SmolVLM2-2.2B-Instruct-Q8_0.gguf}"
HOST="${TIMMY_MODEL_HOST:-127.0.0.1}"
PORT="${TIMMY_MODEL_PORT:-8080}"
THREADS="${TIMMY_MODEL_THREADS:-4}"

for file in "$LLAMA_SERVER" "$MODEL" "$MMPROJ"; do
  if [[ ! -f "$file" ]]; then
    printf 'Missing required file: %s\n' "$file" >&2
    exit 1
  fi
done

exec "$LLAMA_SERVER" \
  --model "$MODEL" \
  --mmproj "$MMPROJ" \
  --alias SmolVLM2-2.2B-Instruct \
  --host "$HOST" \
  --port "$PORT" \
  --ctx-size 4096 \
  --parallel 1 \
  --threads "$THREADS" \
  --no-webui
