#!/bin/bash

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

#### meta start
#### project Whonix
#### category networking and firewall
#### description
## Creates linux user accounts used by firewall script
## clearnet tunnel notunnel systemcheck sdwdate updatesproxycheck.
##
## Creates empty /etc/whonix_firewall.d/50_user.conf which is not owned
## by any package if not existing.
#### 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 $@
#####################################################################
"

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

    user_account_list="clearnet tunnel notunnel systemcheck sdwdate updatesproxycheck"
    for user_account in $user_account_list; do
      ## Create users so we do not have to depend on these packages.
      ## https://web.archive.org/web/20150908082420/https://lintian.debian.org/tags/maintainer-script-should-not-use-adduser-system-without-home.html
      adduser --home "/run/$user_account" --no-create-home --quiet --system --group --shell /bin/false "$user_account" || true
    done

    ## Whonix start menu entry is using:
    ## xdg-open /etc/whonix_firewall.d/50_user.conf
    ## In Xfce this results in:
    ## exo-open /etc/whonix_firewall.d/50_user.conf
    ## Which fails if the file is non-existing.
    if [ ! -f /etc/whonix_firewall.d/50_user.conf ]; then
      mkdir -p /etc/whonix_firewall.d || true
      touch /etc/whonix_firewall.d/50_user.conf || true
    fi

    true "INFO: End configuring $DPKG_MAINTSCRIPT_PACKAGE."

    ;;

  *) ;;

esac

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
