function post_install() {
#!/bin/sh
# After the package is installed or upgraded: the two user services are
# enabled for every user, restarted for each user signed in now (so an
# upgrade runs the new programs at once), and the webcam's kernel module is
# set up.
#
# One script for every package (packaging/linux/nfpm.yaml): dpkg calls it
# with "configure", rpm with the number of copies installed (1 or 2), and
# pacman, after an install or an upgrade, with the new version.
set -e

units="uniconnd.service uniconn-session-agent.service"

# The users signed in now, by name.
signed_in() {
  command -v loginctl >/dev/null 2>&1 || return 0
  loginctl list-users --no-legend 2>/dev/null | while read -r uid name _; do
    [ "$uid" -ge 1000 ] 2>/dev/null && printf '%s\n' "$name"
  done
}

case "${1:-}" in
  configure|[0-9]*)
    # shellcheck disable=SC2086
    systemctl --global enable $units >/dev/null 2>&1 || true
    for name in $(signed_in); do
      systemctl --user -M "$name@" daemon-reload >/dev/null 2>&1 || continue
      for unit in $units; do
        systemctl --user -M "$name@" restart "$unit" >/dev/null 2>&1 || true
      done
    done
    # The webcam is wanted, not needed: without it every other feature works.
    if ! /usr/lib/uniconn/uniconn-webcam-setup; then
      echo "UniConn: the webcam is not set up; the rest of UniConn works. Fix what is said above, then run: sudo /usr/lib/uniconn/uniconn-webcam-setup" >&2
    fi
    ;;
esac
exit 0

}

function post_remove() {
#!/bin/sh
# After removal: each signed-in user's service manager forgets the units.
#
# One script for every package: dpkg calls it with "remove" or "purge", rpm
# with the number of copies left (0 on a removal), and pacman only on a
# removal, with the version gone.
set -e

case "${1:-}" in
  remove|purge|0|*.*)
    if command -v loginctl >/dev/null 2>&1; then
      loginctl list-users --no-legend 2>/dev/null | while read -r uid name _; do
        [ "$uid" -ge 1000 ] 2>/dev/null || continue
        systemctl --user -M "$name@" daemon-reload >/dev/null 2>&1 || true
      done
    fi
    ;;
esac
exit 0

}

function post_upgrade() {
#!/bin/sh
# After the package is installed or upgraded: the two user services are
# enabled for every user, restarted for each user signed in now (so an
# upgrade runs the new programs at once), and the webcam's kernel module is
# set up.
#
# One script for every package (packaging/linux/nfpm.yaml): dpkg calls it
# with "configure", rpm with the number of copies installed (1 or 2), and
# pacman, after an install or an upgrade, with the new version.
set -e

units="uniconnd.service uniconn-session-agent.service"

# The users signed in now, by name.
signed_in() {
  command -v loginctl >/dev/null 2>&1 || return 0
  loginctl list-users --no-legend 2>/dev/null | while read -r uid name _; do
    [ "$uid" -ge 1000 ] 2>/dev/null && printf '%s\n' "$name"
  done
}

case "${1:-}" in
  configure|[0-9]*)
    # shellcheck disable=SC2086
    systemctl --global enable $units >/dev/null 2>&1 || true
    for name in $(signed_in); do
      systemctl --user -M "$name@" daemon-reload >/dev/null 2>&1 || continue
      for unit in $units; do
        systemctl --user -M "$name@" restart "$unit" >/dev/null 2>&1 || true
      done
    done
    # The webcam is wanted, not needed: without it every other feature works.
    if ! /usr/lib/uniconn/uniconn-webcam-setup; then
      echo "UniConn: the webcam is not set up; the rest of UniConn works. Fix what is said above, then run: sudo /usr/lib/uniconn/uniconn-webcam-setup" >&2
    fi
    ;;
esac
exit 0

}

function pre_remove() {
#!/bin/sh
# Before the package is removed (not upgraded): its services stop for every
# user signed in and are no longer enabled, and the webcam's set-up goes.
# Each user's own data, in their home, is left as it is.
#
# One script for every package: dpkg calls it with "remove" (or "upgrade",
# which is left alone), rpm with the number of copies left after it (0 on a
# removal, 1 on an upgrade), and pacman only on a removal, with the version
# going.
set -e

units="uniconnd.service uniconn-session-agent.service"

signed_in() {
  command -v loginctl >/dev/null 2>&1 || return 0
  loginctl list-users --no-legend 2>/dev/null | while read -r uid name _; do
    [ "$uid" -ge 1000 ] 2>/dev/null && printf '%s\n' "$name"
  done
}

case "${1:-}" in
  remove|deconfigure|0|*.*)
    for name in $(signed_in); do
      # shellcheck disable=SC2086
      systemctl --user -M "$name@" stop $units >/dev/null 2>&1 || true
    done
    # shellcheck disable=SC2086
    systemctl --global disable $units >/dev/null 2>&1 || true
    /usr/lib/uniconn/uniconn-webcam-setup --remove >/dev/null 2>&1 || true
    ;;
esac
exit 0

}

