#!/bin/sh
set -e

case "$1" in
    configure)
        if ! getent group rekono >/dev/null; then
            addgroup --system rekono
        fi
        if ! getent passwd rekono >/dev/null; then
            adduser --system --ingroup rekono --no-create-home \
                --home /var/lib/rekono \
                --gecos "Rekono pentesting automation platform" \
                rekono
        fi

        for directory in /var/lib/rekono /var/lib/rekono/bin /var/lib/rekono/nginx \
                         /var/lib/rekono/valkey \
                         /var/cache/rekono /var/cache/rekono/uv \
                         /var/log/rekono; do
            if [ -d "${directory}" ]; then
                chown rekono:rekono "${directory}"
                chmod 0750 "${directory}"
            fi
        done

        # A unit left in the failed state keeps its cgroup accounting, and
        # replacing its unit file then makes the reload below fail
        if [ -d /run/systemd/system ]; then
            systemctl reset-failed 'rekono-*.service' rekono.target >/dev/null 2>&1 || true
        fi
        ;;

    abort-upgrade|abort-remove|abort-deconfigure)
        ;;

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

#DEBHELPER#

exit 0
