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

get_build_version() {
   ## fallback
   dist_build_version="Could not read Whonix Build Version File. (Code: 3) Please report this bug!"

   local build_version_file
   if [ -f "/usr/share/whonix/build_version" ]; then
      ## Legacy.
      build_version_file="/usr/share/whonix/build_version"
   elif [ -f "/var/lib/anon-dist/build_version" ]; then
      ## Legacy.
      build_version_file="/var/lib/anon-dist/build_version"
   elif [ -f "/var/lib/dist-base-files/build_version" ]; then
      build_version_file="/var/lib/dist-base-files/build_version"
   fi

   if [ "$build_version_file" = "" ]; then
      dist_build_version="There is no Whonix Build Version File. (Code: 4) Please report this bug!"
   else
      if [ -f "$build_version_file" ]; then
         dist_build_version="$(cat "$build_version_file")"
      fi
   fi

   ## fallback
   if [ "$dist_build_version" = "" ]; then
      dist_build_version="Could not read Whonix Build Version File. (Code: 5) Please report this bug!"
   fi

   true "dist_build_version: $dist_build_version"
}

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

   ## https://git-tails.immerda.ch/tails/tree/config/chroot_local-preseed/firmware
   echo b43-fwcutter b43-fwcutter/cut_firmware boolean true | debconf-set-selections
   echo firmware-ipw2x00 firmware-ipw2x00/license/accepted boolean true | debconf-set-selections
   echo firmware-iwlwifi firmware-iwlwifi/license/accepted boolean true | debconf-set-selections
   echo firmware-ralink firmware-ralink/license/accepted boolean true | debconf-set-selections

   mkdir --parents "/var/lib/whonix/do_once"
   touch "/var/lib/whonix/do_once/${FUNCNAME}_version_1"
}

## https://forums.whonix.org/t/setting-up-anon-base-files-shows-machine-id-prompt-followed-by-ominous-warning/8045
symlink_fix() {
   if dpkg --compare-versions "$my_old_package_version" ge "3:9.9-1" ; then
      true "INFO: 'dpkg --compare-versions $my_old_package_version ge 3:9.9-1', skipping."
      return 0
   fi

   if ! command -v qubesdb-read >/dev/null 2>&1; then
      return 0
   fi

   if [ -f "/var/lib/whonix/do_once/${FUNCNAME}_version_4" ]; then
      return 0
   fi

   ## output if ok:
   ## ls -la /etc/hosts
   ## lrwxrwxrwx 1 root root 14 Sep  9 09:34 /etc/hosts -> hosts.whonix

   ## config package dev does:
   ## ln -s hosts.whonix /etc/hosts

   pushd /etc >/dev/null || true

   if [ -f "/etc/resolv.conf" ]; then
      if test -f "/etc/resolv.conf.whonix" ; then
         chattr -i "/etc/resolv.conf" &>/dev/null || true
         unlink "/etc/resolv.conf" &>/dev/null || true
         rm -f "/etc/resolv.conf" &>/dev/null || true
         ln -s "$(basename "/etc/resolv.conf.whonix")" "/etc/resolv.conf" || true
      fi
   fi

   if [ -f "/etc/hostname" ]; then
      if test -f "/etc/hostname.whonix" ; then
         chattr -i "/etc/hostname" &>/dev/null || true
         unlink "/etc/hostname" &>/dev/null || true
         rm -f "/etc/hostname" &>/dev/null || true
         ln -s "$(basename "/etc/hostname.whonix")" "/etc/hostname" || true
      fi
   fi

   if [ -f "/etc/hosts" ]; then
      if test -f "/etc/hosts.whonix" ; then
         chattr -i "/etc/hosts" &>/dev/null || true
         unlink "/etc/hosts" &>/dev/null || true
         rm -f "/etc/hosts" &>/dev/null || true
         ln -s "$(basename "/etc/hosts.whonix")" "/etc/hosts" || true
      fi
   fi

   for broken_file in /etc/resolv.conf /etc/hostname /etc/hosts ; do
      ## Debugging / Info.
      ls -la -- "$broken_file" || true
      #cat -- "$broken_file" || true
   done

   popd >/dev/null || true

   mkdir --parents "/var/lib/whonix/do_once"
   touch "/var/lib/whonix/do_once/${FUNCNAME}_version_4"
}

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

   ## https://forums.whonix.org/t/keyboard-configuration-debconf-popup-during-apt-get-dist-upgrade/8318
   echo "keyboard-configuration	keyboard-configuration/variantcode	string	" | debconf-set-selections
   echo "keyboard-configuration	keyboard-configuration/altgr	select	The default for the keyboard layout" | debconf-set-selections
   echo "keyboard-configuration	keyboard-configuration/compose	select	No compose key" | debconf-set-selections
   echo "keyboard-configuration	keyboard-configuration/ctrl_alt_bksp	boolean	false" | debconf-set-selections
   echo "keyboard-configuration	keyboard-configuration/unsupported_config_options	boolean	true" | debconf-set-selections
   echo "keyboard-configuration	keyboard-configuration/model	select	Generic 105-key PC (intl.)" | debconf-set-selections
   echo "keyboard-configuration	keyboard-configuration/switch	select	No temporary switch" | debconf-set-selections
   echo "keyboard-configuration	keyboard-configuration/unsupported_options	boolean	true" | debconf-set-selections
   echo "keyboard-configuration	keyboard-configuration/layoutcode	string	us" | debconf-set-selections
   echo "keyboard-configuration	keyboard-configuration/toggle	select	No toggling" | debconf-set-selections
   echo "keyboard-configuration	keyboard-configuration/variant	select	English (US)" | debconf-set-selections
   echo "keyboard-configuration	keyboard-configuration/store_defaults_in_debconf_db	boolean	false" | debconf-set-selections
   echo "keyboard-configuration	keyboard-configuration/optionscode	string	" | debconf-set-selections
   echo "keyboard-configuration	keyboard-configuration/xkb-keymap	select	us" | debconf-set-selections
   echo "keyboard-configuration	keyboard-configuration/unsupported_layout	boolean	true" | debconf-set-selections
   echo "keyboard-configuration	keyboard-configuration/layout	select	" | debconf-set-selections
   echo "keyboard-configuration	keyboard-configuration/unsupported_config_layout	boolean	true" | debconf-set-selections
   echo "keyboard-configuration	keyboard-configuration/modelcode	string	pc105" | debconf-set-selections

   mkdir --parents "/var/lib/whonix/do_once"
   touch "/var/lib/whonix/do_once/${FUNCNAME}_version_1"
}

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

   ## kexec-tools is a dependency of ram-wipe.
   ## Prevent question "Should kexec-tools handle reboots (sysvinit only)?"
   echo "kexec-tools	kexec-tools/load_kexec	boolean	false" | debconf-set-selections

   mkdir --parents "/var/lib/whonix/do_once"
   touch "/var/lib/whonix/do_once/${FUNCNAME}_version_1"
}

