#!/bin/bash

set -e

# Source debconf library.
. /usr/share/debconf/confmodule

db_version 2.0

# This conf script is capable of backing up
db_capb backup

# Cool. ifplugd.conf just sets environment, so we
# can use it and convert from and to debconf without
# loss.
CONFIGFILE=/etc/ifplugd/ifplugd.conf
if [ -f ${CONFIGFILE} ] ; then
    . ${CONFIGFILE}

    db_set ifplugd/args ${ARGS}
    db_set ifplugd/interfaces ${INTERFACES}
fi

DEFAULTFILE=/etc/default/ifplugd
if [ -f ${DEFAULTFILE} ] ; then
    . ${DEFAULTFILE}

    db_set ifplugd/suspend_action ${SUSPEND_ACTION}
    db_set ifplugd/hotplug_interfaces ${HOTPLUG_INTERFACES}

    # we move those to /etc/default/ifplugd since 0.19:
    db_set ifplugd/args ${ARGS}
    db_set ifplugd/interfaces ${INTERFACES}
fi

STATE=1
while [ "$STATE" != 0 -a "$STATE" != 5 ]; do
	case "$STATE" in
	1)
		db_input medium ifplugd/interfaces || true
		;;
	2)
		db_input medium ifplugd/hotplug_interfaces || true
		;;
	3)
		db_input medium ifplugd/args || true
		;;
	4)
		db_input low ifplugd/suspend_action || true
		;;
	esac			

	if db_go; then
		STATE=$(($STATE + 1))
	else
		STATE=$(($STATE - 1))
	fi
done
 	
# dpkg-reconfigure does not stop services:
# (cause of Bug#151528)
# in debhelper version < 1.2.9

DH_VERSION=`dpkg -l debconf | awk ' /^ii/ { print $3 }'`

if dpkg --compare-versions $DH_VERSION lt "1.2.9" ; then
    if [ x"$1" = x"reconfigure" ] ; then
	if [ -x "/etc/init.d/ifplugd" ]; then
		if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
			invoke-rc.d ifplugd stop || exit $?
		else
			/etc/init.d/ifplugd stop || exit $?
		fi
	fi
    fi
fi

exit 0
