#!/bin/sh
# Removing the package does not stop a running daemon: it keeps serving the
# mount from the deleted binary, so files deleted under the mount afterwards,
# `rm -rf ~/ProtonDrive` included, are still deleted on Proton Drive. Stop every
# logged-in user's service first; anything still queued stays on disk for the
# next install. Same loop as packaging/proton-drive.install.
set -e

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

for dir in /run/user/*; do
  user=$(id -nu "${dir##*/}" 2>/dev/null) || continue
  if runuser -u "$user" -- env XDG_RUNTIME_DIR="$dir" \
    systemctl --user --quiet is-active proton-drive.service 2>/dev/null; then
    echo "Stopping Proton Drive for $user, which unmounts its drive."
    runuser -u "$user" -- env XDG_RUNTIME_DIR="$dir" \
      systemctl --user stop proton-drive.service 2>/dev/null || :
  fi
done
