#!/usr/bin/env bash
#
# Run darkchan-admin the way it has to be run: AS THE SERVICE USER, with the
# service stopped around anything that writes.
#
# Both of those are failure modes that leave no error behind, which is why this
# wrapper exists rather than a paragraph in the runbook:
#
#   - darkchan-admin as ROOT writes records the service cannot read, and the
#     account then vanishes from login with nothing logged (D-030). The tool
#     refuses outright now, which is how you find out.
#   - darkchan-admin behind a RUNNING server edits files on disk that the
#     server's next in-memory persist overwrites (OPERATIONS.md §3). Nothing
#     reports this; the change simply is not there afterwards.
#
# Read-only commands run against the live server and do not stop it.
#
# Installed by every deployment route and identical in each — the
# darkchan-server package puts it in /usr/sbin, the source route and
# packaging/deploy-vps.sh in /usr/local/sbin — and all of them configure it
# through /etc/default/darkchan-server, which the systemd unit reads too, so the
# wrapper and the server cannot end up disagreeing about where the store is.
#
set -euo pipefail

PROG=darkchan-adm
LIB=/usr/share/darkchan/deploy-lib.sh
[ -r "$LIB" ] || { echo "$PROG: $LIB is missing — reinstall darkchan-server" >&2
                   exit 1; }
# shellcheck source=packaging/deploy-lib.sh
. "$LIB"

CONF=/etc/default/darkchan-server
# shellcheck source=/dev/null
[ -r "$CONF" ] && . "$CONF"

: "${DARKCHAN_DATA:=/var/lib/darkchan}"
: "${DARKCHAN_USER:=darkchan}"
: "${DARKCHAN_UNIT:=darkchan-server}"
: "${DARKCHAN_ADMIN:=darkchan-admin}"
UNIT="$DARKCHAN_UNIT"   # svc_start_checked reads this

[ "$(id -u)" = 0 ] || {
  err "run me as root (sudo darkchan-adm ...) — I drop to '$DARKCHAN_USER'" \
      "myself"
  exit 1
}

run_admin() {
  as_user "$DARKCHAN_USER" "$DARKCHAN_ADMIN" --data "$DARKCHAN_DATA" "$@"
}

case "${1:-}" in
  ""|-h|--help) run_admin; exit $? ;;
esac

# ASK the CLI whether this command writes the store, rather than keeping a
# second list of read-only commands here (D-037). Two hand-maintained copies
# drift, and this particular drift is invisible: the wrapper would run a write
# against a live server, whose next save silently discards it.
#   0 = writes, 1 = read-only, anything else = an older CLI that does not know
#       the flag, or a command it does not recognise -> treat it as a write,
#       because a needless stop/start is recoverable and a clobber is not.
wrc=0
as_user "$DARKCHAN_USER" "$DARKCHAN_ADMIN" --is-store-write "$1" \
  >/dev/null 2>&1 || wrc=$?
if [ "$wrc" = 1 ]; then
  run_admin "$@"; exit $?
fi

# Everything else writes to the store.
active=0
if command -v systemctl >/dev/null 2>&1 &&
   systemctl is-active --quiet "$UNIT"; then
  active=1
  echo "darkchan-adm: stopping $UNIT (a CLI write behind a live server is" \
       "clobbered)"
  systemctl stop "$UNIT"
fi
rc=0
run_admin "$@" || rc=$?
if [ "$active" = 1 ]; then
  echo "darkchan-adm: starting $UNIT"
  svc_start_checked || rc=1
fi
exit "$rc"