apt_mark_deprecated_packages_for_autoremoval() {
   if dpkg --compare-versions "$my_old_package_version" ge "3:9.9-1" ; then
      true "INFO: 'dpkg --compare-versions $my_old_package_version ge 3:9.9-1', skipping."
      return 0
   fi

   if [ -f "/var/lib/whonix/do_once/${FUNCNAME}_version_3" ]; then
      return 0
   fi
   apt-mark auto hardened-packages-dependencies-cli &>/dev/null || true

   ## https://forums.whonix.org/t/disable-grub-output-verbose-by-default/8656
   apt-mark auto grub-output-verbose &>/dev/null || true

   ## https://forums.whonix.org/t/whonix-default-packages-review-mmdebstrap-varriant-related-risk-of-regressions/9254
   apt-mark auto rsyslog &>/dev/null || true
   apt-mark auto isc-dhcp-client &>/dev/null || true
   apt-mark auto isc-dhcp-common &>/dev/null || true
   apt-mark auto logrotate &>/dev/null || true
   apt-mark auto tasksel &>/dev/null || true
   apt-mark auto vim-common &>/dev/null || true
   apt-mark auto vim-tiny &>/dev/null || true
   apt-mark auto dmidecode &>/dev/null || true

   ## https://forums.whonix.org/t/consolidating-whonix-packages/1945
   apt-mark manual anon-base-files &>/dev/null || true
   apt-mark manual anon-gpg-tweaks &>/dev/null || true
   apt-mark manual anon-gw-dhcp-conf &>/dev/null || true
   apt-mark manual anon-gw-dns-conf &>/dev/null || true
   apt-mark manual anon-gw-leaktest &>/dev/null || true
   apt-mark manual anon-ws-dns-conf &>/dev/null || true
   apt-mark manual anon-ws-leaktest &>/dev/null || true
   apt-mark manual apparmor-profile-anondist &>/dev/null || true
   apt-mark manual apparmor-profile-gwenview &>/dev/null || true
   apt-mark manual apparmor-profile-icedove &>/dev/null || true
   apt-mark manual apparmor-profile-okular &>/dev/null || true
   apt-mark manual apparmor-profile-virtualbox &>/dev/null || true
   apt-mark manual apparmor-profile-xchat &>/dev/null || true
   apt-mark manual grub-default-live &>/dev/null || true
   apt-mark manual grub-screen-resolution &>/dev/null || true
   apt-mark manual shared-folder-help &>/dev/null || true
   apt-mark manual swappiness-lowest &>/dev/null || true
   apt-mark manual timezone-utc &>/dev/null || true
   apt-mark manual whonix-ws-irc-chat-support &>/dev/null || true
   apt-mark manual whonix-ws-start-menu-additions &>/dev/null || true
   apt-mark manual xchat-improved-privacy &>/dev/null || true

   apt-mark manual exim &>/dev/null || true
   apt-mark manual exim4 &>/dev/null || true
   apt-mark manual exim4-config &>/dev/null || true
   apt-mark manual exim4-base &>/dev/null || true
   apt-mark manual exim4-daemon-light &>/dev/null || true

   mkdir --parents "/var/lib/whonix/do_once"
   touch "/var/lib/whonix/do_once/${FUNCNAME}_version_3"
}

