#!/bin/bash

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

check_virtualizer() {
   local MSG
   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")"

   if [ "$systemcheck_virtualizer_tool_unavailable" = "true" ]; then
      MSG="<p>Check Virtualizer Result: $systemcheck_virt_detection_tool not available. Skipping test.</p>"
      $output_x ${output_opts[@]} --messagex --typex "warning" --message "$MSG"
      $output_cli ${output_opts[@]} --messagecli --typecli "warning" --message "$MSG"
      EXIT_CODE="1"
      return 0
   fi

   if [ "$systemcheck_virtualizer_detected" = "none" ]; then
      ## Not running in a Virtual Machine (or none detected).
      MSG="<p>Check Virtualizer Result: No virtualizer detected, probably running on <u>bare metal</u>.</p>"
   elif [ "$systemcheck_virtualizer_detected" = "oracle" ]; then
      MSG="<p>Check Virtualizer Result: Supported virtualizer <u>VirtualBox</u> detected.
<br></br>$systemcheck_virt_detection_tool result: <code>$systemcheck_virtualizer_detected</code></p>"
   elif [ "$systemcheck_virtualizer_detected" = "kvm" ]; then
      MSG="<p>Check Virtualizer Result: Supported virtualizer <u>KVM</u> detected.
<br></br>$systemcheck_virt_detection_tool result: <code>$systemcheck_virtualizer_detected</code></p>"
   elif [ "$systemcheck_virtualizer_detected" = "qubes" ]; then
      MSG="<p>Check Virtualizer Result: Supported virtualizer <u>Qubes</u> detected.
<br></br>$systemcheck_virt_detection_tool result: <code>$systemcheck_virtualizer_detected</code></p>"
   else
      if [ "$vm_lower_case_short" = "machine" ]; then
         MSG="\
<p>Check Virtualizer Result: Supported virtualizer <u>$systemcheck_virtualizer_detected</u> detected.
<br></br>$systemcheck_virt_detection_tool result: <code>$systemcheck_virtualizer_detected</code></p>"
      else
         MSG="\
<p>Virtualizer <code>$systemcheck_virtualizer_detected</code> is unsupported by $PROJECT_NAME developers!
<br></br>
<br></br>Using Virtualizer this together with $PROJECT_NAME is recommended against.
<br></br>
<br></br><code>$systemcheck_virt_detection_tool</code> output:
<blockquote><code>$systemcheck_virtualizer_detected</code></blockquote>
This might endanger your anonymity. Do not proceed unless you know what you are doing.
<br></br>
Recommended action:
<br></br>- Shut down.
<br></br>- Read $PROJECT_NAME documentation.
<br></br>- Use $PROJECT_NAME on a supported platform.
<br></br>
<br></br>Links:
<br></br>
<br></br>- <a href=${PROJECT_HOMEPAGE}/wiki/Dev/Leak_Tests>Leak Tests</a>
<br></br>- <a href=${PROJECT_HOMEPAGE}/wiki/Protocol-Leak-Protection_and_Fingerprinting-Protection>Protocol Leak Protection and Fingerprinting Protection</a>
<br></br>- <a href=${PROJECT_HOMEPAGE}/wiki/Documentation>Documentation</a>
<br></br>
<br></br>$if_you_know_what_you_are_doing_msg</p>"
         EXIT_CODE="1"
         $output_x ${output_opts[@]} --messagex --typex "error" --message "$MSG"
         $output_cli ${output_opts[@]} --messagecli --typecli "error" --message "$MSG"
         return 0
      fi
   fi

   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
}
