#!/usr/bin/env bash
# kb-ingest-run.sh — thin launcher for kb-ingest.service (module 5 phase 3 step 5,
# docs/kb/modules/05-faza3-plan.md §7.1).
#
# All sequencing/tolerance/exit-code logic lives in documents-ingest-cyclic (Python,
# jobs/documents-ingest/src/documents_ingest/cyclic_ingest.py) — this script only computes
# a date-stamped log path and redirects, per the repo convention that a long run must never
# log only to a terminal/journal (lesson from the 2026-07 backfill that lost 4999 rows to a
# closed tmux — see the kb-piha-job-execution memory note).
#
# Installed at /opt/homelab/kb/kb-ingest-run.sh (copied from repo at deploy time), invoked
# by kb-ingest.service. `exec` at the end preserves documents-ingest-cyclic's exit code as
# this script's own, so systemd sees the real success/failure.
set -euo pipefail

LOG_DIR="/opt/homelab/logs/kb-ingest"
mkdir -p "$LOG_DIR"
LOG_FILE="${LOG_DIR}/run-$(date +%Y%m%d).log"

exec /opt/homelab/kb/venv/bin/documents-ingest-cyclic --apply >> "$LOG_FILE" 2>&1