apt_mark_deprecated_packages_for_autoremoval_two() {
   if [ -f "/var/lib/whonix/do_once/${FUNCNAME}_version_3" ]; then
      return 0
   fi

   apt-mark manual secbrowser &>/dev/null || true

   mkdir --parents "/var/lib/whonix/do_once"
   touch "/var/lib/whonix/do_once/${FUNCNAME}_version_3"
}

## TODO
# apt_mark_virtualbox_guest_addition_from_debian_package_autoremoval() {
#    if [ -f "/var/lib/whonix/do_once/${FUNCNAME}_version_1" ]; then
#       return 0
#    fi
#
#    apt-mark manual virtualbox-guest-dkms &>/dev/null || true
#    apt-mark manual virtualbox-guest-utils &>/dev/null || true
#    apt-mark manual virtualbox-guest-x11 &>/dev/null || true
#
#    mkdir --parents "/var/lib/whonix/do_once"
#    touch "/var/lib/whonix/do_once/${FUNCNAME}_version_1"
# }

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

   apt-mark manual pulseaudio-qubes &>/dev/null || true
   apt-mark manual pulseaudio-utils &>/dev/null || true
   apt-mark manual pulseaudio &>/dev/null || true

   mkdir --parents "/var/lib/whonix/do_once"
   touch "/var/lib/whonix/do_once/${FUNCNAME}_version_1"
}

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

   apt-mark manual qubes-core-agent-passwordless-root &>/dev/null || true

   mkdir --parents "/var/lib/whonix/do_once"
   touch "/var/lib/whonix/do_once/${FUNCNAME}_version_1"
}

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

   ## See:
   ## package dist-base-files
   ## file /etc/dracut.conf.d/60-dist-base-files.conf
   apt-mark auto dracut-config-generic &>/dev/null || true

   mkdir --parents "/var/lib/whonix/do_once"
   touch "/var/lib/whonix/do_once/${FUNCNAME}_version_1"
}

apt_mark_keep_one() {
   if [ -f "/var/lib/whonix/do_once/${FUNCNAME}_version_2" ]; then
      return 0
   fi

   ## 'apt-mark manual' - keep
   ## 'apt-mark auto'   - mark as automatically installed for 'apt autoremove'

   ## * In the past, a meta packages has 'Depends:' on these packages.
   ## * Nowadays, these are "weak recommended packages". No meta package
   ##   'Depends:' on these packages. These packages are conditionally
   ##   installed by build steps **_install-packages' and '*_create-lb-iso'.
   apt-mark manual kicksecure-network-conf &>/dev/null || true
   apt-mark manual kicksecure-network-conf-gui &>/dev/null || true
   apt-mark manual tirdad &>/dev/null || true
   apt-mark manual tirdad-dkms &>/dev/null || true
   apt-mark manual firmware-nonfreedom-network &>/dev/null || true

   mkdir --parents "/var/lib/whonix/do_once"
   touch "/var/lib/whonix/do_once/${FUNCNAME}_version_2"
}

migrations_for_old_versions() {
   apt_mark_deprecated_packages_for_autoremoval
   apt_mark_deprecated_packages_for_autoremoval_two
   symlink_fix
   #apt_mark_virtualbox_guest_addition_from_debian_package_autoremoval
   apt_mark_deprecated_packages_for_autoremoval_three
   apt_mark_deprecated_packages_for_autoremoval_four
   apt_mark_deprecated_packages_for_autoremoval_five
   apt_mark_keep_one
}

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

true "INFO: Configuring $DPKG_MAINTSCRIPT_PACKAGE..."

get_build_version

my_old_package_version="$2"
## example my_old_package_version:
## 3:9.9-1

## Will probably "forever" be required.
nonfree_firmware_preseed
keyboard_configuration_debconf_preseed
kexec_tools_preseed

if [ "$1" = "install" ]; then
   true "INFO: $DPKG_MAINTSCRIPT_PACKAGE not installed yet."
else
   true "INFO: $DPKG_MAINTSCRIPT_PACKAGE already installed."
   migrations_for_old_versions
fi

true "INFO: End configuring $DPKG_MAINTSCRIPT_PACKAGE."

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
