#!/bin/sh
# postinst script for yandex-unbound
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see https://www.debian.org/doc/debian-policy/ or
# the debian-policy package


case "$1" in
    configure)
        if ! getent passwd unbound >/dev/null; then
            adduser --quiet --system --group --no-create-home --home /var/lib/unbound unbound
        fi
        mkdir -p /var/lib/unbound ||:
        chown unbound:unbound /var/lib/unbound ||:

        if [ ! -f /etc/unbound/unbound_control.key ]; then
            unbound-control-setup 1>/dev/null 2>&1 || true
        fi

        if [ -f /etc/resolvconf/update.d/unbound ]; then
            chmod -x /etc/resolvconf/update.d/unbound
        fi
    ;;

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

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

if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ]; then
    if [ -x "/etc/init.d/unbound" ]; then
        update-rc.d unbound defaults >/dev/null
    fi
    if [ -x "/etc/init.d/unbound" ] || [ -e "/etc/init/unbound.conf" ]; then
        invoke-rc.d unbound start || exit $?
    fi

    systemctl stop systemd-resolved.service ||:
    systemctl disable systemd-resolved.service ||:
    systemctl unmask unbound.service ||:
    systemctl enable unbound.service ||:
    systemctl start unbound.service ||:
fi

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
