#!/bin/sh
set -eu

. "$(dirname "$0")/ferm-lib.sh"

CACHE_DIR="/var/cache/ferm"
CACHE_NAME="$(systemd-escape /etc/ferm/ferm.conf)"
CACHE_FILE="${CACHE_DIR}/${CACHE_NAME}.sh"
KERNEL_FILE="${CACHE_DIR}/${CACHE_NAME}.kernel"

enable_cache() {
    mkdir -p /etc/systemd/system/ferm.service.d
    cat > /etc/systemd/system/ferm.service.d/cache.conf <<-EOF
	[Service]
	Environment=CACHE=yes
	EOF
    systemctl daemon-reload
}

disable_cache() {
    rm -f /etc/systemd/system/ferm.service.d/cache.conf
    rmdir /etc/systemd/system/ferm.service.d 2>/dev/null || true
    systemctl daemon-reload
}

if [ -z "${AUTOPKGTEST_REBOOT_MARK:-}" ]; then
    echo "=== BUILD CONFIG, ENABLE CACHE, START ==="

    echo "flush"
    flush
    echo "setup_config"
    setup_config
    echo "flush"
    flush

    echo "enable_cache"
    enable_cache

    echo "clear stale cache dir"
    rm -rf "${CACHE_DIR}"
    mkdir -p "${CACHE_DIR}"

    echo "systemctl restart ferm.service"
    systemctl restart ferm.service
    echo "systemctl is-active --quiet ferm.service"
    systemctl is-active --quiet ferm.service
else
    wait_after_reboot
fi

echo "=== CACHE FILE MUST EXIST AND BE NON-EMPTY AFTER FIRST ACTIVATION ==="
[ -s "${CACHE_FILE}" ] || { echo >&2 "ERROR: ${CACHE_FILE} missing or empty (ferm cache-generation call likely broken)"; exit 1; }
[ -f "${KERNEL_FILE}" ] || { echo >&2 "ERROR: ${KERNEL_FILE} missing"; exit 1; }

echo "=== CHECK RULES ACTIVE (fresh cache) ==="
echo "check_present"
check_present

echo "=== RESTART, CONFIG UNCHANGED: CACHE MUST BE REUSED (not regenerated) ==="
MTIME_BEFORE=$(stat -c %Y "${CACHE_FILE}")
systemctl restart ferm.service
systemctl is-active --quiet ferm.service
MTIME_AFTER=$(stat -c %Y "${CACHE_FILE}")
if [ "${MTIME_BEFORE}" != "${MTIME_AFTER}" ]; then
    echo >&2 "ERROR: cache file mtime changed despite unchanged config -- cache_needs_regen false positive"
    exit 1
fi
echo "check_present"
check_present

echo "=== CHANGE CONFIG: CACHE MUST BE REGENERATED ==="
# setup_config() takes no arguments and only varies via TOOLSUFFIX, so we
# force a content/mtime change directly rather than inventing lib surface
# that doesn't exist. This also tests invalidation on ANY config edit,
# not just a TOOLSUFFIX-specific one.
echo "# cache-invalidation probe $(date +%s)" >> /etc/ferm/ferm.conf
sleep 1
MTIME_BEFORE=$(stat -c %Y "${CACHE_FILE}")
systemctl restart ferm.service
systemctl is-active --quiet ferm.service
MTIME_AFTER=$(stat -c %Y "${CACHE_FILE}")
if [ "${MTIME_BEFORE}" = "${MTIME_AFTER}" ]; then
    echo >&2 "ERROR: cache file was NOT regenerated after config change -- stale cache served"
    exit 1
fi
echo "check_present"
check_present

echo "=== STOP: RULES MUST BE FLUSHED (deactivate path does not use cache) ==="
echo "systemctl stop ferm.service"
systemctl stop ferm.service
echo "check_absent"
check_absent

if [ -z "${AUTOPKGTEST_REBOOT_MARK:-}" ] && [ "${MODE:-basic}" = "reboot" ]; then
    echo "=== autopkgtest-reboot ferm-systemd-cache-reboot ==="
    autopkgtest-reboot ferm-systemd-cache-reboot
    exit 0
fi

flush
disable_cache
rm -rf "${CACHE_DIR}"

exit 0
