#!/bin/bash

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

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

   ## Initialize local variables and fallbacks.
   local hostname_exit_code_was_non_zero="false"
   local hostname_discouraged_setting_detected="false"

   local hostname_exit_code="0"
   hostname_fqdn_output="$(hostname --fqdn 2>&1)" || { hostname_exit_code="$?" ; true; };
   if [ ! "$hostname_exit_code" = "0" ]; then
      hostname_exit_code_was_non_zero="true"
   fi
   if [ "$hostname_fqdn_output" = "host.localdomain" ]; then
      local MSG="<p>Check Hostname Result: \"hostname --fqdn\" output is \"host.localdomain\", 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
      hostname_discouraged_setting_detected="true"
      local MSG="<p>Check Hostname Result: <b>\"hostname --fqdn\" output is not \
\"host.localdomain\", it is \"$hostname_fqdn_output\".</b> (hostname_exit_code: $hostname_exit_code)</p>"
      $output_x ${output_opts[@]} --messagex --typex "error" --message "$MSG"
      $output_cli ${output_opts[@]} --messagecli --typecli "error" --message "$MSG"
      EXIT_CODE="1"
   fi

   local hostname_exit_code="0"
   hostname_output="$(hostname 2>&1)" || { hostname_exit_code="$?" ; true; };
   if [ ! "$hostname_exit_code" = "0" ]; then
      hostname_exit_code_was_non_zero="true"
   fi
   if [ "$hostname_output" = "host" ]; then
      local MSG="<p>Check Hostname Result: \"hostname\" output is \"host\", 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
      hostname_discouraged_setting_detected="true"
      local MSG="<p>Check Hostname Result: <b>\"hostname\" output is not \"host\", \
it is \"$hostname_output\".</b> (hostname_exit_code: $hostname_exit_code)</p>"
      $output_x ${output_opts[@]} --messagex --typex "error" --message "$MSG"
      $output_cli ${output_opts[@]} --messagecli --typecli "error" --message "$MSG"
      EXIT_CODE="1"
   fi

   local hostname_exit_code="0"
   hostname_ip_address_output="$(hostname --ip-address 2>&1)" || { hostname_exit_code="$?" ; true; };
   if [ ! "$hostname_exit_code" = "0" ]; then
      hostname_exit_code_was_non_zero="true"
   fi
   if [ "$hostname_ip_address_output" = "127.0.0.1" ]; then
      local MSG="<p>Check Hostname Result: \"hostname --ip-address\" output is \"127.0.0.1\", 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
      hostname_discouraged_setting_detected="true"
      local MSG="<p>Check Hostname Result: <b>\"hostname --ip-address\" is not \
\"127.0.0.1\", it is \"$hostname_ip_address_output\".</b> (hostname_exit_code: $hostname_exit_code)</p>"
      $output_x ${output_opts[@]} --messagex --typex "error" --message "$MSG"
      $output_cli ${output_opts[@]} --messagecli --typecli "error" --message "$MSG"
      EXIT_CODE="1"
   fi

   local hostname_exit_code="0"
   hostname_domain_output="$(hostname --domain 2>&1)" || { hostname_exit_code="$?" ; true; };
   if [ ! "$hostname_exit_code" = "0" ]; then
      hostname_exit_code_was_non_zero="true"
   fi
   if [ "$hostname_domain_output" = "localdomain" ]; then
      local MSG="<p>Check Hostname Result: \"hostname --ip-address\" output is \"localdomain\", 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
      hostname_discouraged_setting_detected="true"
      local MSG="<p>Check Hostname Result: <b>\"hostname --domain\" is not \
\"localdomain\"</b>, it is \"$hostname_domain_output\". (hostname_exit_code: $hostname_exit_code)</p>"
      $output_x ${output_opts[@]} --messagex --typex "error" --message "$MSG"
      $output_cli ${output_opts[@]} --messagecli --typecli "error" --message "$MSG"
      EXIT_CODE="1"
   fi

   if [ "$hostname_exit_code_was_non_zero" = "true" ]; then
      hostname_discouraged_setting_detected="true"
      local MSG="<p>Check Hostname Result: <b>\"hostname\" exited at least once with \
a non-zero exit code.</b> This is probably a network misconfiguration. See above.</p>"
      $output_x ${output_opts[@]} --messagex --typex "error" --message "$MSG"
      $output_cli ${output_opts[@]} --messagecli --typecli "error" --message "$MSG"
      EXIT_CODE="1"
   fi

   if [ "$hostname_discouraged_setting_detected" = "true" ]; 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 Hostname Result:
<br></br><b>Hostname settings different from $PROJECT_NAME defaults.</b> (See above.)
<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"
   fi
}
