#!/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
export LC_ALL=C

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() {
   true "
${red}${bold}BASH_COMMAND${reset}: ${BASH_COMMAND}
${red}${bold}ERROR ${BASH_SOURCE[0]}: | caller: $(caller)${reset}
"
   exit 1
}

## Only unmount things we know are safe to unmount when dealing with an
## install-to-root build.
unmount_install_to_root_binds() {
   local derivative_name_item

   for derivative_name_item in ${derivative_name_list} ; do
      ${SUDO_TO_ROOT} "${dist_source_help_steps_folder}/mount-cleanup" -- "${CHROOT_FOLDER}/${DEB_INSTALL_FOLDER}/${derivative_name_item}"
      unmount_if_mounted "${CHROOT_FOLDER}/${DEB_INSTALL_FOLDER}/${derivative_name_item}"
   done

   ## Also possibly unmount '.backup' file for bare metal builds.
   unmount_if_mounted "${CHROOT_FOLDER}/etc/resolv.conf"
   unmount_if_mounted "${CHROOT_FOLDER}/etc/hosts"
   unmount_if_mounted "${CHROOT_FOLDER}/etc/hostname"
}

main() {
   trap error_handler_unchroot_raw ERR INT TERM

   sync

   if [ "${dist_build_install_to_root:-}" = "true" ]; then
      unmount_install_to_root_binds
   else
      ## Clean up orphaned processes and submounts.
      ${SUDO_TO_ROOT} "${MYDIR}/mount-cleanup" -- "${CHROOT_FOLDER}"

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

   ## Now that nothing is mounted there, drop the mount point directories.
   local derivative_name_item
   for derivative_name_item in ${derivative_name_list} ; do
      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

   sync
}

main "$@"
