#!/bin/sh

set -e

reboot_required() {
    if [ "$1" -eq 1 ]; then
        echo "Upgrading to a newer dbus-broker version requires a reboot:" >&2
    else
        echo "Replacing the running dbus-daemon with dbus-broker requires a reboot:" >&2
    fi
    echo "please reboot the system when convenient." >&2

    # Trigger an update notification that recommends a reboot.
    touch /var/run/reboot-required || true

    if ! grep -Fqsx dbus-broker /run/reboot-required.pkgs; then
        echo dbus-broker >> /run/reboot-required.pkgs || true
    fi
}

if [ "$1" = triggered ] && systemctl --quiet is-active dbus-broker.service; then
    systemctl reload dbus-broker || true
    exit 0
fi

if [ "$1" = configure ]; then
    # Restarting the system bus is not supported yet. On upgrades do
    # not restart the broker but recomment a reboot if the service is
    # running. Replacing dbus-deamon also requires a reboot.

    # dbus-daemon depends on systemd but if we are replacing
    # dbus-broker systemd may not be running yet, so fallback to sysv-utils.
    if [ -z "$2" ]; then
        if [ -d /run/systemd/system ]; then
            # Don't fallback to sysvs-utils if definitely running under systemd
            if systemctl --quiet is-active dbus.service; then
                reboot_required 0
            fi
        elif command -v pidof >/dev/null 2>&1 && pidof -c /bin/dbus-daemon /usr/bin/dbus-daemon >/dev/null; then
            reboot_required 0
        fi
    fi

    # dbus-broker depends on systemd thus we can use its functionality
    if [ -n "$2" ] && systemctl --quiet is-active dbus-broker.service; then
        reboot_required 1

        # In case we got new configuration files
        systemctl reload dbus-broker || true
    fi
fi

# Automatically added by dh_installsystemduser/13.14.1ubuntu5
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
	if [ -z "${DPKG_ROOT:-}" ] ; then
		# The following line should be removed in trixie or trixie+1
		deb-systemd-helper --user unmask 'dbus-broker.service' >/dev/null || true

		# was-enabled defaults to true, so new installations run enable.
		if deb-systemd-helper --quiet --user was-enabled 'dbus-broker.service' ; then
			# Enables the unit on first installation, creates new
			# symlinks on upgrades if the unit file has changed.
			deb-systemd-helper --user enable 'dbus-broker.service' >/dev/null || true
		else
			# Update the statefile to add new symlinks (if any), which need to be
			# cleaned up on purge. Also remove old symlinks.
			deb-systemd-helper --user update-state 'dbus-broker.service' >/dev/null || true
		fi
	fi
fi
# End automatically added section
# Automatically added by dh_installsystemd/13.14.1ubuntu5
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
	# The following line should be removed in trixie or trixie+1
	deb-systemd-helper unmask 'dbus-broker.service' >/dev/null || true

	# was-enabled defaults to true, so new installations run enable.
	if deb-systemd-helper --quiet was-enabled 'dbus-broker.service'; then
		# Enables the unit on first installation, creates new
		# symlinks on upgrades if the unit file has changed.
		deb-systemd-helper enable 'dbus-broker.service' >/dev/null || true
	else
		# Update the statefile to add new symlinks (if any), which need to be
		# cleaned up on purge. Also remove old symlinks.
		deb-systemd-helper update-state 'dbus-broker.service' >/dev/null || true
	fi
fi
# End automatically added section

