#!/bin/sh
#
# Copyright (c) 2026 Bryan C. Everly
# SPDX-License-Identifier: BSD-2-Clause
#
# restate hook: the UniFi Network application, for capture --quiesce-with unifi.
#
#   present NAME  whether UniFi is on this host: prints present or absent
#   paths NAME    the directories UniFi keeps its data in, one a line: what the
#                 capture copies while UniFi is stopped
#   pause NAME    dump its database into $RESTATE_DUMP_DIR (mongodump, where it
#                 is installed, from the database UniFi runs itself, on port
#                 27117), then stop the unifi service
#   resume NAME   start it again
#
# UniFi runs a MongoDB of its own, started by its own service rather than as
# mongod's, which is why the mongodb hook cannot pause it. Its own backups --
# /var/lib/unifi/backup/autobackup/*.unf -- are in the capture too, and
# restore onto a new UniFi without restate.
#
# It is stopped as well as dumped because its MongoDB runs alone, not in a
# replica set, so a dump taken while it runs is not of one moment (there is
# no --oplog to make it one); the files copied while it is stopped are. Only
# the controller is down meanwhile: the access points and switches it manages
# go on forwarding without it.
#
# To change what this does, copy it to /etc/restate/hooks.d/unifi (root's,
# mode 0755) and edit the copy: that one is used in place of this.
set -eu

# Before anything is made: present only looks.
if [ "$1" = present ]; then
  if [ -d /usr/lib/unifi ]; then
    echo present
  else
    echo absent
  fi
  exit 0
fi

action=$1
port=27117

case "$action" in
paths)
  for dir in /var/lib/unifi /usr/lib/unifi/data; do
    if [ -d "$dir" ]; then
      echo "$dir"
    fi
  done
  ;;
pause)
  if [ -n "${RESTATE_DUMP_DIR:-}" ] && command -v mongodump > /dev/null 2>&1; then
    mongodump --quiet --port "$port" --gzip --archive="$RESTATE_DUMP_DIR/unifi.archive.gz.tmp"
    mv -f "$RESTATE_DUMP_DIR/unifi.archive.gz.tmp" "$RESTATE_DUMP_DIR/unifi.archive.gz"
  fi
  systemctl stop unifi
  ;;
resume)
  systemctl start unifi
  ;;
restored)
  # Asked of every hook after a restore: nothing to do for it here, which
  # exit 2 says without a word.
  exit 2
  ;;
*)
  echo "usage: $0 present|paths|pause|resume NAME" >&2
  exit 2
  ;;
esac
