#!/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 -x
set -e

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

true "1: '$1'"
true "2: '$2'"

power_savings_disable_in_vms() {
   if [ "$SUDO_USER" = "" ]; then
      return 0
   fi

   if ischroot --default-false ; then
      return 0
   fi

   if [ -x "/etc/profile.d/20_power_savings_disable_in_vms.sh" ]; then
      ## '/etc/profile.d/20_power_savings_disable_in_vms.sh' will exit zero and take no action
      ## in case there is no virtualization.
      ## NOTE: Might fail because this packages does not 'Depends:' 'sudo' to simplify dependencies.
      ##       Not important.
      sudo --non-interactive -u "$SUDO_USER" "/etc/profile.d/20_power_savings_disable_in_vms.sh" >/dev/null 2>&1 || true
   fi
}

vbox_guest_installer() {
   true "dist_build_virtualbox: $dist_build_virtualbox"
   if [ "$dist_build_virtualbox" = "true" ] \
      || [ -f '/var/lib/dist-base-files/live_build' ] \
      || [ "$(systemd-detect-virt)" = "oracle" ]; then
      true "INFO: Running vbox-guest-installer..."
      vbox-guest-installer || true
      true "INFO: vbox-guest-installer done."
      return 0
   fi
   true "INFO: Not running vbox-guest-installer because neither dist_build_virtualbox is set to 'true', nor does '/var/lib/dist-base-files/live_build' exist, nor systemd-detect-virt output was 'oracle'."
}

vbox_guest_installer_maybe() {
   if [ -f "/var/lib/vm-config-dist/do_once/${FUNCNAME[0]}_version_1" ]; then
      true "INFO: Upgrade, and vbox_guest_installer has already been run once. Therefore not calling function vbox_guest_installer."
      return 0
   fi
   mkdir --parents "/var/lib/vm-config-dist/do_once"

   true "INFO: Upgrade, and vbox_guest_installer has not yet been run once. Therefore calling function vbox_guest_installer."
   vbox_guest_installer

   touch "/var/lib/vm-config-dist/do_once/${FUNCNAME[0]}_version_1"
}

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

      ## Only disables power savings in TTYs now, a logout/login cycle or
      ## reboot is needed to fully disable power savings.
      #power_savings_disable_in_vms

      ## https://askubuntu.com/questions/101962/how-to-run-a-script-only-during-first-install-of-a-package-and-during-upgrades
      if [ "$2" = "" ]; then
        true "INFO: Initial installation. Therefore calling function vbox_guest_installer."
        vbox_guest_installer
      else
        vbox_guest_installer_maybe
      fi

      true "INFO: End configuring $DPKG_MAINTSCRIPT_PACKAGE."
      ;;

    triggered)
      true "INFO: triggered $DPKG_MAINTSCRIPT_PACKAGE..."
      vbox_guest_installer
      true "INFO: triggered $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
