#!/bin/bash

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

check_timezone() {
   if [ "$vm_lower_case_short" = "machine" ]; then
      return 0
   fi

   local timezone_file_location timezone_file_expected_content timezone_file_actual_content timezone_status_code timezone_check_result
   local localtime_file_etc localtime_result localtime_status_code

   timezone_check_result="true"
   timezone_status_code="0"
   timezone_file_location="/etc/timezone"
   timezone_file_expected_content="Etc/UTC"

   localtime_status_code="0"
   localtime_file_etc="/etc/localtime"
   localtime_file_usr="/usr/share/zoneinfo/Etc/UTC"

   if [ -f "$timezone_file_location" ]; then
      timezone_file_actual_content="$(cat "$timezone_file_location" 2>&1)" || { timezone_status_code="$?" ; true; };
      if [ "$timezone_file_actual_content" = "$timezone_file_expected_content" ]; then
         local MSG="<p>Check Timezone Result: <code>$timezone_file_location</code>, <code>$timezone_file_actual_content</code> matches <code>$timezone_file_expected_content</code>, 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
      else
         timezone_status_code="3"
         local MSG="<p>Check Timezone Result:
<br></br><code>$timezone_file_location</code> settings different from $PROJECT_NAME defaults.
<br></br>timezone_file_expected_content: <code>$timezone_file_expected_content</code>
<br></br>timezone_file_actual_content: <code>$timezone_file_actual_content</code></p>"
         $output_x ${output_opts[@]} --messagex --typex "error" --message "$MSG"
         $output_cli ${output_opts[@]} --messagecli --typecli "error" --message "$MSG"
         EXIT_CODE="1"
      fi
   else
      timezone_status_code="2"
      timezone_file_actual_content="File <code>$timezone_file_location</code> does not exist."
   fi

   if [ ! "$timezone_status_code" = "0" ]; then
      timezone_check_result="false"
   fi

   if [ -f "$localtime_file_etc" ]; then
      if [ -f "$localtime_file_usr" ]; then
         localtime_result="$(diff "$localtime_file_usr" "$localtime_file_etc" 2>&1)" || { localtime_status_code="$?" ; true; };
         if [ "$localtime_status_code" = "0" ]; then
            local MSG="<p>Check Timezone Result: <code>$localtime_file_usr</code> matches <code>$localtime_file_etc</code>, 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
         else
            local MSG="<p>Check Timezone Result:
<br></br>localtime_file_etc <code>$localtime_file_etc</code> settings different from <code>$localtime_file_usr</code>. Different from $PROJECT_NAME defaults.
<br></br>localtime_status_code: <code>$localtime_status_code</code>
<br></br>localtime_result: <code>$localtime_result</code></p>"
            $output_x ${output_opts[@]} --messagex --typex "error" --message "$MSG"
            $output_cli ${output_opts[@]} --messagecli --typecli "error" --message "$MSG"
            EXIT_CODE="1"
         fi
      else
         localtime_status_code="2"
         localtime_result="File <code>$localtime_file_usr</code> does not exist."
         local MSG="<p>Check Timezone Result: $localtime_result</p>"
         $output_x ${output_opts[@]} --messagex --typex "error" --message "$MSG"
         $output_cli ${output_opts[@]} --messagecli --typecli "error" --message "$MSG"
         EXIT_CODE="1"
      fi
   else
      localtime_status_code="3"
      localtime_result="File <code>$localtime_file_etc</code> does not exist."
      local MSG="<p>Check Timezone Result: $localtime_result</p>"
      $output_x ${output_opts[@]} --messagex --typex "error" --message "$MSG"
      $output_cli ${output_opts[@]} --messagecli --typecli "error" --message "$MSG"
      EXIT_CODE="1"
   fi

   if [ ! "$localtime_status_code" = "0" ]; then
      timezone_check_result="false"
   fi

   if [ "$timezone_check_result" = "false" ]; then
      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>Check Timezone Result:
<br></br><b>Settings different from $PROJECT_NAME defaults. (See above!)</b>
<br></br>You could try to fix this.
<blockquote>    $persistent_changes_start_menu_instructions_first_part Konsole</blockquote>
<blockquote><code>sudo su
<br></br>echo \"Etc/UTC\" > /etc/timezone
<br></br>cp \"$localtime_file_usr\" \"$localtime_file_etc\"</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.
<br></br>$if_you_know_what_you_are_doing_msg
[1] <a href=${PROJECT_HOMEPAGE}/wiki/Dev/Design-Shared#timezone>${PROJECT_HOMEPAGE}/wiki/Dev/Design-Shared#timezone</a></p>"
      $output_x ${output_opts[@]} --messagex --typex "error" --message "$MSG"
      $output_cli ${output_opts[@]} --messagecli --typecli "error" --message "$MSG"
      EXIT_CODE="1"
   fi
}
