#!/bin/bash

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

#set -x
set -o pipefail
set -o errtrace

error_handler() {
   local MSG="\
###########################################################
## Something went wrong. Please report this bug!
##
## BASH_COMMAND: $BASH_COMMAND
###########################################################\
"
   echo "$MSG"
   exit 1
}

trap "error_handler" ERR

source "/usr/libexec/setup-dist/shared"

tor_action_exit_code_check() {
   if [ ! "$exit_code" = "0" ]; then
      TITLE="setup-dist - Error!"
      MSG="
    sudo service tor@default $service_action

returned exit code $exit_code, which means Tor does NOT work.
Maybe your Whonix-Gateway has only one network card attached? Most likely there
is something wrong with your Tor config.

You can open /usr/local/etc/torrc.d/50_user.conf in the terminal:
    sudoedit nano /usr/local/etc/torrc.d/50_user.conf

Or, if you are using a graphical Whonix-Gateway:
    Start Menu -> Applications -> Tor Config File

Running:

   sudo service tor@default restart

might help with troubleshooting.
"
      dialog_wrapper --title "$TITLE" --msgbox "$MSG" 640 480
      true "INFO: Ok, exit 1, so setup-dist will end."
      exit 1
   fi
}

## root check
if [ "$(id -u)" = "0" ]; then
    echo "ERROR: This must NOT be run as root (sudo)!"
    exit 1
fi

MSG='
Connect to the public Tor network now?

By default, Tor does not try to hide the fact that you are using it. If this concerns you, select "No" now. Then, select the menu option "Tor is censored or dangerous in my area" and follow the instructions.

Ultimately, the best protection is a social approach: the more Tor users there are near you and the more diverse their interests, the safer it will be to use it. Convince other people to use Tor!

Note: setup-dist enables Tor networking by adding "DisableNetwork 0" to /usr/local/etc/torrc.d/40_tor_control_panel.conf.
'

TITLE="setup-dist - Connect to the Tor network."

exit_code="0"
dialog_wrapper --title "$TITLE" --yesno "$MSG" 640 480 || { exit_code="$?" ; true; };

if [ ! "$exit_code" = "0" ]; then
   ## Back to main menu.
   exit 0
fi

## set_enabled() function will:
## 1. create 40_tor_control_panel.conf if it was missing
## 2. set final value of DisableNetwork in the file to 0
## 3. restart Tor using systemd

error_msg="$(/usr/libexec/setup-dist/setup-dist-enable-tor 2>&1)" || { exit_code="$?" ; true; };

if [ "$exit_code" != "0" ]; then
    TITLE="setup-dist - Error!"
    MSG="
/usr/libexec/setup-dist/setup-dist-enable-tor
returned exit code $exit_code, which means failed to enable Tor.

Error messages are as follows:
$error_msg

If some dependencies are missing (for example, cannot import name 'tor_status'):
    sudo apt-get install anon-connection-wizard

If there is something wrong with your Tor config, you can open /usr/local/etc/torrc.d/50_user.conf in the terminal:
    sudoedit /usr/local/etc/torrc.d/50_user.conf

Or, if you are using a graphical Whonix-Gateway:
    Start Menu -> System -> Tor User Config

Useful troubleshooting commands:

    sudo service tor@default restart

Or for more detailed Tor config report:
    anon-verify -v
"
    dialog_wrapper --title "$TITLE" --msgbox "$MSG" 640 480
    true "INFO: Ok, exit 1, so setup-dist will end."
    exit 1
fi

TITLE="setup-dist - Success!"
MSG="
Tor has been successfully enabled.

Press Enter to run systemcheck and exit.
"
dialog_wrapper --title "$TITLE" --msgbox "$MSG" 640 480

# exit_code="0"
# service_action="status"
# systemctl --no-pager "$service_action" tor@default || { exit_code="$?" ; true; };
# sync
# sleep 1
# tor_action_exit_code_check

## Start systemcheck.
exit_code="0"
ft_m_end || { exit_code="$?" ; true; };

true "INFO: Ok, exit 1, so setup-dist will end."
exit 1
