#!/usr/bin/sh
# Debian postinst for msime-client, configured by CMakeLists.txt and shipped through CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA.
#
# The Windows installer stops the IME processes on upgrade and its post-install step starts the new ones. On Linux dpkg replaces the files under running user services, which keep executing the old programs until something restarts them. After an upgrade this reloads the systemd user manager of every user logind knows about, so it reads the new unit files, and restarts those of the MSIME services that are running. Services that are not running stay stopped, and socket units are not restarted: they run no program, and restarting a socket whose service is active is refused by systemd and would leave its path removed. Every reachable user also gets a per-user anonymous translation account here; a network failure is ignored and the provider retries later.
#
# The input method hosts are not user services: the IBus host belongs to ibus-daemon and the Fcitx5 addon lives inside fcitx5, so neither can be restarted from here. The IBus host restarts itself on the next focus change and the Fcitx5 addon asks for a restart.
#
# Nothing here may fail the package transaction. Without systemctl or loginctl (containers, non-systemd systems) it does nothing; a user manager that cannot be reached gets the commands that user has to run.

case "$1" in
  configure) ;;
  *) exit 0 ;;
esac

command -v systemctl >/dev/null 2>&1 || exit 0
command -v loginctl >/dev/null 2>&1 || exit 0
users=$(loginctl list-users --no-legend 2>/dev/null) || exit 0

while read -r uid name _; do
  case "$uid" in
    ''|*[!0-9]*) continue ;;
  esac
  if systemctl --user -M "$uid@" daemon-reload </dev/null >/dev/null 2>&1; then
    if command -v systemd-run >/dev/null 2>&1; then
      systemd-run --user -M "$uid@" --wait --collect --quiet /usr/bin/msime-linux-online-provider --ensure-anonymous-account </dev/null >/dev/null 2>&1 || true
    fi
    for unit in msime-linux-online.service msime-linux-voice.service msime-linux-clipboard.service; do
      systemctl --user -M "$uid@" try-restart "$unit" </dev/null >/dev/null 2>&1 || true
    done
  else
    echo "msime-client: could not reach the systemd user manager of ${name:-uid $uid}; that user should restart its services: systemctl --user daemon-reload && systemctl --user try-restart msime-linux-online.service msime-linux-voice.service msime-linux-clipboard.service" >&2
  fi
done <<EOF
$users
EOF

exit 0
