#!/bin/sh
#
# Copyright (c) 2026 Bryan C. Everly
# SPDX-License-Identifier: BSD-2-Clause
#
# restate hook: Redis, for capture --quiesce.
#
#   present NAME  whether Redis is on this host: prints present or absent
#   pause NAME    stop redis-server, which saves its data as it stops
#   resume NAME   start it again
#
# To change what this does, copy it to /etc/restate/hooks.d/redis (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 command -v redis-server > /dev/null 2>&1; then
    echo present
  else
    echo absent
  fi
  exit 0
fi

case "$1" in
pause) systemctl stop redis-server ;;
resume) systemctl start redis-server ;;
restored | paths)
  # Asked of every hook, after a restore and before a walk: nothing to do
  # for either here, which exit 2 says without a word.
  exit 2
  ;;
*)
  echo "usage: $0 present|pause|resume NAME" >&2
  exit 2
  ;;
esac
