#!/bin/sh

set -e

_systemctl() {
    if [ -z "$DPKG_ROOT" ] && [ -d /run/systemd/system ]; then
        systemctl "$@"
    fi
}

_update_catalog() {
    journalctl ${DPKG_ROOT:+--root="$DPKG_ROOT"} --update-catalog || true
}

_update_binfmt() {
    if [ "$(_systemctl show -P LoadState systemd-binfmt.service)" != "masked" ]; then
        _systemctl restart systemd-binfmt.service || true
    fi
}

_update_sysctl() {
    if [ "$(_systemctl show -P LoadState systemd-sysctl.service)" != "masked" ]; then
        _systemctl restart systemd-sysctl.service || true
    fi
}

_restart_managers() {
    if [ -z "$DPKG_ROOT" ] && [ -d /run/systemd/system ]; then
        # Re-exec user instances so that running user managers are updated too.
        # Note that this is asynchronous, but we can't use
        # D-Bus as dbus-user-session is not guaranteed to be available.
        deb-systemd-invoke --user --no-dbus daemon-reexec || true
        deb-systemd-invoke --no-dbus daemon-reexec || true
    fi
}

# Update Message Catalogs database and binfmt registrations in response to dpkg triggers
if [ "$1" = "triggered" ]; then
    shift
    for trigger in $@; do
        case $trigger in
            /usr/lib/systemd/catalog)
                _update_catalog
                ;;
            /usr/lib/binfmt.d)
                _update_binfmt
                ;;
            /usr/lib/sysctl.d)
                _update_sysctl
                ;;
            libc-upgrade)
                _restart_managers
                ;;
        esac
    done
    exit 0
fi

# Enable getty, remote-fs.target and systemd-pstore by default on new installs
if [ -z "$2" ]; then
    systemctl ${DPKG_ROOT:+--root="$DPKG_ROOT"} enable remote-fs.target || true
    systemctl ${DPKG_ROOT:+--root="$DPKG_ROOT"} enable systemd-pstore.service || true
fi

# This is no longer statically enabled since v260
# Enable autovt@.service and getty@tty1 on upgrades and new installs
if dpkg --compare-versions "$2" lt "260~rc4-2~"; then
    systemctl ${DPKG_ROOT:+--root="$DPKG_ROOT"} enable getty@.service || true
fi

# Create /etc/machine-id
systemd-machine-id-setup ${DPKG_ROOT:+--root="$DPKG_ROOT"}

# Enable persistent journal, in auto-mode, by default on new installs
if [ -z "$2" ]; then
    mkdir -p "$DPKG_ROOT/var/log/journal"
fi

# skip daemon-reexec and try-restarts during shutdown to avoid hitting LP: #1803391
if [ -n "$2" ] && [ "$(systemctl is-system-running)" != "stopping" ]; then
    _restart_managers
fi

# Initial update of the Message Catalogs database
_update_catalog

# Automatically added by dh_installsysusers/14.3ubuntu1
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
   systemd-sysusers ${DPKG_ROOT:+--root="$DPKG_ROOT"} basic.conf systemd-journal.conf systemd-network.conf
fi
# End automatically added section
# Automatically added by dh_installtmpfiles/14.3ubuntu1
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
	systemd-tmpfiles ${DPKG_ROOT:+--root="$DPKG_ROOT"} --create 20-systemd-osc-context.conf 20-systemd-shell-extra.conf 20-systemd-ssh-generator.conf 20-systemd-stub.conf 20-systemd-varlink.conf credstore.conf debian.conf home.conf journal-nocow.conf provision.conf systemd-network.conf systemd-nologin.conf systemd-pstore.conf systemd-tmp.conf systemd.conf tmp.conf var.conf x11.conf
fi
# End automatically added section
# Automatically added by dh_installdeb/14.3ubuntu1
dpkg-maintscript-helper rm_conffile /etc/systemd/resolved.conf 251.3-2\~ -- "$@"
# End automatically added section
# Automatically added by dh_installsystemd/14.3ubuntu1
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
	if deb-systemd-helper debian-installed 'systemd-networkd.service'; then
		# The following line should be removed in trixie or trixie+1
		deb-systemd-helper unmask 'systemd-networkd.service' >/dev/null || true

		if deb-systemd-helper --quiet was-enabled 'systemd-networkd.service'; then
			# Create new symlinks, if any.
			deb-systemd-helper enable 'systemd-networkd.service' >/dev/null || true
		fi
	fi

	# 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 'systemd-networkd.service' >/dev/null || true
fi
# End automatically added section
# Automatically added by dh_installsystemd/14.3ubuntu1
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
	if [ -z "$DPKG_ROOT" ] && [ -d /run/systemd/system ]; then
		systemctl --system daemon-reload >/dev/null || true
		if [ -n "$2" ]; then
			deb-systemd-invoke try-restart 'systemd-journald.service' 'systemd-networkd.service' >/dev/null || true
		fi
	fi
fi
# End automatically added section

