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

#######################################
# Set locale, locale overrides and configure keyboard layout.
# Globals:
#   ARY_LOCALE
#   TARGET
#   VAR_ARCHITECTURE
#   VAR_CODENAME
#   VAR_VERSION
#   locale_country
#   locale_keyboard_xkb_keymap
#   locale_language
#   locale_override_address
#   locale_override_collate
#   locale_override_ctype
#   locale_override_measurement
#   locale_override_messages
#   locale_override_monetary
#   locale_override_name
#   locale_override_numeric
#   locale_override_paper
#   locale_override_telephone
#   locale_override_time
# Arguments:
#   None
# Returns:
#   0: on success
#######################################
setup_locales() {
  ### Declare Arrays, HashMaps, and Variables.
  declare     var_locale_hook="/root/.ciss/cdi/hooks/4050_setup_locales.hooks.sh"

  ### Give priority to '${locale_locale}' over separately configured variables '${locale_country}' and '${locale_language}'.
  ### If 'locale_locale' is not set, build it from 'locale_language' and 'locale_country'.
  if [[ -n "${locale_language:-}" && -n "${locale_country:-}" && -z "${ARY_LOCALE[0]:-}" ]]; then
    ARY_LOCALE+="${locale_language}_${locale_country}.UTF-8"
  fi

  ### Creat Hook in target.
  cat << EOF >| "${TARGET}${var_locale_hook}"
#!/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

set -Ceuo pipefail

declare -a ary_locale=()
declare -r locale_0=${ARY_LOCALE[0]:-en_US.UTF-8}
declare -r locale_1=${ARY_LOCALE[1]:-}
declare    loc=""

EOF

  cat << 'EOF' >> "${TARGET}${var_locale_hook}"
ary_locale+=( "${locale_0}" )
[[ -n "${locale_1}" ]] && ary_locale+=( "${locale_1}" )

for loc in "${ary_locale[@]}"; do
  if ! grep -qx "${loc} UTF-8" /etc/locale.gen; then
    sed -i "s/^# ${loc} UTF-8/${loc} UTF-8/" /etc/locale.gen || echo "${loc} UTF-8" >> /etc/locale.gen
  fi
done

locale-gen --purge "${ary_locale[@]}"

for loc in "${ary_locale[@]}"; do
  locale_check=$(echo "${loc}" | sed 's/UTF-8/utf8/')
  if ! locale -a | grep -qx "${locale_check}"; then
    echo "Locale '${loc}' has not been generated successfully." >&2
    exit 1
  fi
done

EOF

  cat << EOF >> "${TARGET}${var_locale_hook}"
update-locale \
LANG=${ARY_LOCALE[0]} \
LC_ADDRESS=${locale_override_address:-${ARY_LOCALE[0]}} \
LC_COLLATE=${locale_override_collate:-${ARY_LOCALE[0]}} \
LC_CTYPE=${locale_override_ctype:-${ARY_LOCALE[0]}} \
LC_MEASUREMENT=${locale_override_measurement:-${ARY_LOCALE[0]}} \
LC_MESSAGES=${locale_override_messages:-${ARY_LOCALE[0]}} \
LC_MONETARY=${locale_override_monetary:-${ARY_LOCALE[0]}} \
LC_NAME=${locale_override_name:-${ARY_LOCALE[0]}} \
LC_NUMERIC=${locale_override_numeric:-${ARY_LOCALE[0]}} \
LC_PAPER=${locale_override_paper:-${ARY_LOCALE[0]}} \
LC_TELEPHONE=${locale_override_telephone:-${ARY_LOCALE[0]}} \
LC_TIME=${locale_override_time:-${ARY_LOCALE[0]}} \
LC_IDENTIFICATION=${ARY_LOCALE[0]}

# vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=sh
EOF
  chmod 0700 "${TARGET}${var_locale_hook}"
  do_in_target_script "${TARGET}" "${var_locale_hook}"

  ### Set the keyboard layout for the system (for consoles).
  cat << EOF >| "${TARGET}/etc/default/keyboard"
# 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/default/keyboard : Generated by CISS.debian.installer ${VAR_VERSION}
# Architecture          : ${VAR_ARCHITECTURE}
# Distribution          : ${VAR_CODENAME}

# KEYBOARD CONFIGURATION FILE
# Consult the keyboard(5) manual page.

XKBMODEL="pc105"
XKBLAYOUT="${locale_keyboard_xkb_keymap}"
XKBVARIANT=""
XKBOPTIONS=""
BACKSPACE="guess"

# vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=conf
EOF
  chmod 0644 "${TARGET}/etc/default/keyboard"
  do_log "info" "file_only" "4050() Keyboard layout updated: 'XKBLAYOUT=${locale_keyboard_xkb_keymap}' -> '${TARGET}/etc/default/keyboard'."

  # TODO: Move this command later than 4131_installation_systemd.sh
  ### Set the X11 keyboard layout (for graphical environments).
  #do_in_target "${TARGET}" localectl set-x11-keymap "${locale_keyboard_xkb_keymap}"

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