#!/bin/bash

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

source /usr/libexec/helper-scripts/torsocks-remove-ld-preload

case "$1" in
   configure)
      ## Compatibility with anon-ws-disable-stacked-tor.
      adduser debian-tor 2>/dev/null || true

      adduser --home /var/lib/canary --no-create-home --quiet --system --group canary || true
      ## Allow running tor-circuit-established-check.
      adduser canary debian-tor

      adduser --home /var/lib/systemcheck --no-create-home --quiet --system --group systemcheck || true

      ## Add systemcheck to group debian-tor so it can read
      ## /run/tor/control.authcookie which is required to check for Tor
      ## bootstrap test.
      adduser systemcheck debian-tor

      ## Add systemcheck to group systemd-journal so it can use journalctl.
      adduser systemcheck systemd-journal

      ## updatecheck no longer uses a systemd WantedBy= line to autostart,
      ## inform the system of this
      if [ ! -x "$(command -v systemctl)" ]; then
         true "INFO: systemctl command unavailable."
         return 0
      fi
      systemctl --global disable updatecheck.service &>/dev/null || true
   ;;

   abort-upgrade|abort-remove|abort-deconfigure)
   ;;

   *)
      echo "$DPKG_MAINTSCRIPT_NAME called with unknown argument \`$1'" >&2
      exit 1
   ;;
esac

## workaround for 'dh_installinit should run systemd-tmpfiles if a
## /usr/lib/tmpfiles.d/ snippet gets shipped for systemd-only packages
## also' - http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=795519
# In case this system is running systemd, we need to ensure that all
# necessary tmpfiles (if any) are created before starting.
if [ -d /run/systemd/system ] ; then
	systemd-tmpfiles --create /usr/lib/tmpfiles.d/systemcheck.conf >/dev/null || true
fi

true "INFO: debhelper beginning here."

#DEBHELPER#

true "INFO: Done with debhelper."

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

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