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

source ./help-steps/pre
## Not sourcing ./help-steps/variables. Doing so breaks the build.

error_handler_dist_build_one() {
   true "${red}${bold}ERROR in $0${reset}"
   true "${red}${bold}BASH_COMMAND${reset}: $BASH_COMMAND"
   true "${red}${bold}dist_build_one_build_step_current${reset}: $dist_build_one_build_step_current"
   true "${red}\$*: $*${reset}"
   true "${red}${bold}INFO: Now exiting from $0 (because error was detected, see above).${reset}"
   exit 1
}

trap "error_handler_dist_build_one" ERR

dist_build_machine() {
   run-parts --verbose --test ./build-steps.d

   ## Not using:
   #run-parts --verbose --exit-on-error ./build-steps.d
   ## Because of an issue,
   ## "run-parts, trap INT, read error":
   ## https://lists.gnu.org/archive/html/help-bash/2015-03/msg00066.html

   for dist_build_one_build_step_current in ./build-steps.d/*; do
      if ! test -x "$dist_build_one_build_step_current"; then
         true "${cyan}${bold}INFO: skip non-executable file (missing chmod +x) dist_build_one_build_step_current: $dist_build_one_build_step_current${reset}"
         continue
      fi
      ## If the last character is a ~, ignore that file,
      ## because it was created by some editor,
      ## which creates backup files.
      if [ "${dist_build_one_build_step_current: -1}" = "~" ]; then
         true "${cyan}${bold}INFO: skip tilde at the end dist_build_one_build_step_current: $dist_build_one_build_step_current${reset}"
         continue
      fi
      ## Skipping files such as .dpkg-old and .dpkg-dist.
      if printf "%s\n" "$dist_build_one_build_step_current" | grep --quiet ".dpkg-"; then
         true "${cyan}${bold}INFO: skip .dpkg- inside the file name dist_build_one_build_step_current: $dist_build_one_build_step_current${reset}"
         continue
      fi
      true "${cyan}${bold}${under}############################################################${reset}"
      true "${cyan}${bold}${under}############################################################${reset}"
      true "${cyan}${bold}${under}############################################################${reset}"
      true "${cyan}${bold}${under}INFO: BEGIN: dist_build_one_build_step_current: $dist_build_one_build_step_current${reset}"
      "./$dist_build_one_build_step_current" "$@"
      true "${cyan}${bold}${under}INFO: END  : dist_build_one_build_step_current: $dist_build_one_build_step_current${reset}"
      true "${cyan}${bold}${under}############################################################${reset}"
      true "${cyan}${bold}${under}############################################################${reset}"
      true "${cyan}${bold}${under}############################################################${reset}"
   done
}

main() {
   if [ "${1:-}" = "--help" ]; then
      ./help-steps/parse-cmd --help
      exit 0
   fi

   root_check

   ## XXX
   trap "error_handler_dist_build_one" ERR
   trap - INT
   trap - TERM

   dist_build_machine "$@"

   true
}

main "$@"
