#! /bin/sh
# postinst script for noris-server
#
# 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>
#        * <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
#
# quoting from the policy:
#     Any necessary prompting should almost always be confined to the
#     post-installation script, and should be protected with a conditional
#     so that unnecessary prompting doesn't happen if a package's
#     installation fails and the `postinst' is called with `abort-upgrade',
#     `abort-remove' or `abort-deconfigure'.

case "$1" in
    configure)
	# nrpe konfigurieren
	grep -e "^nrpe" /etc/inetd.conf > /dev/null || \
	echo "nrpe	stream	tcp	nowait	root	/usr/sbin/tcpd	/usr/sbin/nrpe -i /etc/nrpe.cfg" >> /etc/inetd.conf
	grep -e '^nrpe.*5666\/tcp' /etc/services > /dev/null || \
	echo "nrpe		5666/tcp			# NETSAINT" >> /etc/services
	# ssh
	cp /etc/ssh/sshd_config /etc/ssh/sshd_config.dpkg-old
	sed -e 's/X11Forwarding\ no/X11Forwarding\ yes/g' /etc/ssh/sshd_config.dpkg-old > /etc/ssh/sshd_config
	/etc/init.d/ssh reload
	# inittab
	if [ -f /etc/inittab ]; then
		cp /etc/inittab /etc/inittab.dpkg-old
	sed -e 's#getty\ 38400#getty\ -f\ \/etc\/issue\.linuxlogo\ 38400#g;s#^\#T0\:23\:respawn\:\/sbin\/getty\ \-L\ ttyS0\ 9600\ vt100#T0\:23\:respawn\:\/sbin\/getty\ \-L\ ttyS0\ 9600\ vt100#g' /etc/inittab.dpkg-old > /etc/inittab
	fi
	/sbin/telinit q
	/etc/init.d/openbsd-inetd restart
	# firewall konfigurieren
	chmod 755 /etc/init.d/firewall
	update-rc.d firewall defaults 12 88

        # noris collectd config verwenden
        if ! grep -q '^CONFIGFILE' /etc/default/collectd; then
		# DISABLE=0 auf 1 setzen, damit collectd nicht startet
		cp /etc/default/collectd /etc/default/collectd.dpkg-old
		sed -e 's/DISABLE=0/DISABLE=1/g' /etc/default/collectd.dpkg-old > /etc/default/collectd
		echo 'CONFIGFILE="/etc/collectd/noris.conf"' >> /etc/default/collectd
		# sonst error bei Neuinstallation
		if [ ! -e /etc/collectd/noris.conf ]; then 
			ln -s /etc/collectd/collectd.conf /etc/collectd/noris.conf 
		fi
                /etc/init.d/collectd restart
        fi

	# Sicherheitsmassnahme Backup RT#538576
	CONFIG=/etc/opt/omni/client/allow_hosts
	if [ ! -f $CONFIG ]; then
		echo 127.0.0.1 > $CONFIG
	fi

	# Wir testen ob /etc/cron.d/noris-dedicated-common da ist
        if [ ! -f /etc/cron.d/noris-dedicated-common ]; then
		echo "23 7 * * 1-5  root    /usr/sbin/noris-apt.sh" > /etc/cron.d/noris-dedicated-common
	fi

	# Ticket 10175415
        if [ ! -d /var/log/omni ]; then
                mkdir /var/log/omni
        fi

	if [[ ! -L /var/log/omni/debug.log && -f /var/opt/omni/log/debug.log ]]; then
		ln -s /var/opt/omni/log/debug.log /var/log/omni/debug.log
	fi
	if [[ ! -L /var/log/omni/inet.log && -f /var/opt/omni/log/debug.log ]]; then
		ln -s /var/opt/omni/log/inet.log /var/log/omni/inet.log
	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


