#!/bin/bash

## Copyright (C) 2012 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

check_systemd_clock() {
   if ! command -v timedatectl >/dev/null 2>&1 ; then
      local MSG="<p>Systemd Clock Check Result: <code>timedatectl</code> not available, ok.</p>"
      if [ "$verbose" -ge "1" ]; then
         $output_x ${output_opts[@]} --messagex --typex "info" --message "$MSG"
         $output_cli ${output_opts[@]} --messagecli --typecli "info" --message "$MSG"
      fi
      return 0
   fi

   ## timedatectl example output on Debian jessie:
   ##       Local time: Fri 2015-07-10 16:22:26 UTC
   ##   Universal time: Fri 2015-07-10 16:22:26 UTC
   ##         RTC time: Fri 2015-07-10 16:22:24
   ##        Time zone: Etc/UTC (UTC, +0000)
   ##      NTP enabled: no
   ## NTP synchronized: no
   ##  RTC in local TZ: no
   ##       DST active: n/a

   ## timedatectl example output on Debian stretch:
   ##       Local time: Wed 2016-11-23 05:58:44 UTC
   ##   Universal time: Wed 2016-11-23 05:58:44 UTC
   ##         RTC time: Wed 2016-11-23 05:58:59
   ##        Time zone: Etc/UTC (UTC, +0000)
   ##  Network time on: yes
   ## NTP synchronized: no
   ##  RTC in local TZ: no

   ## Not checking for 'Time zone: Etc/UTC (UTC, +0000)'. This is left to check_timezone.
   ## Not checking for 'Time zone: DST active: n/a'. This is left to check_timezone.

   timedatectl_output_original="$(timedatectl)"
   timedatectl_output_pretty="$(br_add "$timedatectl_output_original")"

   local timedatectl_ok_counter_actual="0"
   local timedatectl_ok_counter_expected="3"
   local timedatectl_line
   while read -r -d $'\n' timedatectl_line; do
      timedatectl_line="${timedatectl_line,,}"
      if [ "$timedatectl_line" = 'ntp enabled: no' ] || [ "$timedatectl_line" = 'network time on: no' ]; then
         timedatectl_ok_counter_actual="$(( $timedatectl_ok_counter_actual + 1 ))"
      fi
      if [ "$timedatectl_line" = 'ntp synchronized: no' ]; then
         timedatectl_ok_counter_actual="$(( $timedatectl_ok_counter_actual + 1 ))"
      fi
      if [ "$timedatectl_line" = 'rtc in local tz: no' ]; then
         timedatectl_ok_counter_actual="$(( $timedatectl_ok_counter_actual + 1 ))"
      fi
   done < <( echo "$timedatectl_output_original" )

   if [ "$timedatectl_ok_counter_actual" -ge "$timedatectl_ok_counter_expected" ]; then
      local MSG="<p>Systemd Clock Check Result: set to UTC and NTP disabled as expected, ok.
<br></br>timedatectl_output_pretty:
<blockquote><code>$timedatectl_output_pretty</code></blockquote></p>"
      if [ "$verbose" -ge "1" ]; then
         $output_x ${output_opts[@]} --messagex --typex "info" --message "$MSG"
         $output_cli ${output_opts[@]} --messagecli --typecli "info" --message "$MSG"
      fi
   else
      local if_you_know_what_you_are_doing_msg
      if_you_know_what_you_are_doing_msg="$(if_you_know_what_you_are_doing_funct "$FUNCNAME")"
      local MSG="<p>Systemd Clock Check Result:
<br></br><b>Unexpected results by timedatectl.</b>
<br></br>timedatectl_output_pretty:
<blockquote><code>$timedatectl_output_pretty</code></blockquote>
It is generally recommended to keep the default as per $PROJECT_NAME Design. [1]
If you did not change timezone related settings, please report this $PROJECT_NAME bug.
If you know what you are doing and changed this on purpose, feel free to
disable this check. [2]
<br />
<br />$if_you_know_what_you_are_doing_msg</p>"
      $output_x ${output_opts[@]} --messagex --typex "error" --message "$MSG"
      $output_cli ${output_opts[@]} --messagecli --typecli "error" --message "$MSG"
      EXIT_CODE="1"
      return 0
   fi
}
