#!/bin/bash

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

#### meta start
#### project Whonix
#### category tor
#### gateway_only yes
#### description
## Add account "user" to the group "debian-tor", so account "user" can access Tor's control port.
## Account "user" already exists thanks to the dist-base-files package.
#### meta end

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

vanguards_enable_and_start_once() {
   if [ -f "/var/lib/anon-dist/do_once/${FUNCNAME}_version_1" ]; then
      return 0
   fi
   ## enable vanguards systemd unit file by default
   ## https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=948975
   systemctl --no-pager --no-block enable vanguards || true
   systemctl --no-pager --no-block start vanguards || true
   mkdir --parents "/var/lib/anon-dist/do_once"
   touch "/var/lib/anon-dist/do_once/${FUNCNAME}_version_1"
}

vanguards_disable_and_stop_once() {
   if [ -f "/var/lib/anon-dist/do_once/${FUNCNAME}_version_1" ]; then
      return 0
   fi
   ## https://forums.whonix.org/t/connections-drop-on-tor-0-4-8-9/17702
   ## https://gitlab.torproject.org/tpo/core/tor/-/issues/40892
   ## https://github.com/mikeperry-tor/vanguards/issues/100
   ## https://github.com/QubesOS/qubes-issues/issues/8726
   systemctl --no-pager --no-block disable vanguards || true
   systemctl --no-pager --no-block stop vanguards || true
   mkdir --parents "/var/lib/anon-dist/do_once"
   touch "/var/lib/anon-dist/do_once/${FUNCNAME}_version_1"
}

configure_function() {
   mkdir -p /usr/local/etc/torrc.d

   addgroup --quiet debian-tor 2>/dev/null || true

   ## Add account "user" to the group "debian-tor", so account "user" can access Tor's control port.
   ## Account "user" already exists thanks to the dist-base-files package.
   adduser --quiet user debian-tor

   ## Account 'sysmaint' may not exist at this time.
   adduser --quiet sysmaint debian-tor 2>/dev/null || true

   #vanguards_enable_and_start_once
   #vanguards_disable_and_stop_once

   ## force-reload Tor, if running.
   service_tor_status_exit_code="0"
   systemctl --no-pager --no-block status tor@default >/dev/null 2>&1 || { service_tor_status_exit_code="$?" ; true; };
   if [ "$service_tor_status_exit_code" = "0" ]; then
      systemctl --no-pager --no-block force-reload tor@default  || true
   fi
}

## Make systemd drop-in config snippets take effect.
if [ -d /run/systemd/system ]; then
        systemctl --system daemon-reload >/dev/null || true
fi

case "$1" in
   configure)
      true "INFO: Configuring $DPKG_MAINTSCRIPT_PACKAGE..."

      configure_function

      true "INFO: End configuring $DPKG_MAINTSCRIPT_PACKAGE."

      ;;

   *)
      ;;
esac

true "INFO: debhelper beginning here."

#DEBHELPER#

true "INFO: Done with debhelper."

## Too slow.
# debsums_output="$(debsums -ce | grep torrc)" || true
# if [ ! "$debsums_output" = "" ]; then
#     echo "$0: INFO: output of command 'debsums -ce | grep torrc':"
#     echo "$debsums_output"
# fi

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

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