#!/bin/bash
# SPDX-Version: 3.0
# SPDX-CreationInfo: 2025-06-17; WEIDNER, Marc S.; <msw@coresecret.dev>
# SPDX-ExternalRef: GIT https://git.coresecret.dev/msw/CISS.debian.installer.git
# SPDX-FileContributor: WEIDNER, Marc S.; Centurion Intelligence Consulting Agency
# SPDX-FileCopyrightText: 2024-2025; WEIDNER, Marc S.; <msw@coresecret.dev>
# SPDX-FileType: SOURCE
# SPDX-License-Identifier: EUPL-1.2 OR LicenseRef-CCLA-1.0
# SPDX-LicenseComment: This file is part of the CISS.debian.installer.secure framework.
# SPDX-PackageName: CISS.debian.installer
# SPDX-Security-Contact: security@coresecret.eu

guard_sourcing

#######################################
# Use do_in_target() for:
#   - simple commands (e.g., dpkg, ln, mkdir, apt, etc.).
# Use do_in_target_script() for:
#   - all shell scripts, redirects, pipes, conditions, loops, or subshells.
#######################################

#######################################
# Wrapper for executing commands in the desired chroot environment.
# Globals:
#   BASH_SOURCE
#   TERM
# Arguments:
#   1: Target of the chroot environment.
#   2: Commands and options and parameters to be executed in chroot.
# Returns:
#   0: on success
#   ERR_CHRT_COMMAND: on failure
#######################################
do_in_target() {
  declare    var_chroot_target="$1"; shift
  declare -a ary_chroot_command=("$@")
  declare -r var_default_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  declare    var_mod="${BASH_SOURCE[1]##*/}"; var_mod="${var_mod%%_*}()"

  if (( ${#ary_chroot_command[@]} == 0 )); then

    do_log "emergency" "file_only" "1080() Empty command passed to 'do_in_target()'."
    return "${ERR_CHRT_COMMAND}"

  fi

  if ! chroot "${var_chroot_target}" /usr/bin/env -i PATH="${var_default_path}" which "${ary_chroot_command[0]}" &>/dev/null; then

    do_log "emergency" "file_only" "1080() Binary: '${ary_chroot_command[0]}' not found in target 'PATH=${var_default_path}'."
    do_log "emergency" "file_only" "1080() Command: [chroot ${var_chroot_target} /usr/bin/env -i PATH=${var_default_path} which ${ary_chroot_command[0]} &>/dev/null]."
    return "${ERR_CHRT_COMMAND}"

  fi

  if ! chroot "${var_chroot_target}" /usr/bin/env -i \
    HOME="/root" \
    PATH="${var_default_path}" \
    TERM="${TERM}" \
    LANG="C.UTF-8" \
    LC_ALL="C.UTF-8" \
    DEBIAN_FRONTEND="noninteractive" \
    APT_LISTCHANGES_FRONTEND="none" \
    "${ary_chroot_command[@]}"

  then

    do_log "emergency" "file_only" "1080() Command of ${var_mod} [chroot ${var_chroot_target} /usr/bin/env -i HOME=/root PATH=${var_default_path} TERM=${TERM} LANG=C.UTF-8 LC_ALL=C.UTF-8 DEBIAN_FRONTEND=noninteractive APT_LISTCHANGES_FRONTEND=none ${ary_chroot_command[*]}] failed."
    return "${ERR_CHRT_COMMAND}"

  else

    do_log "info" "file_only" "1080() Command of ${var_mod} [chroot ${var_chroot_target} /usr/bin/env -i HOME=/root PATH=${var_default_path} TERM=${TERM} LANG=C.UTF-8 LC_ALL=C.UTF-8 DEBIAN_FRONTEND=noninteractive APT_LISTCHANGES_FRONTEND=none ${ary_chroot_command[*]}] successful."
    return 0

  fi
}

#######################################
# Execute a full shell script line inside the chroot via bash -c.
# TODO: Supports interactive debug shell on error.
# Globals:
#   BASH_SOURCE
#   TERM
# Arguments:
#   1: Target of the chroot environment
#   2: Command string to execute inside a shell (quoted)
#   3: Log level of command pipeline to be executed.
# Returns:
#   0: on success
#   ERR_CHRT_COMMAND: on failure
#######################################
do_in_target_script() {
  declare    var_chroot_target="$1"
  declare    var_chroot_script="$2"
  declare    var_log_level_on_error="${3:-emergency}"
  declare -r var_default_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  declare    var_mod="${BASH_SOURCE[1]##*/}"; var_mod="${var_mod%%_*}()"

  if [[ -z "${var_chroot_script}" ]]; then

    do_log "emergency" "file_only" "1080() Empty command passed to 'do_in_target_script()'."
    return "${ERR_CHRT_COMMAND}"

  fi

  if ! chroot "${var_chroot_target}" /usr/bin/env -i \
    HOME="/root" \
    PATH="${var_default_path}" \
    TERM="${TERM}" \
    LANG="C.UTF-8" \
    LC_ALL="C.UTF-8" \
    DEBIAN_FRONTEND="noninteractive" \
    APT_LISTCHANGES_FRONTEND="none" \
    /bin/bash -o errexit -o errtrace -o functrace -o nounset -o pipefail \
    -O inherit_errexit -O failglob -O lastpipe -c "${var_chroot_script}"

  then

    do_log "${var_log_level_on_error}" "file_only" "1080() Command of ${var_mod} [chroot ${var_chroot_target} /usr/bin/env -i HOME=/root PATH=${var_default_path} TERM=${TERM} LANG=C.UTF-8 LC_ALL=C.UTF-8 DEBIAN_FRONTEND=noninteractive APT_LISTCHANGES_FRONTEND=none /bin/bash -c ${var_chroot_script}] failed."
    return "${ERR_CHRT_COMMAND}"

    # TODO: Test with Dialog Wrapper in interactive mode.
    # TODO: Call clean screen first to terminate dialog wrapper !
    #if [[ "${DEBUG_INTERACTIVE}" == "true" ]]; then
    #  do_log "warning" "true" "Launching interactive debug shell in chroot: '${var_chroot_target}'."
    #  chroot "${var_chroot_target}" /bin/bash -l
    #fi

  else

    do_log "info" "file_only" "1080() Command of ${var_mod} [chroot ${var_chroot_target} /usr/bin/env -i HOME=/root PATH=${var_default_path} TERM=${TERM} LANG=C.UTF-8 LC_ALL=C.UTF-8 DEBIAN_FRONTEND=noninteractive APT_LISTCHANGES_FRONTEND=none /bin/bash -c ${var_chroot_script}] successful."
    return 0

  fi
}
# vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=sh
