#!/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

bisq_desktop_directories_workaround() {
   if [ -f "/var/lib/usability-misc/do_once/${FUNCNAME[0]}_version_1" ]; then
      return 0
   fi

   ## https://github.com/bisq-network/bisq/issues/848
   mkdir -p /usr/share/desktop-directories

   mkdir -p "/var/lib/usability-misc/do_once"
   touch "/var/lib/usability-misc/do_once/${FUNCNAME[0]}_version_1"
}

systemd_journald_audit_transport_enable() {
   if [ -f "/var/lib/usability-misc/do_once/${FUNCNAME[0]}_version_1" ]; then
      return 0
   fi

   ## Debian no longer enables systemd-journald-audit.socket by default.
   ## https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1038993
   ##
   ## Enable systemd-journald audit transport.
   ## Required by helper-scripts 'apparmor-info' and 'apparmor-watch'.
   deb-systemd-helper enable systemd-journald-audit.socket

   mkdir -p "/var/lib/usability-misc/do_once"
   touch "/var/lib/usability-misc/do_once/${FUNCNAME[0]}_version_1"
}

generate_grub_keyboard_layouts_maybe() {
   if [ -f "/var/lib/usability-misc/do_once/${FUNCNAME[0]}_version_1" ]; then
      return 0
   fi

   ## > Would console-setup (or console-setup-mini) package be discouraged?
   ## @marmarek in Matrix qubes-public
   ## > I guess it's harmless if small, but also not very useful
   if [ -f /usr/share/qubes/marker-vm ]; then
      true "INFO: Skipping 'set-grub-keymap --build-all' because Qubes detected, ok."
      return 0
   fi

   if ! command -v update-grub >/dev/null 2>&1; then
      true "INFO: Skipping 'set-grub-keymap --build-all' because command 'update-grub' is unavailable, ok."
      return 0
   fi

   ## > /usr/bin/set-grub-keymap: ERROR: Cannot proceed with generating GRUB keymap because requirements are unavailable. The 'ckbcomp' program is unavailable in the PATH environment variable or not installed. Is package 'console-setup' or 'console-setup-mini' installed?
   if ! command -v ckbcomp >/dev/null 2>&1; then
      true "INFO: Skipping 'set-grub-keymap --build-all' because command 'ckbcomp' is unavailable, ok."
      return 0
   fi

   if [ -d /boot/grub/kb_layouts ]; then
      true "INFO: Skipping 'set-grub-keymap --build-all' because folder '/boot/grub/kb_layouts' already exists, ok."
      return 0
   fi

   ## Use '--no-live-changes' because this postinst script will run 'update-grub' if available anyhow.
   if ! set-grub-keymap --build-all --no-live-changes >/dev/null 2>&1; then
      printf '%s\n' "INFO: Minor issue. Command 'set-grub-keymap --build-all' failed. This means, the GRUB boot menu will have no keyboard layout selection menu, which is a minor usability feature. For most users, no action is required. If you know to need this feature, please execute 'sudo set-grub-keymap --build-all' (in sysmaint session)."
      return 0
   fi

   printf '%s\n' "INFO: Command 'set-grub-keymap --build-all' success."

   mkdir -p "/var/lib/usability-misc/do_once"
   touch "/var/lib/usability-misc/do_once/${FUNCNAME[0]}_version_1"
}

shim_signed_mok_setup() {
   if [ -f "/var/lib/usability-misc/do_once/${FUNCNAME[0]}_version_1" ]; then
      return 0
   fi

   ## '--default-true': return true if detection fails
   if ischroot --default-true ; then
      return 0
   fi

   ## Provided by package 'helper-scripts'.
   shim-signed-mok-setup || true

   mkdir -p "/var/lib/usability-misc/do_once"
   touch "/var/lib/usability-misc/do_once/${FUNCNAME[0]}_version_1"
}

true "
#####################################################################
## INFO: BEGIN: $DPKG_MAINTSCRIPT_PACKAGE $DPKG_MAINTSCRIPT_NAME $*
#####################################################################
"

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

      adduser --home /run/tunnel --no-create-home --quiet --system --group --shell /bin/false "tunnel" || true

      generate_grub_keyboard_layouts_maybe

      true "INFO: End configuring $DPKG_MAINTSCRIPT_PACKAGE."

      ;;

   *)
      ;;
esac

bisq_desktop_directories_workaround
systemd_journald_audit_transport_enable
shim_signed_mok_setup

if [ -d /run/systemd/system ] ; then
        systemd-tmpfiles --create /usr/lib/tmpfiles.d/50_openvpn_unpriv.conf >/dev/null || true
fi

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

## https://phabricator.whonix.org/T377
## Debian has no update-grub trigger yet:
## https://bugs.debian.org/481542
if command -v update-grub >/dev/null 2>&1; then
   update-grub || \
      echo "$DPKG_MAINTSCRIPT_PACKAGE $DPKG_MAINTSCRIPT_NAME ERROR: Running \
'update-grub' failed with exit code $?. $DPKG_MAINTSCRIPT_PACKAGE is most \
likely only the trigger, not the cause. Unless you know this is not an issue, \
you should fix running 'update-grub', otherwise your system might no longer \
boot." >&2
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
