#!/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 $@
#####################################################################
"

#case "$1" in
#   configure)
#      true "INFO: Configuring $DPKG_MAINTSCRIPT_PACKAGE..."
#
#      true "INFO: Checking if function enable_echo is available..."
#      if [ "$(type -t enable_echo)" = "function" ] ; then
#         true "INFO: Yes, function enable_echo is available."
#         ## Not trying to enable echo, if DEBDEBUG is set to 1, because then
#         ## echo is already enabled.
#         if [ ! "$DEBDEBUG" = "1" ]; then
#            true "INFO: DEBDEBUG is not set to 1, therefore running enable_echo..."
#            ## provided by pre.bsh
#            enable_echo
#         fi
#      fi
#
#      if [ "$DERIVATIVE_APT_REPOSITORY_OPTS" = "" ]; then
#         if ischroot --default-false ; then
#            echo "INFO: Variable DERIVATIVE_APT_REPOSITORY_OPTS is empty, not calling repository-dist tool."
#         else
#            true "INFO: Variable DERIVATIVE_APT_REPOSITORY_OPTS is empty, not calling repository-dist tool."
#         fi
#      else
#         ## https://www.kicksecure.com/wiki/Trust#APT_Repository_Default_Settings
#         if [ -o xtrace ]; then
#            bash -x repository-dist $DERIVATIVE_APT_REPOSITORY_OPTS
#         else
#            echo "$DPKG_MAINTSCRIPT_PACKAGE $DPKG_MAINTSCRIPT_NAME Running: repository-dist $DERIVATIVE_APT_REPOSITORY_OPTS"
#            repository-dist $DERIVATIVE_APT_REPOSITORY_OPTS
#         fi
#      fi
#
#      true "INFO: End configuring $DPKG_MAINTSCRIPT_PACKAGE."
#
#      ;;
#
#   *)
#      ;;
#esac

true "INFO: debhelper beginning here."

# Automatically added by dh_installdeb/13.24.2
dpkg-maintscript-helper rm_conffile /etc/whonix.d/30_aptrepository_default -- "$@"
# End automatically added section
# Automatically added by dh_installsystemd/13.24.2
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
	# The following line should be removed in trixie or trixie+1
	deb-systemd-helper unmask 'repository-dist-initializer.service' >/dev/null || true

	# was-enabled defaults to true, so new installations run enable.
	if deb-systemd-helper --quiet was-enabled 'repository-dist-initializer.service'; then
		# Enables the unit on first installation, creates new
		# symlinks on upgrades if the unit file has changed.
		deb-systemd-helper enable 'repository-dist-initializer.service' >/dev/null || true
	else
		# Update the statefile to add new symlinks (if any), which need to be
		# cleaned up on purge. Also remove old symlinks.
		deb-systemd-helper update-state 'repository-dist-initializer.service' >/dev/null || true
	fi
fi
# End automatically added section
# Automatically added by dh_installsystemd/13.24.2
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
	if [ -d /run/systemd/system ]; then
		systemctl --system daemon-reload >/dev/null || true
		if [ -n "$2" ]; then
			_dh_action=restart
		else
			_dh_action=start
		fi
		deb-systemd-invoke $_dh_action 'repository-dist-initializer.service' >/dev/null || true
	fi
fi
# End automatically added section


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
