#!/bin/bash
# SPDX-Version: 3.0
# SPDX-CreationInfo: 2025-10-11; WEIDNER, Marc S.; <msw@coresecret.dev>
# SPDX-ExternalRef: GIT https://git.coresecret.dev/msw/CISS.debian.live.builder.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.live.builder
# SPDX-Security-Contact: security@coresecret.eu
set -Ceuo pipefail

printf "\e[95m++++ ++++ ++++ ++++ ++++ ++++ ++ 🧪 '%s' starting ... \e[0m\n" "${0}"

mkdir -p /var/log/chrony

[[ -r /root/ciss_xdg_tmp.sh ]] && . /root/ciss_xdg_tmp.sh
export DEBIAN_FRONTEND="noninteractive" INITRD="No"
export TZ="Etc/UTC"

apt-get install -y adjtimex chrony tzdata

systemctl enable chrony.service

mv /etc/chrony/chrony.conf /root/.ciss/cdlb/backup/chrony.conf.bak
chmod 0644 /root/.ciss/cdlb/backup/chrony.conf.bak

cat << EOF >| /etc/chrony/chrony.conf
# SPDX-Version: 3.0
# SPDX-CreationInfo: 2025-10-11; WEIDNER, Marc S.; <msw@coresecret.dev>
# SPDX-ExternalRef: GIT https://git.coresecret.dev/msw/CISS.debian.live.builder.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.live.builder
# SPDX-Security-Contact: security@coresecret.eu

# Include configuration files found in /etc/chrony/conf.d.
confdir /etc/chrony/conf.d
driftfile /var/lib/chrony/chrony.drift
keyfile /etc/chrony/chrony.keys
logdir /var/log/chrony
ntsdumpdir /var/lib/chrony
sourcedir /run/chrony-dhcp
sourcedir /etc/chrony/sources.d

log tracking measurements statistics

authselectmode require

# server ntp.ripe.net      iburst nts minpoll 5 maxpoll 9
server ptbtime3.ptb.de   iburst nts minpoll 5 maxpoll 9
server ptbtime2.ptb.de   iburst nts minpoll 5 maxpoll 9
server ptbtime1.ptb.de   iburst nts minpoll 5 maxpoll 9
# server ntp13.metas.ch    iburst nts minpoll 5 maxpoll 9
# server time-c-b.nist.gov iburst nts minpoll 5 maxpoll 9
# server sth1.ntp.se       iburst nts minpoll 5 maxpoll 9
server ntp0.fau.de       iburst nts minpoll 5 maxpoll 9

leapsectz right/UTC

leapsecmode system

maxupdateskew 100.0

rtcsync

makestep 0.25 3

# vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=conf
EOF

chmod 0644 /etc/chrony/chrony.conf

[[ -f /root/.ciss/check_chrony.sh ]] && chmod 0700 /root/.ciss/check_chrony.sh

### Build right/UTC from tzdata leap table if missing.
if [[ ! -e /usr/share/zoneinfo/right/UTC ]]; then

  install -d -m 0755 /usr/share/zoneinfo/right

  ### Minimal zic source for a fixed UTC zone.
  declare -r tmp_src="/tmp/UTC.src"
  printf 'Zone UTC  0  -  UTC\n' > "${tmp_src}"

  ### Prefer the zic-format leapseconds file.
  declare leap_zic="/usr/share/zoneinfo/leapseconds"

  if [[ -s "${leap_zic}" ]]; then

    zic -d /usr/share/zoneinfo/right -L "${leap_zic}" "${tmp_src}"

  else

    echo "WARNING: ${leap_zic} not found; building right/UTC without leap info." >&2
    zic -d /usr/share/zoneinfo/right -L /dev/null "${tmp_src}"

  fi

  rm -f "${tmp_src}"

fi

if [[ -e /usr/share/zoneinfo/right/UTC ]]; then

  ### Expect to see 'Sat Dec 31 23:59:60 UTC 2016' rendered in right/UTC
  TZ=right/UTC date -ud '2017-01-01 00:00:00 -1 second' || true

fi

chronyd -Q -f /etc/chrony/chrony.conf 2>&1

printf "\e[92m++++ ++++ ++++ ++++ ++++ ++++ ++ ✅ '%s' applied successfully. \e[0m\n" "${0}"

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