#!/bin/sh
# postinst script for pnp4nagios-bin
#
# 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 http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

setperm() {
	user="$1"
	group="$2"
	mode="$3"
	file="$4"
	shift 4
	# only do something when no setting exists
	if ! dpkg-statoverride --list "$file" >/dev/null 2>&1; then
		chown "$user":"$group" "$file"
		chmod "$mode" "$file"
	fi
}

case "$1" in
	configure)
		if ! getent passwd nagios > /dev/null ; then
			echo 'Adding system-user for nagios' 1>&2
			adduser --system --group --home /var/lib/nagios \
				--disabled-login --force-badname nagios > /dev/null
		fi
		setperm nagios www-data	750 /var/lib/pnp4nagios
		setperm nagios nagios   755 /var/lib/pnp4nagios/perfdata
		setperm nagios www-data	750 /var/log/pnp4nagios
		setperm nagios nagios	750 /var/log/pnp4nagios/stats
		setperm nagios nagios	770 /var/spool/pnp4nagios
		setperm nagios nagios	770 /var/spool/pnp4nagios/nagios
		setperm nagios nagios	770 /var/spool/pnp4nagios/npcd

		setperm root nagios	640 /etc/pnp4nagios/process_perfdata.cfg

		if [ -d /etc/nagios4/conf.d/ ]; then
			if [ ! -e /etc/nagios4/conf.d/pnp4nagios.cfg ]; then
				ln -s /etc/pnp4nagios/nagios.cfg /etc/nagios4/conf.d/pnp4nagios.cfg
			fi
		fi
		if [ -d /etc/icinga/objects/ ]; then
			if [ ! -e /etc/icinga/objects/pnp4nagios.cfg ]; then
				ln -s /etc/pnp4nagios/nagios.cfg /etc/icinga/objects/pnp4nagios.cfg
			fi
		fi

	;;

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

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

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

#DEBHELPER#

exit 0

