#!/bin/bash

## Copyright (C) 2012 - 2020 ENCRYPTED SUPPORT LP <adrelanos@riseup.net>
## See the file COPYING for copying conditions.

set -x
set -o pipefail

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

trap "error_handler" ERR

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

stop_tor() {
      exit_code="0"
      systemctl --no-pager status tor@default || { exit_code="$?"; true; };
      sync
      sleep 1

      if [ "$exit_code" = "0" ]; then
         dialog --title "whonixsetup - Info" --msgbox 'The Tor process is still running. Stopping it now...' 640 480

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

         dialog --title "whonixsetup - Info" --msgbox 'Stopped the Tor process.' 640 480
      else
         dialog --title "whonixsetup - Info" --msgbox 'The Tor process was already stopped.' 640 480
      fi
}

MSG="
Do you want to disable Tor networking?
"

TITLE="whonixsetup - Disable Tor Networking"

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_disabled() function will:
## 1. create 40_tor_control_panel.conf if it was missing
## 2. set final value of DisableNetwork in the file to 1
## 3. stop Tor using systemd

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

if [ "$exit_code" = "0" ]; then
   true "INFO: Ok, exit 1, so whonixsetup will end."
   dialog --title "whonixsetup - Success!" --msgbox 'Tor networking has been disabled.

Note: whonixsetup disables Tor networking by writing "DisableNetwork 1" to /usr/local/etc/torrc.d/40_tor_control_panel.conf
' 640 480
   ## Signal for whonixsetup to break the while loop.
   exit 1
else
   dialog --title "whonixsetup - ERROR!" --msgbox 'Tor networking could not be disabled. Please report this bug!

Note: whonixsetup tried to disable Tor networking by writing "DisableNetwork 1" to /usr/local/etc/torrc.d/40_tor_control_panel.conf but was unable to.
' 640 480

   dialog --title "whonixsetup - WARNING" --msgbox 'Trying to stop the Tor process...

Warning: Since Tor networking could not be disabled in /usr/local/etc/torrc.d/40_tor_control_panel.conf, Tor will reconnect the next time you reboot or restart Tor. You can try to manually comment "DisableNetwork 0" in /usr/local/etc/torrc.d/40_tor_control_panel.conf and run "sudo service tor@default stop" to ensure that Tor will not start.
' 640 480

   stop_tor
   true "INFO: Ok, exit 1, so whonixsetup will end."
   ## Signal for whonixsetup to break the while loop.
   exit 1
fi
