#!/bin/sh
set -e
export LC_ALL=C

type=$1
preversion=$2

package_name()
{
    echo LIBC
}

# element() is a helper function for file-rc:
element() {
    local element list IFS

    element="$1"

    [ "$2" = "in" ] && shift
    list="$2"
    [ "$list" = "-" ] && return 1
    [ "$list" = "*" ] && return 0

    IFS=","
    set -- $list
    case $element in
	"$1"|"$2"|"$3"|"$4"|"$5"|"$6"|"$7"|"$8"|"$9")
	    return 0
    esac
    return 1
}

# filerc (runlevel, service) returns /etc/init.d/service, if service is
# running in $runlevel:
filerc() {
    local runlevel basename
    runlevel=$1
    basename=$2
    while read LINE
    do
	case $LINE in
	    \#*|"") continue
	esac

	set -- $LINE
	SORT_NO="$1"; STOP="$2"; START="$3"; CMD="$4"
	[ "$CMD" = "/etc/init.d/$basename" ] || continue

	if element "$runlevel" in "$START" || element "S" in "$START"
	then
	    echo "/etc/init.d/$basename"
	    return 0
	fi
    done < /etc/runlevel.conf
    echo ""
}

checkpkgver () {
    local status pkg
    pkg=$1
    status=$(dpkg -s $pkg 2>/dev/null | grep ^Status: | sed -e 's/^Status: \(.*\) \(.*\) \(.*\)/\3/g')
    if [ -n "$status" ] && [ "$status" != "not-installed" ] && [ "$status" != "config-files" ]; then
	echo $(dpkg -s $pkg 2>/dev/null | grep ^Version: | sed -e 's/^Version: *//');
    fi
}

