#!/bin/sh
# connector-entrypoint.sh — Docker entrypoint for the BYO-CCTV connector. On first
# run it pairs with a short-lived claim code (exchanging it for a per-feed device
# token, which is NEVER baked into the image) and writes the paired config;
# thereafter it senses. Mount /app/data on a volume so the paired config survives
# restarts — re-pairing needs a fresh code from the portal.
#
# ── LOCAL INFERENCE ONLY ───────────────────────────────────────────────────
#
# This entrypoint used to assert `localSensing.allowCloudFallback: true` into the
# paired config on EVERY start, so a plain `docker run` sent every analyzed frame
# to the cloud VLM. It was one of three places that forced the cloud path (the
# systemd unit hardcoded `cloud-sense`; `TRILLBOARDS_SENSE_MODE` overrode at run
# time). All three are gone.
#
# WHY. Measured 2026-08-12 over 255,812 image-bearing cloud VLM calls: $0.007008
# per analyzed frame — ~$605/camera/month at a 30s cadence, ~$9,688/month for a
# 16-camera store against a $99-500 subscription. The product was ~10x underwater
# at scale. On-device inference has no per-frame cost.
#
# This SUPERSEDES the escalation architecture from #11971 ("local Gemma decides
# *when* to look, the cloud decides *what* it saw") and the frequency-based cost
# control in plans/cctv-byo-sensing-plan.md:26. Cost is no longer managed by
# looking less often; the metered path is removed.
#
# ── What this image needs from you ────────────────────────────────────────
#
# It bundles no model, so it needs a litert-lm serve it can reach:
#   docker run --network host ...              # serve on localhost:9379
#   # or set localSensing.litertLmBaseUrl to http://host.docker.internal:9379
#
# Without one, `auto-sense` probes and WAITS: it analyzes nothing and uploads no
# FRAMES. It is not dark, though — it runs the same manager in health-only mode,
# so the cameras open, video is proven, and camera health reports on the
# heartbeat with sensingState='unavailable'. The portal sees a live connector
# that cannot sense yet, rather than one indistinguishable from a dead machine.
#
# TRILLBOARDS_SENSE_MODE selects auto-sense (probe, then local-or-wait) or
# local-sense (require local, no probe loop). `cloud-sense` is rejected BY NAME
# rather than ignored, so an existing deployment pinning it stops with a message
# instead of silently getting different behaviour.
set -eu

CONFIG="${TRILLBOARDS_CONFIG:-/app/data/trillboards.config.json}"
API_BASE="${TRILLBOARDS_API_BASE:-https://api.trillboards.com}"
CLI="node packages/edge-sdk/dist/cli.js"
MODE="${TRILLBOARDS_SENSE_MODE:-auto-sense}"

case "$MODE" in
  auto-sense|local-sense) ;;
  cloud-sense)
    echo "[connector] TRILLBOARDS_SENSE_MODE=cloud-sense is no longer supported." >&2
    echo "[connector] The connector is local-inference only: cloud VLM inference cost" >&2
    echo "[connector] \$0.007008/frame (~\$9,688/month for a 16-camera store), so the path" >&2
    echo "[connector] was removed rather than rate-limited." >&2
    echo "[connector] Use auto-sense (the default) and give the container a litert-lm:" >&2
    echo "[connector]   docker run --network host ...   # serve on localhost:9379" >&2
    exit 1
    ;;
  *)
    echo "[connector] Unknown TRILLBOARDS_SENSE_MODE '$MODE' (want auto-sense|local-sense)." >&2
    exit 1
    ;;
esac

if [ ! -f "$CONFIG" ]; then
  if [ -z "${TRILLBOARDS_CLAIM_CODE:-}" ]; then
    echo "[connector] No paired config and no TRILLBOARDS_CLAIM_CODE." >&2
    echo "[connector] Get a code from the Trillboards portal (Add a camera -> Install), then:" >&2
    echo "[connector]   docker run --network host -e TRILLBOARDS_CLAIM_CODE=XXXX-XXXX -v trillboards:/app/data ghcr.io/trillboards/edge-sdk:auto-sense" >&2
    exit 1
  fi
  echo "[connector] Pairing with claim code (token delivered at pair, never baked)..."
  # shellcheck disable=SC2086
  $CLI pair --code "$TRILLBOARDS_CLAIM_CODE" --config "$CONFIG"  --api-base "$API_BASE"
fi

if [ "$MODE" = "auto-sense" ]; then
  echo "[connector] Starting $MODE (API $API_BASE) — probing for on-device inference." >&2
  echo "[connector] This image bundles no model. If no litert-lm is reachable the" >&2
  echo "[connector] connector analyzes nothing and uploads no frames — but it still" >&2
  echo "[connector] opens the cameras and reports their health, so the portal shows it" >&2
  echo "[connector] alive. It starts sensing automatically once a litert-lm appears." >&2
else
  echo "[connector] Starting explicitly selected $MODE (API $API_BASE)..." >&2
fi
# shellcheck disable=SC2086
exec $CLI "$MODE" --config "$CONFIG"
