#!/bin/bash

## Copyright (C) 2025 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

if [ -f /usr/libexec/helper-scripts/pre.bsh ]; then
   source /usr/libexec/helper-scripts/pre.bsh
fi

set -e

true "
#####################################################################
## INFO: BEGIN: $DPKG_MAINTSCRIPT_PACKAGE $DPKG_MAINTSCRIPT_NAME $@
#####################################################################
"

true "INFO: debhelper beginning here."

#DEBHELPER#

true "INFO: Done with debhelper."

if [ "$1" = 'remove' ] || [ "$1" = 'purge' ]; then
  ## Under Qubes OS, removing and then reinstalling privleap in the same
  ## session results in a crash when deb-systemd-invoke attempts to (re)start
  ## privleapd at postinst runtime. Stop privleapd when uninstalled to prevent
  ## this from occurring.
  if [ -d /run/systemd/system ] \
    && [ -x "$(command -v systemctl)" ]; then
    deb-systemd-invoke stop privleapd
  fi
fi

## Make sure if the user reinstalls the package after purge, the systemd unit
## is automatically enabled.
if [ "$1" = 'purge' ]; then
   deb-systemd-helper purge privleapd.service
fi

true "
#####################################################################
## INFO: END  : $DPKG_MAINTSCRIPT_PACKAGE $DPKG_MAINTSCRIPT_NAME $@
#####################################################################
"

## Explicitly "exit 0", so eventually trapped errors can be ignored.
exit 0
