#!/bin/bash

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

## Guarantee the existence of:
## /etc/tor/torrc
## /etc/torrc.d/95_whonix.conf
## /usr/local/etc/tor/torrc/40_tor_control_panel.conf
## /usr/local/etc/tor/torrc/50_user.conf
##
##           "%include /etc/torrc.d/*.conf" line in /etc/tor/torrc file
## "%include /usr/local/etc/torrc.d/*.conf" line in /etc/torrc.d/95_whonix.conf

set -x
set -e

true "$0: START"

if test -f /usr/share/anon-gw-base-files/gateway ; then
   true "INFO: Whonix detected, ok."
else
   true "\
WARNING: $0 is not yet implemented for non-Whonix, see:
https://forums.whonix.org/t/tor-controller-gui-tor-control-panel/5444/94"
   exit 0
fi

torrcd_path='/etc/torrc.d/95_whonix.conf'
torrc_file_path='/usr/local/etc/torrc.d/40_tor_control_panel.conf'
torrc_user_file_path='/usr/local/etc/torrc.d/50_user.conf'

for folder_name in /etc/tor /etc/torrc.d /usr/local/etc/torrc.d ; do
   if test -d "$folder_name" ; then
      true "INFO: folder $folder_name already exists, ok."
   else
      true "INFO: folder $folder_name does not exist yet, creating..."
      mkdir --parents "$folder_name"
   fi
done

if test -f /etc/tor/torrc ; then
   true "INFO: file /etc/tor/torrc already exists, ok."
   if grep -q '%include /etc/torrc.d/\*.conf$' /etc/tor/torrc ; then
      true "INFO: already includes - %include /etc/torrc.d/*.conf - ok."
   else
      true "INFO: does not include - %include /etc/torrc.d/*.conf - yet."
      echo '\
# The following line has been added by /usr/libexec/helper-scripts/repair-torrc
%include /etc/torrc.d/*.conf' | tee -a /etc/tor/torrc >/dev/null
   fi
else
   true "INFO: file /etc/tor/torrc does not exist yet, creating..."
   echo '\
# The following line has been added by /usr/libexec/helper-scripts/repair-torrc
%include /etc/torrc.d/*.conf' | tee -a /etc/tor/torrc >/dev/null
fi

if grep -q "%include /etc/torrc.d/\$" "/etc/tor/torrc" ; then
   true "INFO: still using old include directive - %include /etc/torrc.d/ - upgrading..."
      search="%include /etc/torrc.d/"
   replace="%include /etc/torrc.d/*.conf"
   file_name="/etc/tor/torrc"
   str_replace "$search" "$replace" "$file_name"
fi

if test -f "$torrcd_path" ; then
   true "INFO: file $torrcd_path already exists, ok."
else
   true "INFO: file $torrcd_path does not exist yet, creating."
   echo "\
# Do not edit this file!
# Please add modifications to the following file instead:
# This file has been created by /usr/libexec/helper-scripts/repair-torrc
%include /usr/local/etc/torrc.d/*.conf" | tee -a "$torrcd_path" >/dev/null
fi

if test -f "$torrc_file_path" ; then
   true "INFO: file $torrc_file_path already exists, ok."
else
   true "INFO: file $torrc_file_path does not exist yet, creating..."
   echo "\
# Do not edit this file!
# This file has been created by /usr/libexec/helper-scripts/repair-torrc
# Please add modifications to the following file instead:
# /usr/local/etc/torrc.d/50_user.conf" | tee "$torrc_file_path" >/dev/null
fi

if test -f "$torrc_user_file_path" ; then
   true "INFO: file $torrc_user_file_path already exits, ok."
else
   true "INFO: file $torrc_user_file_path does not exist yet, creating..."
   echo "\
# Tor user specific configuration file
#
# Add user modifications below this line:
############################################" | tee "$torrc_user_file_path" >/dev/null
fi

true "$0: END"
