#!/usr/bin/env bash
# cron-vnc-check.sh — dipanggil cron Hermes tiap 10 menit (watchdog pattern)
VD="${HOME:-/opt/data/home}/vncsetup"
RUN="$VD/run"
report=""
out=$("$VD/start-vnc.sh" --quiet 2>&1)
[ -n "$out" ] && report="$report$out"$'\n'
if [ -f "$RUN/watchdog.pid" ]; then
  wpid=$(cat "$RUN/watchdog.pid" 2>/dev/null)
  if [ -z "$wpid" ] || ! kill -0 "$wpid" 2>/dev/null; then
    out=$("$VD/vnc-watchdog.sh" 2>&1)
    [ -n "$out" ] && report="$report$out"$'\n'
  fi
else
  out=$("$VD/vnc-watchdog.sh" 2>&1)
  [ -n "$out" ] && report="$report$out"$'\n'
fi
if [ -n "$report" ]; then
  echo "[vnc-cron] $(date '+%F %T') — ada yang diperbaiki:"
  echo "$report"
fi

# ── portal session watchdog ─────────────────────────────────────────────────
# Deteksi otomatis login/logout job portal (cookie browser) → sync status ke BE.
if [ -f /opt/data/jobseeker/portal-check.py ] && [ -x /opt/data/venv-cdp/bin/python ]; then
  pout=$(/opt/data/venv-cdp/bin/python /opt/data/jobseeker/portal-check.py 2>&1)
  [ -n "$pout" ] && echo "$pout"
fi
exit 0
