echo "Creating user account for the Linglong daemon owner ..."
systemd-sysusers linglong.conf

echo "Creating daemon directories for Linglong ..."
systemd-tmpfiles --create linglong.conf

# Borrowed from Debian.
_systemctl() {
	if [ -z "$DPKG_ROOT" ] && [ -d /run/systemd/system ]; then
		systemctl "$@" 1>&2
	else
		return 127
	fi
}

# Add `|| true' just to be safe, but this shouldn't be needed unless
# the package upgrade sequence is messed up (i.e., whilst updating
# OpenSSL, breaking systemd programs).
_systemctl daemon-reload || true

# Only preset these services for a fresh installation
# i.e. installing for the first time or after purging
if [ "$1" = configure ] && [ -z "$2" ]; then
	_systemctl preset org.deepin.linglong.PackageManager.service || true
fi

if ! systemd-detect-virt --container; then
	echo "Reloading D-Bus configurations ..."
	dbus-send \
		--system \
		--type=method_call \
		--dest=org.freedesktop.DBus / org.freedesktop.DBus.ReloadConfig
fi
