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

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

   shopt -s globstar dotglob nullglob

   for file_name in /etc/whonix.d/* /usr/local/etc/whonix.d/* ; do
      search="WHONIXCHECK_NO_EXIT_ON_IP_FORWARDING_DETECTION"
      replace="NO_EXIT_ON_IP_FORWARDING_DETECTION"
      str_replace "$search" "$replace" "$file_name"
      search="check_whonix_apt_repository"
      replace="check_apt_repository"
      str_replace "$search" "$replace" "$file_name"
      search="whonixcheck"
      replace="systemcheck"
      str_replace "$search" "$replace" "$file_name"
      search="WHONIXCHECK"
      search="SYSTEMCHECK"
      str_replace "$search" "$replace" "$file_name"
      search="/etc/whonix.d"
      search="/etc/systemcheck.d"
      str_replace "$search" "$replace" "$file_name"
   done

   shopt -u globstar dotglob nullglob

   if test -d /etc/whonix.d ; then
      if ! test -d /etc/systemcheck.d ; then
         mv --verbose --no-clobber /etc/whonix.d /etc/systemcheck.d
      fi
   fi

   if test -d /usr/local/etc/whonix.d ; then
      if ! test -d /usr/local/etc/systemcheck.d ; then
         mv --verbose --no-clobber /usr/local/etc/whonix.d /usr/local/etc/systemcheck.d
      fi
   fi

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

true "INFO: End configuring $DPKG_MAINTSCRIPT_PACKAGE."

true "INFO: debhelper beginning here."

#DEBHELPER#

true "INFO: Done with debhelper."

## Do after debhelper so this happens after rm_conffile maintscript.
legacy_settings_folder_migrate

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

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