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

if [ -x /usr/libexec/helper-scripts/torsocks-remove-ld-preload ]; then
   source /usr/libexec/helper-scripts/torsocks-remove-ld-preload
fi

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

       adduser --home /run/sdwdate --no-create-home --quiet --system --group sdwdate || true

       ## user `sdwdate` legacy home folder migration
       sdwdate_home="$(getent passwd sdwdate | cut -d: -f6)" || true
       if [ "$sdwdate_home" = "/nonexistent" ]; then
         if [ -d /run/systemd/system ]; then
            ## Make /lib/systemd/system/sdwdate.service `RestartSec=10s` take effect.
            systemctl --system daemon-reload >/dev/null || true
         fi
         ## Need to kill sdwdate, otherwise we cannot change user home using `usermod`.
         ## `usermod` would complain:
         ## usermod: user sdwdate is currently used by process 2491
         ##
         ## Making sure pgrep will not match this postinst script.
         kill -s sigterm $(pgrep -f /usr/bin/sdwdate) 2>/dev/null || true
         ## Allow sigterm to actually kill sdwdate.
         sleep 5
       fi
       ## Change user home of user sdwdate to /run/sdwdate.
       ## Legacy because previously was created using folder /nonexistent.
       ## Folder /run/sdwdate gets created by /usr/lib/tmpfiles.d/sdwdate.conf.
       usermod -m -d /run/sdwdate sdwdate || true

       mkdir -p /run/sdwdate || true
       ## Fix bug since /run/sdwdate might be owned by onion-grater.
       chown sdwdate:sdwdate /run/sdwdate || true

       ## Add sdwdate to group debian-tor so it can read
       ## /run/tor/control.authcookie which is required to check if Tor has
       ## already successfully established a circuit before fetching time.
       adduser sdwdate debian-tor

       ## Build sclockadj program.
       if ! pushd -- /usr/src/sdwdate >dev/null ; then
         printf '%s\n' "$0: Cannot change to '/usr/src/sdwdate'!" >&2
         exit 1
       fi
       if ! make ; then
         printf '%s\n' "$0: Error building sclockadj!" >&2
         exit 1
       fi
       if ! rm -f -- /usr/libexec/sdwdate/sclockadj ; then
         printf '%s\n' "$0: Cannot remove old sclockadj!" >&2
         exit 1
       fi
       if ! mv -- sclockadj /usr/libexec/sdwdate/sclockadj ; then
         printf '%s\n' "$0: Cannot move sclockadj to '/usr/libexec/sdwdate'!" >&2
         exit 1
       fi
       popd >/dev/null
    ;;

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

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

## Deletes /etc/systemd/system/sysinit.target.wants/systemd-timesyncd.service.
## Otherwise timedatectl still thinks systemd-timesyncd is enabled.
timedatectl set-ntp false >/dev/null 2>&1 || true
## Make sure it gets really deleted even if timedatectl does not work.
rm --force /etc/systemd/system/sysinit.target.wants/systemd-timesyncd.service

## Add arch-specific syscalls to the seccomp filter.
if ! [ -d /lib/systemd/system/sdwdate.service.d/ ]; then
  mkdir --parents /lib/systemd/system/sdwdate.service.d/
fi

## Legacy 2023.
rm -f /lib/systemd/system/sdwdate.service.d/20_arch_syscall_whitelist.conf

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
