#!/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

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

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

source "$MYDIR/pre"
source "$MYDIR/variables"

error_handler_unchroot-raw() {
   : echo "
${red}${bold}BASH_COMMAND${reset}: $BASH_COMMAND
${red}${bold}ERROR ${BASH_SOURCE[0]}: | caller: $(caller)${reset}
"
   exit 1
}

main() {
   trap "error_handler_unchroot-raw" ERR INT TERM

   sync

   if [ "${dist_build_install_to_root:-}" = "true" ]; then
      true
   else
      "$dist_source_help_steps_folder/unmount-helper" "$CHROOT_FOLDER/dev/pts"
      "$dist_source_help_steps_folder/unmount-helper" "$CHROOT_FOLDER/dev"
      "$dist_source_help_steps_folder/unmount-helper" "$CHROOT_FOLDER/proc"
      "$dist_source_help_steps_folder/unmount-helper" "$CHROOT_FOLDER/sys"

      ## TODO: image should not contain folders /run /sys /proc /dev?
      ## https://github.com/grml/grml-debootstrap/issues/276

      #if test -e "$CHROOT_FOLDER/dev/pts" ; then
         #$SUDO_TO_ROOT rmdir -- "$CHROOT_FOLDER/dev/pts"
      #fi

      #if test -d "$CHROOT_FOLDER/dev" ; then
         #$SUDO_TO_ROOT rm -r "$CHROOT_FOLDER/dev"
      #fi

      ## TODO: re-enable if/when dracut bug gets fixed
      ## LiveOS/squashfs.img missing an empty /proc results in broken boot
      ## https://github.com/dracut-ng/dracut-ng/issues/244
      ## https://github.com/dracut-ng/dracut-ng/pull/703
      #if test -d "$CHROOT_FOLDER/proc" ; then
         #$SUDO_TO_ROOT rmdir -- "$CHROOT_FOLDER/proc"
      #fi

      #if test -d "$CHROOT_FOLDER/sys" ; then
         #$SUDO_TO_ROOT rmdir -- "$CHROOT_FOLDER/sys"
      #fi

      #if test -d "$CHROOT_FOLDER/run" ; then
         #$SUDO_TO_ROOT rm -r -- "$CHROOT_FOLDER/run"
      #fi
   fi

   local derivative_name_item
   for derivative_name_item in $derivative_name_list ; do
      "$dist_source_help_steps_folder/unmount-helper" "$CHROOT_FOLDER/$DEB_INSTALL_FOLDER/$derivative_name_item"
      if test -d "$CHROOT_FOLDER/$DEB_INSTALL_FOLDER/$derivative_name_item" ; then
         $SUDO_TO_ROOT rmdir --ignore-fail-on-non-empty -- "$CHROOT_FOLDER/$DEB_INSTALL_FOLDER/$derivative_name_item"
      fi
   done
   if test -d "$CHROOT_FOLDER/$DEB_INSTALL_FOLDER" ; then
      $SUDO_TO_ROOT rmdir --ignore-fail-on-non-empty -- "$CHROOT_FOLDER/$DEB_INSTALL_FOLDER"
   fi

   ## Also possibly unmount '.backup' file for bare metal builds.
   "$dist_source_help_steps_folder/unmount-helper" "$CHROOT_FOLDER"/etc/resolv.conf
   "$dist_source_help_steps_folder/unmount-helper" "$CHROOT_FOLDER"/etc/hosts
   "$dist_source_help_steps_folder/unmount-helper" "$CHROOT_FOLDER"/etc/hostname
   "$dist_source_help_steps_folder/unmount-helper" "$CHROOT_FOLDER"/etc/network/interfaces

   if [ "${dist_build_install_to_root:-}" = "true" ]; then
      ## $CHROOT_FOLDER would be /, not wise to kill all
      true
   else
      ## kill dbus-launch when building Non-Qubes-Whonix inside a Qubes Debian template
      $SUDO_TO_ROOT "$MYDIR/umount_kill.sh" "$CHROOT_FOLDER"
   fi

   sync
}

main "$@"
