#!/bin/sh
# -*- mode: shell-script; coding: utf-8 -*-
#
# postinst script for elho-base
#
# 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

. /lib/elho/invokeinitscript

case "$1" in
    configure)
	debconf-force-selections /usr/share/elho/preseed/elho-base.cfg
	debconf-force-selections /usr/share/elho/preseed/exim4-dns.cfg

	/lib/elho/seed-exim4-hostnames | debconf-force-selections
	# prevent exim4-config from appending mailname again
	debconf-fset exim4/dc_other_hostnames mailname true

	if [ ! -f /etc/exim4/update-exim4.conf.conf.elho-old ]; then
	    if [ -f /etc/exim4/update-exim4.conf.conf ]; then
		mv /etc/exim4/update-exim4.conf.conf \
		    /etc/exim4/update-exim4.conf.conf.elho-old
	    fi

	    dpkg-reconfigure --priority critical exim4-config

	    if [ -f /etc/exim4/update-exim4.conf.conf.elho-old ]; then
		if diff --brief /etc/exim4/update-exim4.conf.conf.elho-old \
		    /etc/exim4/update-exim4.conf.conf > /dev/null
		then
		    rm -f /etc/exim4/update-exim4.conf.conf.elho-old
		else
		    echo "Updated update-exim4.conf.conf as follows:"
		    diff --minimal /etc/exim4/update-exim4.conf.conf.elho-old \
		    /etc/exim4/update-exim4.conf.conf \
			| sed --quiet --file=/lib/elho/diff-pretty-print.sed
		fi
	    fi
	    invokeinitscript exim4 force-reload
	fi

	/lib/elho/addtogroup root sudo

	/lib/elho/addtogroup elho audio
	/lib/elho/addtogroup elho cdrom
	/lib/elho/addtogroup elho floppy
	/lib/elho/addtogroup elho plugdev
	/lib/elho/addtogroup elho src
	/lib/elho/addtogroup elho sudo
	/lib/elho/addtogroup elho video

	# trigger locales regeneration on first installation and after updates
	# to the locales settings
	if [ -z "$2" ] || dpkg --compare-versions "$2" lt "3"; then
	    # force debconf values to be used instead of old generated config
	    rm --force /etc/locale.gen
	    dpkg-reconfigure --priority critical locales
	fi

	# installed by netbase 4.38 up to 4.42
	rm -f /etc/sysctl.d/bindv6only.conf

	invokeinitscript procps force-reload

	/lib/elho/customize-conffile --ok /etc/apt/sources.list

	/lib/elho/customize-conffile --ok /etc/default/debsecan

	/lib/elho/customize-conffile --ok /etc/default/ferm
	rm -f /var/cache/ferm/start.sh /var/cache/ferm/stop.sh
	invokeinitscript ferm force-reload

	if /lib/elho/customize-conffile /etc/default/grub ; then
	    update-grub
	fi

	if /lib/elho/customize-conffile /etc/default/oidentd ; then
	    invokeinitscript oidentd restart
	fi

	# replace sudoers versions of older releases by squeeze one
	for dist in etch lenny; do
	    if diff --brief "/usr/share/elho/distfiles/${dist}/sudoers" \
		/etc/sudoers > /dev/null
	    then
		echo -n "Updating sudoers file from ${dist} to squeeze" \
		    "version..."
		install --backup=simple --suffix=.dpkg-old \
		    --mode=440 --owner=root --group=root \
		    /usr/share/elho/distfiles/squeeze/sudoers /etc
		echo "done."
		invokeinitscript sudo force-reload
		break
	    fi
	done

	/lib/elho/customize-conffile --ok /etc/default/tmpfs

	if /lib/elho/customize-conffile /etc/ssh/sshd_config ; then
	    invokeinitscript ssh reload
	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


