#!/bin/bash

## Copyright (C) 2012 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

# shellcheck source=../../helper-scripts/usr/libexec/helper-scripts/pre.bsh
source /usr/libexec/helper-scripts/pre.bsh

# shellcheck source=../../helper-scripts/usr/libexec/helper-scripts/log_run_die.sh
source /usr/libexec/helper-scripts/log_run_die.sh
log_level='debug'

set -e

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

create_tb_updater_user_account() {
   adduser --home /var/lib/tb-updater/work --quiet --system --group tb-updater || true
}

tb_resolv_conf_debugging() {
   log notice "tb_resolv_conf_debugging: start"

   log_run notice ls -la -- /etc/resolv.conf || true
   log_run notice cat -- /etc/resolv.conf || true
   log_run notice cat -- /etc/resolv.whonix || true
   log_run notice cat -- /etc/resolv.conf.whonix-orig || true

   log notice "tb_resolv_conf_debugging: end"
}

tb_anon_ws_dns_conf() {
    tb_resolv_conf_debugging

    log notice "Checking if /etc/resolv.conf is a symlink..."
    if test -h /etc/resolv.conf ; then
       ## This is only the case when whonix-ws-network-conf is already installed.
       log notice "Yes, /etc/resolv.conf is a symlink..."
       log notice "Checking if /var/lib/dpkg/info/whonix-ws-network-conf.prerm is an executable..."
       if test -x /var/lib/dpkg/info/whonix-ws-network-conf.prerm ; then
          log notice "Yes, /var/lib/dpkg/info/whonix-ws-network-conf.prerm is an executable."
          [ -n "$tb_reenable_anon_ws_dns_conf" ] || tb_reenable_anon_ws_dns_conf=true
          tb_resolv_conf_debugging

          log notice "tb_anon_ws_dns_conf: Restore original /etc/resolv.conf by running /usr/libexec/tb-updater/chroot-pre ..."
          /usr/libexec/tb-updater/chroot-pre

          tb_resolv_conf_debugging
       fi
    fi
}

tb_chroot_pre_exit() {
   if [ "$tb_reenable_anon_ws_dns_conf" = "true" ]; then
      log notice "tb_chroot_pre_exit: tb_reenable_anon_ws_dns_conf=true"

      log notice "tb_chroot_pre_exit: Restore whonix-ws-network-conf /etc/resolv.conf by running /usr/libexec/tb-updater/chroot-post ..."

      /usr/libexec/tb-updater/chroot-post

      tb_resolv_conf_debugging
   fi
}

download() {
   local in_chroot chroot_maybe tool_list tool_item
   in_chroot='false'
   if ischroot --default-false ; then
      in_chroot='true'
      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"

      if [ "${in_chroot}" = 'true' ]; then
         ## This variable may be set to 'false' by derivative-maker during an
         ## image build.
         [ -n "${tb_disable_anon_ws_dns_conf-}" ] || tb_disable_anon_ws_dns_conf='true'

         if [ "$tb_disable_anon_ws_dns_conf" = 'true' ]; then
            tb_anon_ws_dns_conf
         fi
      fi

      if ! log_run notice $tool_item $chroot_maybe --postinst; then
         if [ "${in_chroot}" = 'true' ] \
            && [ "${anon_shared_inst_tb}" = 'closed' ]; then
            log error "update-torbrowser failed! Running in chroot and anon_shared_inst_tb is 'closed', therefore failing closed."
            exit 1
         else
            log warn 'update-torbrowser-failed. Consider running update-torbrowser manually.'
         fi
      fi

      if [ "${in_chroot}" = 'true' ]; then
         tb_chroot_pre_exit
      fi
   done
}

true "anon_shared_inst_tb: $anon_shared_inst_tb"
true "tb_onion: $tb_onion"
true "tb_disable_anon_ws_dns_conf: $tb_disable_anon_ws_dns_conf"

case "$1" in
   configure)
      create_tb_updater_user_account
      download
   ;;

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

   *)
      log error "${DPKG_MAINTSCRIPT_NAME} called with unknown argument \`$1'" >&2
      exit 1
   ;;
esac

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

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
