#!/bin/bash

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

check_tor_enabled() {
   if test -f /run/qubes/this-is-templatevm ; then
      local MSG='<p>Tor Check Result: Skipped, because running in TemplateVM, 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

   ## provided by: /usr/libexec/helper-scripts/tor_enabled_check
   ## sets: TOR_ENABLED
   check_tor_enabled_do

   if [ "$TOR_ENABLED" = "0" ]; then
      ## When Tor is not yet enabled, i.e. if "DisableNetwork 0" is set in
      ## Tor config and systemcheck is manually started, show the following
      ## message and exit. Otherwise, if systemcheck is automatically started
      ## just be quit and exit.

      local MSG="<p>Tor Check Result:
<b>Tor is disabled.</b> Therefore you most likely can not connect to the internet.<br /><br />
(Debugging information: Could not find <code>DisableNetwork 0</code> in Tor config.)<br /><br />
Please close this window and enable Tor using Anon Connection Wizard!
<blockquote>    $start_menu_instructions_system_first_part Anon Connection Wizard</blockquote>
    or in Terminal: <code>sudo setup-dist</code><br /><br />
or manually (If you know about the public Tor network!) and open <code>/usr/local/etc/torrc.d/50_user.conf</code> with root rights
($start_menu_instructions_system_first_part Tor User Config) and set:
<blockquote><code>DisableNetwork 0</code></blockquote>
Then run <code>systemcheck</code> again.</p>"

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

      EXIT_CODE="1"
      cleanup "1"
      return 0
   else
      if [ -e "/usr/share/anon-gw-base-files/gateway" ]; then
         local MSG='<p>Tor Check Result: "DisableNetwork 1" not active, 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>Tor Check Result: Not running on Whonix-Gateway, 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
      fi
   fi
}
