#!/bin/bash

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

set -x
set -o errexit
set -o nounset
set -o pipefail
set -o errtrace
shopt -s inherit_errexit
shopt -s shift_verbose

true "INFO: Currently running script: ${BASH_SOURCE[0]} $*"

MYDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

source "${MYDIR}/pre"
source "${MYDIR}/variables"

main() {
   sync || true

   if [ -e "${CHROOT_FOLDER}/usr/sbin/policy-rc.d.derivative-maker" ]; then
      ## Uninstall alternative
      ${SUDO_TO_ROOT} chroot "${CHROOT_FOLDER}" update-alternatives --remove policy-rc.d /usr/sbin/policy-rc.d.derivative-maker

      ## Remove file
      ${SUDO_TO_ROOT} safe-rm --verbose --force "${CHROOT_FOLDER}/usr/sbin/policy-rc.d.derivative-maker"

      ## update-alternatives adds timestamped logs when we run it above,
      ## and there may not be a future cleanup step to fix this, so truncate
      ## those logs now.
      if [ -f "${CHROOT_FOLDER}/var/log/alternatives.log" ]; then
         ${SUDO_TO_ROOT} truncate --size=0 -- "${CHROOT_FOLDER}/var/log/alternatives.log"
      fi
   fi

   sync || true
}

main "$@"
