#!/bin/sh
#
# Copyright (c) 2026 Bryan C. Everly
# SPDX-License-Identifier: BSD-2-Clause
#
# restate hook: Nextcloud, for capture --quiesce-with nextcloud.
#
#   present NAME  whether Nextcloud is on this host: prints present or absent
#   paths NAME    Nextcloud's own directory and its data directory, one a
#                 line: what the capture copies while it is in maintenance mode
#   pause NAME    put it in maintenance mode, so that no one changes a file
#                 while it is copied
#   resume NAME   take it out of maintenance mode
#   restored NAME after a restore: config/config.php put back as it was before
#                 the pause, which the capture copied in maintenance mode
#
# The pause keeps config.php as it was before it, and as it was once paused,
# in $RESTATE_DUMP_DIR; "restored" puts the first back over the restored one
# only where that is the second exactly -- what the capture copied -- so a
# config.php changed since, or put back from somewhere else, is left alone.
#
# It is found as the snap (nextcloud.occ), or as the first directory with occ
# and config/config.php in it beneath /var/www, /usr/local/www or /srv/www;
# for one elsewhere, set NEXTCLOUD below in a copy of this hook. occ is run as
# the user config/config.php belongs to: the web server's.
#
# Its database, a MySQL, MariaDB or PostgreSQL server of the system's, is
# paused by that database's own hook when the capture is --quiesce's as well:
# capture with both. The snap's database is its own, and is dumped into
# $RESTATE_DUMP_DIR here (nextcloud.mysqldump) while it is in maintenance mode.
#
# To change what this does, copy it to /etc/restate/hooks.d/nextcloud (root's,
# mode 0755) and edit the copy: that one is used in place of this.
set -eu

action=$1
NEXTCLOUD=

snapped() {
  command -v nextcloud.occ > /dev/null 2>&1
}

# Sets NEXTCLOUD to the first directory holding occ and config/config.php;
# fails if there is none.
find_nextcloud() {
  if [ -n "$NEXTCLOUD" ]; then
    return 0
  fi
  for occ in /var/www/*/occ /usr/local/www/*/occ /srv/www/*/occ; do
    if [ -f "$occ" ] && [ -f "${occ%/occ}/config/config.php" ]; then
      NEXTCLOUD=${occ%/occ}
      return 0
    fi
  done
  return 1
}

# occ, as the user Nextcloud's configuration belongs to: the web server's.
occ() {
  if snapped; then
    nextcloud.occ "$@"
    return
  fi
  config="$NEXTCLOUD/config/config.php"
  owner="$(stat -c %U "$config" 2> /dev/null || stat -f %Su "$config")"
  if command -v runuser > /dev/null 2>&1; then
    runuser -u "$owner" -- php "$NEXTCLOUD/occ" "$@"
  else
    # The single quotes are the point: the shell su starts expands them, from
    # the arguments after it, so nothing here is parsed twice.
    # shellcheck disable=SC2016
    su -s /bin/sh "$owner" -c 'php "$0" "$@"' "$NEXTCLOUD/occ" "$@"
  fi
}

# config/config.php's real path, symlinks and all resolved.
config_path() {
  if snapped; then
    dir=/var/snap/nextcloud/current/nextcloud/config
  else
    dir=$NEXTCLOUD/config
  fi
  dir="$(cd -P "$dir" 2> /dev/null && pwd -P)" || return 1
  echo "$dir/config.php"
}

# After a restore, beneath $RESTATE_ROOT, which need not be this machine's
# own root: nothing here is asked of the Nextcloud that may be running.
if [ "$action" = restored ]; then
  dump=${RESTATE_DUMP_DIR:-}
  if [ -z "$dump" ] || [ ! -f "$dump/config.path" ] || [ ! -f "$dump/config.before" ] ||
    [ ! -f "$dump/config.paused" ]; then
    exit 0
  fi
  target="${RESTATE_ROOT%/}$(cat "$dump/config.path")"
  if [ -f "$target" ] && [ ! -L "$target" ] && cmp -s "$target" "$dump/config.paused"; then
    # Written into, not replaced: it keeps the owner and mode it was given.
    cat "$dump/config.before" > "$target"
    echo "restate hook: $target put back as it was before maintenance mode" >&2
  fi
  exit 0
fi

# Looks, and changes nothing.
if [ "$action" = present ]; then
  if snapped || find_nextcloud; then
    echo present
  else
    echo absent
  fi
  exit 0
fi

if ! snapped && ! find_nextcloud; then
  echo "restate hook: no Nextcloud found here; set NEXTCLOUD in a copy of $0" >&2
  exit 1
fi

case "$action" in
paths)
  if snapped; then
    echo /var/snap/nextcloud/current
    echo /var/snap/nextcloud/common
  else
    echo "$NEXTCLOUD"
  fi
  data="$(occ config:system:get datadirectory 2> /dev/null || true)"
  if [ -n "$data" ] && [ -d "$data" ]; then
    echo "$data"
  fi
  ;;
pause)
  config=
  if [ -n "${RESTATE_DUMP_DIR:-}" ] && config="$(config_path)" && [ -f "$config" ]; then
    printf '%s\n' "$config" > "$RESTATE_DUMP_DIR/config.path"
    cat "$config" > "$RESTATE_DUMP_DIR/config.before"
  else
    config=
  fi
  occ maintenance:mode --on
  if [ -n "$config" ]; then
    cat "$config" > "$RESTATE_DUMP_DIR/config.paused"
  fi
  if snapped; then
    if [ -n "${RESTATE_DUMP_DIR:-}" ]; then
      out="$RESTATE_DUMP_DIR/mysqldump.sql"
      nextcloud.mysqldump > "$out.tmp"
      gzip -f "$out.tmp"
      mv -f "$out.tmp.gz" "$out.gz"
    fi
  elif [ "${RESTATE_QUIESCE:-}" = no ]; then
    dbtype="$(occ config:system:get dbtype 2> /dev/null || true)"
    case "$dbtype" in
    '' | sqlite*) ;;
    *)
      echo "restate hook: Nextcloud's database ($dbtype) is copied as it runs; capture with" \
        "--quiesce as well, which pauses it" >&2
      ;;
    esac
  fi
  ;;
resume)
  occ maintenance:mode --off
  ;;
*)
  echo "usage: $0 present|paths|pause|resume|restored NAME" >&2
  exit 2
  ;;
esac
