#!/bin/sh
set -e
case "$1" in
	install|upgrade)
		# version 6 is a user daemon so clear out old system level files

		if [ -f /usr/lib/systemd/system/psd.service ]; then
			[ "$(systemctl is-enabled psd)" = "enabled" ] &&
				systemctl disable psd.service &>/dev/null

			[ "$(systemctl is-active psd)" = "active" ] &&
				systemctl stop psd.service &>/dev/null
		fi

		for i in $(users); do
			if [ "$(su $i -s /bin/bash -c 'XDG_RUNTIME_DIR=/run/user/$UID systemctl --user is-active psd')" = "active" ]; then
				echo "--> Internal changes to psd require it to be stopped before the update."
				echo "--> Restart it manually:  systemctl --user start psd"
				su $i -s /bin/bash -c 'XDG_RUNTIME_DIR=/run/user/$UID systemctl --user stop psd.service'
			fi
			return 0
		done

		;;

	abort-upgrade)
		/bin/true
		;;

	*)
		echo "preinst called with unknown argument \`$1'" >&2
		exit 1
		;;
esac
exit 0
