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

#######################################
# Configure the '/etc/resolv.conf' file.
# Globals:
#   ARY_IPV4_NS
#   ARY_IPV6_NS
#   DIR_BAK
#   TARGET
#   VAR_ARCHITECTURE
#   VAR_CODENAME
#   VAR_FINAL_IPV6
#   VAR_LINK_IPV6
#   VAR_VERSION
# Arguments:
#   None
# Returns:
#   0: on success
#######################################
setup_resolv() {
  ### Declare Arrays, HashMaps, and Variables.
  declare     ns=""

  if [[ -f "${TARGET}/etc/resolv.conf" ]]; then

    mkdir -p "${TARGET}/root/.ciss/cdi/backup/etc"
    mv "${TARGET}/etc/resolv.conf" "${TARGET}/root/.ciss/cdi/backup/etc/resolv.conf.bak"
    do_log "info" "file_only" "4035() Existing '${TARGET}/etc/resolv.conf' moved."

  fi

  touch "${TARGET}/etc/resolv.conf"
  chmod 0644 "${TARGET}/etc/resolv.conf"

  ### Create '/etc/resolv.conf' IPv4 entries for static configuration.
    cat << EOF >> "${TARGET}/etc/resolv.conf"
# 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

# /etc/resolv.conf      : Generated by CISS.debian.installer ${VAR_VERSION}
# Architecture          : ${VAR_ARCHITECTURE}
# Distribution          : ${VAR_CODENAME}

# Static file system information '/etc/resolv.conf '.

### Custom DNS IPv4 configuration
EOF

  for ns in "${ARY_IPV4_NS[@]}"; do

    echo "nameserver ${ns}" >> "${TARGET}/etc/resolv.conf"
    do_log "info" "file_only" "4035() IPv4 nameserver added: [${ns}]."

  done

  echo "" >> "${TARGET}/etc/resolv.conf"
  do_log "info" "file_only" "4035() IPv4 nameserver at: '${TARGET}/etc/resolv.conf' configured."


  ### Create '/etc/resolv.conf' IPv6 entries for static configuration.
  if [[ "${VAR_LINK_IPV6,,}" == "true" || -n "${VAR_FINAL_IPV6}" ]]; then

    cat << EOF >> "${TARGET}/etc/resolv.conf"
### Custom DNS IPv6 configuration
EOF

    for ns in "${ARY_IPV6_NS[@]}"; do

      echo "nameserver ${ns}" >> "${TARGET}/etc/resolv.conf"
      do_log "info" "file_only" "4035() IPv6 nameserver added: [${ns}]."

    done

    echo "" >> "${TARGET}/etc/resolv.conf"
    do_log "info" "file_only" "4035() IPv6 nameserver at: '${TARGET}/etc/resolv.conf' configured."

  fi

  cat << EOF >> "${TARGET}/etc/resolv.conf"
# vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=sh
EOF

  guard_dir && return 0
}
# vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=sh
