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

download() {
   if ischroot --default-false ; then
      chroot_maybe="--is-chroot"
   fi

   tool_list="update-torbrowser update-mullvadbrowser"

   for tool_item in $tool_list ; do
      if [ "$tool_item" = "update-mullvadbrowser" ]; then
         ## TODO: Add update-mullvadbrowser support.
         ## Skipping for now.
         continue
      fi

      if [ "$tool_item" = "update-torbrowser" ]; then
         mkdir --parents "/var/cache/tb-binary"
      elif [ "$tool_item" = "update-mullvadbrowser" ]; then
         mkdir --parents "/var/cache/mullvadbrowser-binary"
      fi

      ## TODO:
      #tpo_downloader_debug="$tpo_downloader_debug"

      ## TODO: Use a dedicated Linux user account.
      ## Ideally this would be run as account "user", but the existence of that account
      ## is not guaranteed by the time this script is running.
      ## Using a dedicated user account is difficult versus Wayland because there
      ## is also tb-updater GUI and no way to run a GUI application under a
      ## different account. could be found for Wayland. tb-updater CLI versus
      ## tb-updater GUI would need to be cleanly separated.

      $tool_item \
         $chroot_maybe \
         --postinst
   done
}

true "INFO: debhelper beginning here."

# Automatically added by dh_installsystemd/13.24.2
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
	# The following line should be removed in trixie or trixie+1
	deb-systemd-helper unmask 'tb-updater-dispvm.service' >/dev/null || true

	# was-enabled defaults to true, so new installations run enable.
	if deb-systemd-helper --quiet was-enabled 'tb-updater-dispvm.service'; then
		# Enables the unit on first installation, creates new
		# symlinks on upgrades if the unit file has changed.
		deb-systemd-helper enable 'tb-updater-dispvm.service' >/dev/null || true
	else
		# Update the statefile to add new symlinks (if any), which need to be
		# cleaned up on purge. Also remove old symlinks.
		deb-systemd-helper update-state 'tb-updater-dispvm.service' >/dev/null || true
	fi
fi
# End automatically added section
# Automatically added by dh_installsystemd/13.24.2
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
	# The following line should be removed in trixie or trixie+1
	deb-systemd-helper unmask 'tb-updater-first-boot.service' >/dev/null || true

	# was-enabled defaults to true, so new installations run enable.
	if deb-systemd-helper --quiet was-enabled 'tb-updater-first-boot.service'; then
		# Enables the unit on first installation, creates new
		# symlinks on upgrades if the unit file has changed.
		deb-systemd-helper enable 'tb-updater-first-boot.service' >/dev/null || true
	else
		# Update the statefile to add new symlinks (if any), which need to be
		# cleaned up on purge. Also remove old symlinks.
		deb-systemd-helper update-state 'tb-updater-first-boot.service' >/dev/null || true
	fi
fi
# End automatically added section
# Automatically added by dh_installsystemd/13.24.2
if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then
	if [ -d /run/systemd/system ]; then
		systemctl --system daemon-reload >/dev/null || true
		if [ -n "$2" ]; then
			_dh_action=restart
		else
			_dh_action=start
		fi
		deb-systemd-invoke $_dh_action 'tb-updater-dispvm.service' 'tb-updater-first-boot.service' >/dev/null || true
	fi
fi
# End automatically added section


true "INFO: Done with debhelper."

true "anon_shared_inst_tb: $anon_shared_inst_tb"
true "tb_onion: $tb_onion"
true "tb_disable_anon_ws_dnf_conf: $tb_disable_anon_ws_dnf_conf"

download

## Makes assumption on account 'user' existing.
## Not needed since done by 'update-torbrowser' and 'torbrowser'.
#/usr/libexec/tb-updater/tb-permission-fix

true "
#####################################################################
## INFO: END  : $DPKG_MAINTSCRIPT_PACKAGE $DPKG_MAINTSCRIPT_NAME $@
#####################################################################
"

## Explicitly "exit 0", so eventually trapped errors can be ignored.
exit 0
