#!/bin/sh
# postinst script for tomsoft-nadzor
#
# 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

. /usr/share/debconf/confmodule
# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#


case "$1" in
    configure)
        db_get tomsoft-nadzor/munin-port
        MUNIN_PORT="$RET"

        db_get tomsoft-nadzor/nrpe-port
        NRPE_PORT="$RET"

	# we don't need anything more from debconf, so we use db_stop so invoke-rc.d doesn't confuse it (see debconf-devel(7))
	db_stop || true


	# sudo validate config
	if which sudo > /dev/null
	then
		sudo -v
		ret=$?
		if [ "$ret" -ne 0 ]
		then
			ret=$?
			echo "package installation breaks SUDO ($ret) -- aborting"
			exit $ret
		fi
	fi
	
	# nagios check duplicates
	NRPE_DUPES=$(grep -h -o '^command\[.*\]' /etc/nagios/nrpe.d/*.cfg  | sort | uniq -dc)
        if [ -n "$NRPE_DUPES" ]
        then
                echo Duplicates found in /etc/nagios/nrpe.d/ - please cleanup first:
                echo $NRPE_DUPES
                echo
                exit 50
        fi


    	MUNIN_CONF=/etc/munin/munin-node.conf
    	RESTART_MUNIN=1
	PLUGIN_NEEDS_UPDATE=0
    	if fgrep -q 'allow ^2001:67c:2f24:22::75$' ${MUNIN_CONF} 2> /dev/null
    	then
    		if grep -q "^port ${MUNIN_PORT}" ${MUNIN_CONF} 2> /dev/null
    		then
    			# both katara IP address is present and port is correct - no need to touch munin-node.conf (unless dpkg-reconfigure forced)
    			RESTART_MUNIN=$DEBCONF_RECONFIGURE
    		fi
    	fi

    	if [ "$RESTART_MUNIN" = "1" ]
    	then
                if [ -z "`dpkg-divert --list ${MUNIN_CONF}`" ]
                then
                        echo "First install, diverting ${MUNIN_CONF} from munin-node to tomsoft-nadzor" >&2
                        dpkg-divert --quiet --package tomsoft-nadzor --rename --add ${MUNIN_CONF} >&2
                fi
    	
    		# create new config file from old, by adding katara IP and port
    		# split into strings to avoid even more layers of unreadeable escaping
    		A1="/^port [0-9]+\$/ 			{P=1; \$0=\"port ${MUNIN_PORT}\"}"
    		A2='/^allow \^127\\\.0\\\.0\\\.1\$$/ 	{A=1; print "allow ^195\\.190\\.136\\.75$"; print "allow ^2001:67c:2f24:22::75$"}'
    		A3='{print}'
    		A4='END {exit 2-P-A}'
    		MUNIN_ORG=`dpkg-divert --truename ${MUNIN_CONF}`
    		
    		# try to autorecover if  /etc/munin/munin-node.conf.distrib is removed manually
    		if [ ! -f "$MUNIN_ORG" ]
    		then
    			cp -a $MUNIN_CONF $MUNIN_ORG
    		fi
    		awk "$A1 $A2 $A3 $A4"  < ${MUNIN_ORG} > ${MUNIN_CONF} || exit $?
	fi

	MYSQL_PLUGIN=/usr/share/munin/plugins/mysql_
	if ! grep -q 'INDIVIDUAL BUFFER POOL INFO' ${MYSQL_PLUGIN}
	then
		# not patched yet, need to patch
                if [ -z "`dpkg-divert --list ${MYSQL_PLUGIN}`" ]
                then
                        echo "First install, diverting ${MYSQL_PLUGIN} from munin-node to tomsoft-nadzor" >&2
                        dpkg-divert --quiet --package tomsoft-nadzor --rename --add ${MYSQL_PLUGIN} >&2
                fi
		PLUGIN_NEEDS_UPDATE=1
		RESTART_MUNIN=1
	fi

	if [ "$PLUGIN_NEEDS_UPDATE" = "1" ]
	then
		sed -ne "p;/BACKGROUND THREAD/c\\\t'INDIVIDUAL BUFFER POOL INFO' => \\\&skip,\t# /mn/ 20141203 nema by default" < `dpkg-divert --truename ${MYSQL_PLUGIN}` > ${MYSQL_PLUGIN} || exit $?
		RESTART_MUNIN=1
	fi

	# if our patched plugin is installed...
	if [ -n "`dpkg-divert --list ${MYSQL_PLUGIN}`" ]
	then
		# .. and patched plugin is no longer needed (due to upstream being fixed), remove our patch
		if grep -q 'INDIVIDUAL BUFFER POOL INFO' `dpkg-divert --truename ${MYSQL_PLUGIN}`
		then
			rm -f ${MYSQL_PLUGIN}
                        echo "New version of distribution plugin detected, removing ${MYSQL_PLUGIN} patched by tomsoft-nadzor" >&2
			dpkg-divert --package tomsoft-nadzor --rename --remove ${MYSQL_PLUGIN} >&2
			RESTART_MUNIN=1
		fi
	fi

	if [ ! -x ${MYSQL_PLUGIN} ]
	then
		chmod 755 ${MYSQL_PLUGIN}
                chmod 000 `dpkg-divert --truename ${MYSQL_PLUGIN}`
		RESTART_MUNIN=1
	fi

	
	# add new plugins (if there are any)
	if [ -n "`munin-node-configure --sh 2> /dev/null`" ]
	then
		echo tomsoft-nadzor detected new munin plugins, adding...
		munin-node-configure --sh 2> /dev/null | sh
		# we don't need per-peer ntp stats
		rm -f /etc/munin/plugins/ntp_[0-9]* 
		# we don't need old mysql plugin
		rm -f /etc/munin/plugins/mysql_.distrib
		RESTART_MUNIN=1
	fi

	# FIXME default port 56066 vidi da isto pita i radi replace?	
	NRPE_CONF=/etc/nagios/nrpe.d/nrpe-tomsoft.cfg
	RESTART_NRPE=1
        if ! grep -q "^server_port=${NRPE_PORT}" ${NRPE_CONF} 2> /dev/null
        then
        	# NRPE port changed, need to rewrite
	    	awk "/^server_port=[0-9]+\$/	{\$0=\"server_port=${NRPE_PORT}\"}  {print}" < ${NRPE_CONF} > ${NRPE_CONF}.tmp && mv -f ${NRPE_CONF}.tmp ${NRPE_CONF}
        fi

	if [ -f /etc/apt/sources.list.d/tomsoft.list ]
	then
		chmod a+r /etc/apt/sources.list.d/*
	fi
	
	if [ "$RESTART_MUNIN" = "1" ]
	then
		invoke-rc.d munin-node restart  >&2 || exit $?
	fi
    	if [ "$RESTART_NRPE"  = "1" ]
    	then
    		invoke-rc.d nagios-nrpe-server restart  >&2 || exit $?
    	fi
    ;;

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

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

exit 0
