#!/bin/sh

set -e

# Unlike postrm, I can be sure, that runit-helper is present on
# postinst.
NAME='snetaid' ENABLE='yes' ONUPGRADE='restart' /lib/runit-helper/runit-helper postinst "$@"

case "$1" in
    configure)	 
		if [ -f "/etc/init.d/snetaid" ]; then
			chmod +x /etc/init.d/snetaid
			#[ "x$(invoke-rc.d snetaid status)" != "x" ] && invoke-rc.d snetaid restart
			if ! dpkg-query -f'${db:Status-Status}\n' -W runit-init 2>/dev/null | grep -q ^installed; then
				chmod -R 755 /etc/sv/snetaid/finish
				[ "x$(pidof snetaid)" != "x" ] && invoke-rc.d snetaid restart
			fi
			update-rc.d snetaid defaults >/dev/null
			[ "x$(pidof snetaid)" = "x" ] && invoke-rc.d snetaid start
		fi
		if ! getent group netaid > /dev/null; then
            addgroup --quiet --system netaid || true
        fi
		;;

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

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

#DEBHELPER#
