#!/bin/sh
# postinst script for nagiosql
#
# 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
}
setgrperm() { 
   group="$1"
   mode="$2"
   file="$3"
   shift 3
   # only do something when no setting exists
   if ! dpkg-statoverride --list "$file" >/dev/null 2>&1; then
      chgrp "$group" "$file"
      chmod "$mode" "$file"
   fi
}


case "$1" in
    configure)
        # -- /etc/nagiosql directories
        for dir in `find /etc/nagiosql -type d 2> /dev/null`; do
            [ -d $dir  ] && setperm www-data nagios 6755 $dir
	done
        # -- /etc/nagiosql files
        for file in `find /etc/nagiosql -type f -name \*.cfg 2> /dev/null`; do
            [ -f $file ] && setperm www-data nagios 0644 $file
        done
        # -- /etc/nagios4 config files
        for cfg in /etc/nagios4/nagios.cfg /etc/nagios/nagios4/cgi.cfg ; do
            [ -e $cfg ] && setgrperm www-data 0644 $cfg
	done
        # -- at last
        nsqlcfg="/etc/nagiosql/settings.php"
        [ -e $nsqlcfg ] && setperm www-data www-data 0600 $nsqlcfg
    ;;

    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
