#!/bin/sh
set -e

case "$1" in
    purge)
        rm -rf /var/lib/rekono /var/cache/rekono /var/log/rekono /etc/rekono/tls
        rm -f /etc/rekono/config.yaml

        # Dropping the cluster that holds the user's findings would be destructive
        if command -v pg_lsclusters >/dev/null 2>&1; then
            cluster="$(pg_lsclusters --no-header | awk '$2 == "rekono" {print $1}')"
            if [ -n "${cluster}" ]; then
                echo "Rekono's PostgreSQL cluster was left untouched. To remove it:"
                echo "  sudo pg_dropcluster ${cluster} rekono --stop"
            fi
        fi

        if getent passwd rekono >/dev/null; then
            deluser --system rekono >/dev/null 2>&1 || true
        fi
        if getent group rekono >/dev/null; then
            delgroup --system rekono >/dev/null 2>&1 || true
        fi
        ;;

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

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

#DEBHELPER#

exit 0
