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

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

source pre
source variables

zerofree_raw() {
   ## Used by "$dist_source_help_steps_folder"/mount-raw and "$dist_source_help_steps_folder"/unmount-raw.
   export kpartx_only=true

   source "$dist_source_help_steps_folder"/mount-raw
   ## provides: dev_mapper_device
   mount_raw

   $SUDO_TO_ROOT zerofree -v "$dev_mapper_device"

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

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

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

   if [ "${dist_build_install_to_root:-}" = "true" ]; then
      true "${green}INFO: Skipping ${BASH_SOURCE[0]}, because dist_build_install_to_root is set to true.${reset}"
   elif [ "${dist_build_type_long:-}" = "custom-workstation" ]; then
      true "${green}INFO: Skipping ${BASH_SOURCE[0]}, because dist_build_type_long is set to $dist_build_type_long.${reset}"
   else
      zerofree_raw "$@"
   fi
}

main "$@"
