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

#######################################
# Generate files: '/etc/hostname' | '/etc/hosts' | '/etc/mailname'
# Globals:
#   TARGET
#   VAR_FINAL_FQDN
#   VAR_FINAL_IPV4
#   VAR_FINAL_IPV6
#   VAR_LINK_IPV6
#   network_ipv6
# Arguments:
#   None
# Returns:
#   0: on success
#######################################
setup_hostname() {
  ### Create '${TARGET}/etc/hostname' file.
  cat << EOF >| "${TARGET}/etc/hostname"
${VAR_FINAL_FQDN}
EOF
  chmod 0644 "${TARGET}/etc/hostname"
  do_log "info" "true" "File generated: '${TARGET}/etc/hostname' | hostname '${VAR_FINAL_FQDN}'."

  ### Create '${TARGET}/etc/mailname' file.
  cat << EOF >| "${TARGET}/etc/mailname"
${VAR_FINAL_FQDN}
EOF
  chmod 0644 "${TARGET}/etc/mailname"
  do_log "info" "true" "File generated: '${TARGET}/etc/mailname' | mailname '${VAR_FINAL_FQDN}'."

  ### Generate '${TARGET}/etc/hosts' basic IPv4 entries
  cat << EOF >| "${TARGET}/etc/hosts"
127.0.0.1 localhost
${VAR_FINAL_IPV4} ${VAR_FINAL_FQDN}

EOF
  chmod 0644 "${TARGET}/etc/hosts"
  do_log "info" "true" "File generated: '${TARGET}/etc/hosts' with basic IPv4 entries."

  ### Generate '${TARGET}/etc/hosts' basic IPv6 entries
  if [[ "${VAR_LINK_IPV6,,}" == "true" || "${network_ipv6,,}" == "true" ]]; then

    cat << EOF >> "${TARGET}/etc/hosts"
# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
${VAR_FINAL_IPV6} ${VAR_FINAL_FQDN}

EOF

    do_log "info" "true" "File updated: '${TARGET}/etc/hosts' with basic IPv6 entries."

  fi

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