#!/bin/bash

## Copyright (C) 2012 - 2023 ENCRYPTED SUPPORT LP <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

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. ${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_install_to_root" = "true" ]; then
      true "${green}INFO: Skipping $BASH_SOURCE, 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, because dist_build_type_long is set to $dist_build_type_long.${reset}"
   else
      zerofree_raw "$@"
   fi
}

main "$@"
