#!/bin/bash

## Copyright (C) 2024 - 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 $@
#####################################################################
"

suggest_sysmaint_account_removal() {
   echo "$DPKG_MAINTSCRIPT_PACKAGE $DPKG_MAINTSCRIPT_NAME INFO: Uninstalling \
user-sysmaint-split. AFTER REBOOT, you may want to remove the sysmaint user \
account by running 'sudo deluser --remove-home sysmaint'. DO NOT run this \
before rebooting!" >&2
}

true "INFO: debhelper beginning here."

#DEBHELPER#

true "INFO: Done with debhelper."

case "$1" in
   remove)
      suggest_sysmaint_account_removal
      ;;
   purge)
      suggest_sysmaint_account_removal
      safe-rm -rf --verbose -- /etc/user-sysmaint-split.conf.d
      safe-rm -rf --verbose -- /var/lib/user-sysmaint-split/do_once/enable_sysmaint_autologin_version_*
      safe-rm -rf --verbose -- /var/lib/user-sysmaint-split/do_once/sysmaint_user_creation_and_groups_setup_version_*
      ;;
esac

if command -v update-grub >/dev/null 2>&1; then
   update-grub || \
      echo "$DPKG_MAINTSCRIPT_PACKAGE $DPKG_MAINTSCRIPT_NAME ERROR: Running \
'update-grub' failed with exit code $?. $DPKG_MAINTSCRIPT_PACKAGE is most \
likely only the trigger, not the cause. Unless you know this is not an issue, \
you should fix running 'update-grub', otherwise your system might no longer \
boot." >&2
fi

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

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