#!/bin/bash

## Copyright (C) 2012 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## 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

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

## root check
if [ "$(id -u)" = "0" ]; then
    echo "ERROR: This must NOT 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_wrapper --title "setup-dist - 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_wrapper --title "setup-dist - Info" --msgbox 'Stopped the Tor process.' 640 480
      else
         dialog_wrapper --title "setup-dist - Info" --msgbox 'The Tor process was already stopped.' 640 480
      fi
}

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

TITLE="setup-dist - Disable Tor Networking"

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_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

leaprun setup-dist-disable-tor >/dev/null 2>&1  || { exit_code="$?" ; true; };

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

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

Note: setup-dist 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_wrapper --title "setup-dist - 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 setup-dist will end."
   ## Signal for setup-dist to break the while loop.
   exit 1
fi
