#!/bin/bash

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

pvclock_unwanted_detected() {
   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>PVClock Test Result:

<br></br>Unwanted PVClock <code>$available_clocksource</code> detected! \
Using this PVClock together with $PROJECT_NAME is recommended against, because it conflicts with $PROJECT_NAME's TimeSync design [1].</p>

$if_you_know_what_you_are_doing_msg

<p>Recommended action:
<br></br>- If you are using KVM, you probably did not follow $PROJECT_NAME's KVM instructions. [2]</p>
<br></br>- Or use a different supported $PROJECT_NAME platform. [3]</p>

<p>[1] <a href=${PROJECT_HOMEPAGE}/wiki/Dev/TimeSync>${PROJECT_HOMEPAGE}/wiki/Dev/TimeSync</a>
<br></br>[2] <a href=${PROJECT_HOMEPAGE}/wiki/KVM>${PROJECT_HOMEPAGE}/wiki/KVM</a>
<br></br>[3] <a href=${PROJECT_HOMEPAGE}/wiki/Supported_Platforms>${PROJECT_HOMEPAGE}/wiki/Supported_Platforms</a>
</p>"

   $output_x ${output_opts[@]} --messagex --typex "warning" --message "$MSG"
   $output_cli ${output_opts[@]} --messagecli --typecli "warning" --message "$MSG"
   EXIT_CODE="1"

   return 0
}

check_pvclock() {
   if [ -f "/run/qubes-whonix/qubes.SetDateTime" ]; then
      qubes_set_date_time_content="$(cat "/run/qubes-whonix/qubes.SetDateTime")" || true
      local MSG="<p>\
PVClock Result: dom0 is telling us the time. \
File <code>/run/qubes-whonix/qubes.SetDateTime</code> exists. \
Its content is <code>$qubes_set_date_time_content</code>. \
This is non-ideal. A known issue. Contributions happily considered. Read more: <a href=https://phabricator.whonix.org/T397>https://phabricator.whonix.org/T397</a></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
   fi

   if [ ! -f "/sys/devices/system/clocksource/clocksource0/current_clocksource" ]; then
      local MSG="<p>PVClock Result: <code>/sys/devices/system/clocksource/clocksource0/current_clocksource</code> does not exist, 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

   local current_clocksource available_clocksource
   current_clocksource="$(leaprun sys-current-clocksource)" || true
   available_clocksource="$(leaprun sys-available-clocksource)" || true

   if [ "$current_clocksource" = "xen" ]; then
      local MSG="<p>PVClock Result: <code>/sys/devices/system/clocksource/clocksource0/current_clocksource</code> exist, is <code>$current_clocksource</code>. \
This is non-ideal. A known issue. Contributions happily considered. Read more: <a href=https://phabricator.whonix.org/T389>https://phabricator.whonix.org/T389</a></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

   ## Variable systemcheck_virtualizer_detected has been set by check_virtualizer.

   ## Only check for kvm, since this is not yet implemented for other platforms.
   ## Qubes: https://phabricator.whonix.org/T389
   ## VirtualBox: https://phabricator.whonix.org/T437
   if [ "$systemcheck_virtualizer_detected" = "kvm" ]; then
      if [ "$current_clocksource" = "kvm-clock" ]; then
         ## Skip this check on Whonix-Gateway.
         ## https://phabricator.whonix.org/T385#7651
         if [ "$vm_lower_case_short" = "gateway" ]; then
            true
         elif [ "$vm_lower_case_short" = "machine" ]; then
            true
         else
            systemcheck_run_function pvclock_unwanted_detected
         fi
      fi
   fi

   local MSG="<p>PVClock Result: <code>/sys/devices/system/clocksource/clocksource0/current_clocksource</code> exist, is <code>$current_clocksource</code>.</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
}
