#!/bin/sh

# postinst based heavily on the postinst of libssl0.9.8, courtesy of 
# Christoph Martin.

. /usr/share/debconf/confmodule

set -e

# 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 ""
}

if test -d /run/systemd/system; then
    is_service_configured() {
        systemctl is-enabled --quiet $1 || return 1
    }
    restart_service() {
        if systemctl try-restart $1; then
            :
        else
            failed="$failed $1"
        fi
    }
else # not systemd
    is_service_configured() {
        service="$1"
	idl="/etc/init.d/${service}"
	if [ -n "$idl" ] && [ -x $idl ]; then
	    return 0
	else
            return 1
        fi
    }

    restart_service() {
        service="$1"
	idl="invoke-rc.d ${service}"
	echo -n "  $service: stopping..." 2>&1
	$idl stop > /dev/null 2>&1 || true
	sleep 1
	echo -n "starting..." 2>&1
	if $idl start > /dev/null 2>&1; then
	    echo "done." 2>&1
	else
	    echo "FAILED! ($?)" 2>&1
	    failed="$service $failed"
	fi
    }
fi
    
                 installed_services() {
    check="$@"

    # Only get the ones that are installed, and configured
    check=$(dpkg -s $check 2> /dev/null | egrep '^Package:|^Status:' | awk '{if ($1 ~ /^Package:/) { package=$2 } else if ($0 ~ /^Status: .* installed$/) { print package }}')

    # some init scripts don't match the package names
    check=$(echo $check | \
	sed -e's/\bapache2-common\b/apache2/g' \
	    -e's/\bat\b/atd/g' \
	    -e's/\bdovecot-common\b/dovecot/g' \
	    -e's/\bdante-server\b/danted/g' \
	    -e's/\bexim4-base\b/exim4/g' \
	    -e's/\bheartbeat-2\b/heartbeat/g' \
	    -e's/\bhylafax-server\b/hylafax/g' \
	    -e's/\bpartimage-server\b/partimaged/g' \
	    -e's/\bpostgresql-common\b/postgresql/g' \
	    -e's/\bsamba\b/smbd samba-ad-dc/g' \
	    -e's/\bsasl2-bin\b/saslauthd/g' \
    )

    for service in $check; do
	if is_service_configured $service; then
		services="$service $services"
	else
		echo "WARNING:  $service not configured." >&2
	fi
    done
    echo "$services"
}

if [ "$1" = "configure" ]
then
    if [ ! -z "$2" ]; then
	if dpkg --compare-versions "$2" lt 1.4.0-2; then
	    db_version 2.0

	    echo -n "Checking for services that may need to be restarted..."

	    check="apache2-common at bayonne cherokee courier-authdaemon"
	    check="$check cron cups"
	    check="$check dante-server diald dovecot-common exim exim4-base"
	    check="$check fcron fireflier-server freeradius gdm heartbeat"
	    check="$check heartbeat-2 hylafax-server iiimf-server inn2"
	    check="$check kannel linesrv linesrv-mysql lsh-server"
	    check="$check muddleftpd netatalk nuauth partimage-server"
	    check="$check perdition pgpool popa3d"
	    check="$check postgresql-common proftpd pure-ftpd"
	    check="$check pure-ftpd-ldap pure-ftpd-mysql"
	    check="$check pure-ftpd-postgresql racoon samba sasl2-bin"
	    check="$check sfs-server solid-pop3d squid squid3 tac-plus"
	    check="$check vsftpd wu-ftpd wzdftpd xrdp yardradius yaws"

	    if [ -z "$(who | awk '$2 ~ /console/ && $NF ~ /:[0-9]/ { print $NF }
                                  $2 ~ /:[0-9]/ { print $2 }')" ]
	    then
		check="$check wdm xdm"
	    fi

	    echo "Checking init scripts..."
	    services=$(installed_services "$check")
	    if [ -n "$services" ]; then
		db_input critical libraries/restart-without-asking || true
		db_go || true
		db_get libraries/restart-without-asking
		if [ "$RET" != true ]; then
		    db_reset libpam0g/restart-services
		    db_set libpam0g/restart-services "$services"
		    db_input critical libpam0g/restart-services || true
		    db_go || true
		    db_get libpam0g/restart-services

		    if [ "x$RET" != "x" ]
		    then
			services=$RET
		    else
			services=""
		    fi
		fi
		echo
		if [ "$services" != "" ]; then
		    echo "Restarting services possibly affected by the upgrade:"
		    failed=""
		    rl=$(runlevel | sed 's/.*\ //')
		    for service in $services; do
			idl="invoke-rc.d ${service}"

			case "$service" in
			    gdm)
				echo -n "  $service: reloading..."
				if $idl reload > /dev/null 2>&1; then
				    echo "done."
				else
				    echo "FAILED! ($?)"
				    failed="$service $failed"
				fi
				continue
			    ;;
			esac
                        restart_service "$service"
		    done
		    echo
		    if [ -n "$failed" ]; then
			db_subst libpam0g/restart-failed services "$failed"
			db_input critical libpam0g/restart-failed || true
			db_go || true
		    else
			echo "Services restarted successfully."
		    fi
		    echo
		fi
	    else
		echo "Nothing to restart."
	    fi

	    if [ -n "$(who | awk '$2 ~ /console/ && $NF ~ /:[0-9]/ { print $NF }
                                  $2 ~ /:[0-9]/ { print $2 }')" ]
	    then
		dms=""
		for service in wdm xdm; do
		    case "$services" in
			*$service*)	;;
			*)	dms="$dms $service"
		    esac
		done
		services=$(installed_services "$dms")
		if [ -n "$services" ]; then
		    db_input critical libpam0g/xdm-needs-restart || true
		    db_go || true
		fi
	    fi

	    # Shut down the frontend, to make sure none of the
	    # restarted services keep a connection open to it
	    db_stop
	fi # end upgrading and $2 lt 1.4.0-2
    fi # Upgrading
fi



