#!/bin/bash

## Copyright (C) 2012 - 2020 ENCRYPTED SUPPORT LP <adrelanos@riseup.net>
## 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

tor_action_exit_code_check() {
   if [ ! "$exit_code" = "0" ]; then
      TITLE="whonixsetup - 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:
    sudo 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 --title "$TITLE" --msgbox "$MSG" 640 480
      true "INFO: Ok, exit 1, so whonixsetup will end."
      exit 1
   fi
}

## root check
if [ "$(id -u)" != "0" ]; then
    echo "ERROR: This must 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: whonixsetup enables Tor networking by adding "DisableNetwork 0" to /usr/local/etc/torrc.d/40_tor_control_panel.conf.
'

TITLE="whonixsetup - Connect to the Tor network."

exit_code="0"
dialog --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

/usr/lib/helper-scripts/repair_torrc.py
error_msg="$(python3 -c 'from tor_control_panel import tor_status; tor_status.set_enabled()' 2>&1)" || { exit_code="$?" ; true; };

if [ "$exit_code" != "0" ]; then
    TITLE="whonixsetup - Error!"
    MSG="
python3 -c 'from tor_control_panel import tor_status; tor_status.set_enabled()'
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 tor-control-panel

If there is something wrong with your Tor config, you can open /usr/local/etc/torrc.d/50_user.conf in the terminal:
    sudo nano /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 --title "$TITLE" --msgbox "$MSG" 640 480
    true "INFO: Ok, exit 1, so whonixsetup will end."
    exit 1
fi

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

Press Enter to run whonixcheck and exit.
"
dialog --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 whonixcheck.
exit_code="0"
ft_m_end || { exit_code="$?" ; true; };

true "INFO: Ok, exit 1, so whonixsetup will end."
exit 1