if [ "$type" = "configure" ]
then
    # Add support for /etc/ld.so.conf.d
    if [ -e /etc/ld.so.conf ]; then
        [ -z "$(tail -n 1 /etc/ld.so.conf)" ] || echo >> /etc/ld.so.conf
    else
        touch /etc/ld.so.conf
    fi
    if ! grep -q '^include /etc/ld.so.conf.d/.*\.conf$' /etc/ld.so.conf ; then
        echo 'include /etc/ld.so.conf.d/*.conf' >> /etc/ld.so.conf
    fi

    # We don't use a registry anymore, remove the old file
    rm -f /etc/ld.so.hwcappkgs
 
    # /etc/ld.so.nohwcap code: NOHWCAP

    # Load debconf module if available
    if [ -f /usr/share/debconf/confmodule ] ; then
	. /usr/share/debconf/confmodule
    fi

    if [ -n "$preversion" ]; then
	if [ ! -d /var/mail ] && [ ! -L /var/mail ]; then
	    ln -sf spool/mail /var/mail
	fi
	if dpkg --compare-versions "$preversion" lt '2.11~20100104-0ubuntu2'; then
	    # LP #504198
	    if which locale-gen >/dev/null; then
		locale-gen --purge
	    fi
	fi
	if dpkg --compare-versions "$preversion" lt 2.11~20100104-0ubuntu3; then
	    check="apache2-common apache apache-ssl apache-perl autofs at"
	    check="$check boa cucipop courier-authdaemon cron cups exim"
	    check="$check exim4-base dovecot-common cucipop incron lprng lpr"
	    check="$check lpr-ppd mysql-server nis openbsd-inetd"
	    check="$check openldapd proftpd postfix postfix-tls rsync samba"
	    check="$check sasl2-bin slapd smail sendmail snmpd ssh"
	    check="$check spamassassin vsftpd wu-ftpd wu-ftpd-academ wwwoffle"
	    check="$check webmin dropbear gdm"
	    # NSS services check: NSS_CHECK
	    if [ -n "$services" ]; then

	        if [ -f /usr/share/debconf/confmodule ] ; then
		    db_version 2.0
		    db_reset glibc/restart-services
		    db_set glibc/restart-services "$services"
		    if [ "$RELEASE_UPGRADE_MODE" = desktop ]; then
			db_input medium glibc/restart-services || true
		    else
			db_input critical glibc/restart-services || true
		    fi
		    db_go || true
		    db_get glibc/restart-services
		    if [ "x$RET" != "x" ]
		    then
		        services="$RET"
		    else
		    	services=""
		    fi
		else
		    echo
		    echo "Name Service Switch update in the C Library: post-installation question."
		    echo
		    echo "Running services and programs that are using NSS need to be restarted,"
		    echo "otherwise they might not be able to do lookup or authentication any more"
		    echo "(for services such as ssh, this can affect your ability to login)."
		    echo "Note: restarting sshd/telnetd should not affect any existing connections."
		    echo
		    echo "The services detected are: "
		    echo "        $services"
		    echo
		    echo "If other services have begun to fail mysteriously after this upgrade, it is"
		    echo "probably necessary to restart them too.  We recommend that you reboot your"
		    echo "machine after the upgrade to avoid NSS-related troubles."
		    echo
		    frontend=`echo "$DEBIAN_FRONTEND" | tr '[:upper:]' '[:lower:]'`
		    if [ "$frontend" = noninteractive ]; then
		    	echo "Non-interactive mode, restarting services"
		    	answer=yes
		    else
		    	echo -n "Do you wish to restart services? [Y/n] "
		    	read answer
		    	case $answer in
			    N*|n*) services="" ;;
			    *) ;;
		        esac
		    fi
		fi
		echo
		if [ "$services" != "" ]; then
		    echo "Restarting services possibly affected by the upgrade:"
		    failed=""
		    for service in $services; do
			if [ -x /lib/init/readlink ] && [ "$(/lib/init/readlink /etc/init.d/$service)" = /lib/init/upstart-job ]; then
			    idl="restart"
			    idlopt="${service}"
		    	elif [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
			    idl="invoke-rc.d ${service}"
			    idlopt="restart"
			elif [ -f /usr/share/file-rc/rc ] || [ -f /usr/lib/file-rc/rc ] && [ -f /etc/runlevel.conf ]; then
			    idl=$(filerc $rl $service)
			    idlopt="restart"
			else
			    idl=$(ls /etc/rc${rl}.d/S??${service} 2> /dev/null | head -1)
			    idlopt="restart"
			fi
			case "$service" in
			    gdm)
				# old gdm uses initscript, we need
				# to restart it. note that we do
				# not need to do anything for new
				# gdm that uses upstart job, so this
				# special case can be removed after lucid
				if [ "$idlopt" = restart ]; then
				    echo -n "  $service: reloading..."
				    idlopt="reload"
				    if $idl $idlopt > /dev/null 2>&1; then
					echo "done."
				    else
					echo "FAILED! ($?)"
					failed="$service $failed"
				    fi
				fi
				;;
			    *)
				echo -n "  $service: restarting..."
				if $idl $idlopt > /dev/null 2>&1; then
				    echo "done."
				else
				    echo "FAILED! ($?)"
				    failed="$service $failed"
				fi
				;;
			esac
		    done
		    echo
		    if [ -n "$failed" ]; then
	        	if [ -f /usr/share/debconf/confmodule ] ; then
		    	    db_fset glibc/restart-failed seen false
			    db_subst glibc/restart-failed services "$failed"
			    if [ "$RELEASE_UPGRADE_MODE" = desktop ]; then
				db_input medium glibc/restart-failed || true
			    else
				db_input critical glibc/restart-failed || true
			    fi
			    db_go || true
			else
			    echo "The following services failed to start: $failed"
			    echo
			    echo "You will need to start these manually by running \`/etc/init.d/<service> start'"
			    echo "If the service still fails to start, you may need to file a bug on"
			    echo "$(package_name) or the service involved."
			    frontend=`echo "$DEBIAN_FRONTEND" | tr '[:upper:]' '[:lower:]'`
			    if [ "$frontend" != noninteractive ]; then
			    	echo
			    	echo -n "Press ENTER to continue: "
			    	read foo
			    fi
			fi
		    else
			echo "Services restarted successfully."
                    fi
		fi
                # Shut down the frontend, to make sure none of the
		# restarted services keep a connection open to it
	        if [ -f /usr/share/debconf/confmodule ] ; then
		    db_stop
		fi
	    else
		echo "Nothing to restart."
	    fi
	fi # end upgrading and $preversion lt 2.10-2
    fi # Upgrading

    if [ "`uname -s`" = Linux ]; then
      if [ "$(stat -c %d/%i /)" = "$(stat -Lc %d/%i /proc/1/root 2>/dev/null)" ]; then
        # the devicenumber/inode pair of / is the same as that of /sbin/init's
        # root, so we're *not* in a chroot
        (telinit u ; sleep 1)
      fi
    fi
fi

#DEBHELPER#

exit 0
