#!/bin/bash
set -e

case "$1" in
    purge)
        # Only on purge: remove user, configs, state, HAProxy
        # drop-in (so the distro haproxy.cfg path takes over again).
        if getent passwd edgeguard >/dev/null; then
            deluser --quiet edgeguard >/dev/null 2>&1 || true
        fi
        rm -f /etc/systemd/system/haproxy.service.d/edgeguard.conf
        rmdir /etc/systemd/system/haproxy.service.d 2>/dev/null || true
        systemctl daemon-reload || true
        rm -rf /etc/edgeguard /var/lib/edgeguard /var/log/edgeguard
        ;;

    remove)
        # Plain remove (not purge) — pull our HAProxy override so
        # the haproxy daemon goes back to its distro config and
        # keeps running.
        rm -f /etc/systemd/system/haproxy.service.d/edgeguard.conf
        rmdir /etc/systemd/system/haproxy.service.d 2>/dev/null || true
        systemctl daemon-reload || true
        systemctl reload haproxy.service 2>/dev/null || true
        ;;

    upgrade|failed-upgrade|abort-install|abort-upgrade|disappear)
        ;;

    *)
        echo "postrm called with unknown argument \`$1'" >&2
        exit 1
        ;;
esac

#DEBHELPER#

exit 0
