#!/bin/ksh

daemon="/usr/local/sbin/reed2"
daemon_flags="-f /etc/reed2/probes"
# stdout and stderr of the VM -- a startup error, the reports of the OTP logger on a crashed
# process or a supervisor giving up -- to syslog: rc_exec appends "2>&1 | logger -isp
# daemon.info -t reed2", rc_bg=YES required [verifie:openbsd/src etc/rc.d/rc.subr 1.168:184-193,
# 243-244]. Set before rc.subr, which lets reed2_logger of rc.conf.local override it
# [verifie:rc.subr:367, 385], as sysutils/telegraf does [verifie:openbsd/ports
# sysutils/telegraf/pkg/telegraf.rc]. info, not notice: daemon.info goes to /var/log/daemon,
# 0640, and *.notice to /var/log/messages, 0644 [verifie:openbsd/src etc/syslog.conf:4, 9;
# etc/newsyslog.conf:8, 11]. The ntfy topic is not in these lines: reed2 holds it behind a fun,
# and a logger filter strikes it from every event of the VM (Reed2.Redact).
daemon_logger="daemon.info"
daemon_user="_reed"

. /etc/rc.d/rc.subr

rc_bg=YES

# The escript is not the process that stays: escript execs erl, erl execs the emulator, and the
# script path with its flags ends up after -extra [verifie:erlang/otp erts/etc/common/escript.c
# main, PUSH("-extra")]. The emulator keeps the script path as argv[0], "beam" appears nowhere:
# "<prefix>/sbin/reed2 -B -fnu -- -root ... -run escript start -- -- -extra
# <prefix>/sbin/reed2 -f /etc/reed2/probes", <prefix> being /usr/local
# [constate:dev, pgrep -lfu _reed, 2026-09-26].
# rc.subr matches "daemon flags" whole through pgrep -xf [verifie:openbsd/src etc/rc.d/rc.subr
# rc_check, and its last line setting pexp]: same head, same tail, anything between.
# daemon_logger adds two processes beside the VM, neither matched: the shell of su that runs the
# pipeline, argv "sh -c ...", no dash under -fl [verifie:rc.subr:176-193; openbsd/src
# usr.bin/su/su.c:260-263, 333-338], and "logger -isp daemon.info -t reed2"; -x wants the match
# to span the whole argv [verifie:usr.bin/pkill/pkill.c:303-313]. stop and reload signal the VM
# alone; logger reads until EOF, then exits [verifie:usr.bin/logger/logger.c:123-126].
pexp="${daemon} .* -extra ${pexp}"

rc_cmd $1
