#!/bin/bash

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

set -x
set -e

true "INFO: Currently running script: $BASH_SOURCE $@"

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

cd "$MYDIR"
cd ..
cd help-steps

source pre
source colors
source variables

hostname_check() {
   test -n "${dist_build_hostname}"
   ## This cannot be done with a chroot in folder /usr/libexec/initializer-dist/chroot-scripts-post.d,
   ## because the image is still chrooted (chroot-raw) at that time. As a result, a host operating
   ## system temporary file (a copy of the host's /etc/hostname) is bind mounted to the chroot's
   ## /etc/hostname file
   if [ "$dist_build_type_short" = "kicksecure" ]; then
      one_expected="$dist_build_hostname"
      two_expected=""
      three_expected=""
      one_actual=$(chroot_run cat "/etc/hostname")
      two_actual=""
      three_actual=""
   elif [ "$dist_build_type_short" = "whonix" ]; then
      one_expected="host"
      two_expected="host"
      three_expected="$dist_build_hostname"
      one_actual=$(chroot_run cat "/etc/hostname")
      ## ./packages/whonix/whonix-base-files/etc/hostname.whonix
      two_actual=$(chroot_run cat "/etc/hostname.${dist_build_type_short}")
      three_actual=$(chroot_run cat "/etc/hostname.${dist_build_type_short}-orig")
   fi

   if [ "$one_actual" != "$one_expected" ] || [ "$two_actual" != "$two_expected" ] || [ "$three_actual" != "$three_expected" ]; then
      error "hostname_check failed! See above for variable one_expected etc."
   fi

   true
}

generate_grub_keyboard_layouts() {
  ## This cannot be done with a chroot in folder /usr/libexec/initializer-dist/chroot-scripts-post.d,
  ## because the list of keyboard layouts comes from locatectl. localectl is not usable in a chroot,
  ## so we have to run localectl on the host and pipe the list of keyboard layouts to the chroot.
  ## Use '--no-update-grub' because GRUB is handled by 'build-steps.d/*_install-packages'.
  ## NOTE: 'build-steps.d/*_create-lb-iso' also uses 'set-grub-keymap'. Should use the same/similar options.
  localectl list-x11-keymap-layouts | chroot_run set-grub-keymap --build-all --read-stdin --no-update-grub
}

run-chroot-scripts-post-d() {
   exception_handler_setup "exception_handler_unchroot_unmount" ERR INT TERM

   $SUDO_TO_ROOT sync

   "$dist_source_help_steps_folder"/mount-raw "$@"

   "$dist_source_help_steps_folder"/prevent-daemons-from-starting "$@"

   "$dist_source_help_steps_folder"/chroot-raw "$@"

   $SUDO_TO_ROOT sync

   ## Sanity tests.
   chroot_run dir
   chroot_run mount
   chroot_run sync

   "$dist_source_help_steps_folder"/create-local-temp-apt-repo "$@"

   $SUDO_TO_ROOT sync

   if [ -d "$CHROOT_FOLDER/usr/libexec/initializer-dist/chroot-scripts-post.d" ]; then
      ## Check which chroot scripts we got.
      chroot_run run-parts --verbose --test "/usr/libexec/initializer-dist/chroot-scripts-post.d/"

      ## Debugging.
      #chroot_run str_replace "/usr/sbin/autologinchange" "bash -x /usr/sbin/autologinchange" "/usr/libexec/initializer-dist/chroot-scripts-post.d/75_autologin"

      ## Run the chroot scripts.
      chroot_run run-parts --verbose --exit-on-error "/usr/libexec/initializer-dist/chroot-scripts-post.d/"

      sync
   else
      true "${green}${bold}INFO: Folder /usr/libexec/initializer-dist/chroot-scripts-post.d does not exist in chroot.}
Not running any chroot scripts.${reset}"
   fi

   "$dist_source_help_steps_folder"/remove-local-temp-apt-repo "$@"

   generate_grub_keyboard_layouts

   "$dist_source_help_steps_folder"/unchroot-raw "$@"

   hostname_check

   "$dist_source_help_steps_folder"/unprevent-daemons-from-starting "$@"

   "$dist_source_help_steps_folder"/unmount-raw "$@"

   sync
}

main() {
   if [ "$build_dry_run" = "true" ]; then
      true "${bold}${cyan}INFO: dry-run, skipping $BASH_SOURCE. ${reset}"
      return 0
   fi

   if [ "$dist_build_fast2" = "2" ]; then
      true "${bold}${cyan}INFO: run with '--fast 2' switch, skipping $BASH_SOURCE. ${reset}"
      return 0
   fi

   if [ "$dist_build_iso" = "true" ]; then
       true "${green}INFO: Skipping $BASH_SOURCE, because dist_build_iso is set to true.${reset}"
       return 0
   fi

   if [ "$dist_build_type_long" = "gateway" ]; then
      run-chroot-scripts-post-d "$@"
   elif [ "$dist_build_type_long" = "workstation" ]; then
      run-chroot-scripts-post-d "$@"
   elif [ "$dist_build_type_long" = "custom-workstation" ]; then
      true "${cyan}INFO: Skipping running chroot-post.d scripts for $VMNAME.${reset}"
   elif [ "$dist_build_type_long" = "whonix-host" ]; then
      run-chroot-scripts-post-d "$@"
   elif [ "$dist_build_type_short" = "kicksecure" ]; then
      run-chroot-scripts-post-d "$@"
   else
      error "ERROR: Invalid dist_build_flavor '$dist_build_flavor'. Please report this bug!"
   fi
}

main "$@"
