#!/bin/bash
set -euo pipefail

HOST="root@ollama-serve.loc"
OVERRIDE_DIR="/etc/systemd/system/ollama.service.d"
OVERRIDE_FILE="${OVERRIDE_DIR}/override.conf"

for arg in "$@"; do
  case "$arg" in
    --help|-h)
      cat <<'EOF'
Usage: update-ollama [options]

SSH to ollama-serve.loc, pull latest Ollama, and apply environment overrides
via a systemd drop-in so they survive upgrades.

Options:
  -h, --help    Show this help message and exit
EOF
      exit 0
      ;;
  esac
done

ssh "${HOST}" bash -s <<'ENDSSH'
set -euo pipefail

echo "=== Updating Ollama ==="
curl -fsSL https://ollama.com/install.sh | sh

echo ""
echo "=== Applying environment overrides (systemd drop-in) ==="
mkdir -p /etc/systemd/system/ollama.service.d
cat > /etc/systemd/system/ollama.service.d/override.conf <<'EOF'
[Service]
Environment="OLLAMA_CONTEXT_LENGTH=16384"
Environment="OLLAMA_HOST=0.0.0.0"
EOF

systemctl daemon-reload
systemctl restart ollama
echo "Done."
ENDSSH
