#!/bin/sh
set -e

. /usr/share/debconf/confmodule
db_version 2.0
db_capb backup

consolekit_running() {
	test -e /var/run/ConsoleKit/database
}

systemd_running() {
	test -e /sys/fs/cgroup/systemd
}

plugdev_group_exists() {
	getent group plugdev >/dev/null
}


# During normal installation/upgrade, try to avoid bothering the user
# if the current settings are sane.
if test -z "$DEBCONF_RECONFIGURE"; then
	consolekit_running && exit 0
	systemd_running && exit 0

	if db_get solaar/use_plugdev_group; then
		test "$RET" = true && plugdev_group_exists && exit 0
	else
		# If the group already exists, just use it.
		plugdev_group_exists && db_set solaar/use_plugdev_group true && exit 0
	fi
fi

# If the package hasn't been configured yet, and no seat daemon is running,
# change the default.
if ! db_get solaar/use_plugdev_group; then
	if ! consolekit_running && ! systemd_running; then
		plugdev_group_exists && db_set solaar/use_plugdev_group true
	fi
fi

# update the question template
if consolekit_running; then
	db_subst solaar/use_plugdev_group SEAT_DAEMON_STATUS "the ConsoleKit"
elif systemd_running; then
	db_subst solaar/use_plugdev_group SEAT_DAEMON_STATUS "the systemd"
else
	db_subst solaar/use_plugdev_group SEAT_DAEMON_STATUS "NEITHER"
fi
# ask the question
db_input high solaar/use_plugdev_group || true
db_go || true

exit 0
