#!/bin/bash

## Copyright (C) 2018 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## Copyright (C) 2018 Iry Koon <iry@riseup.net>
## See the file COPYING for copying conditions.

#### meta start
#### project Whonix
#### category tor
#### gateway_only yes
#### description
## helper script to create folders
##
## * <code>/etc/tor</code>
## * <code>/etc/torrc.d/code>
## * <code>/usr/local/etc/torrc.d</code>
##
## and make sure files exist
##
## * <code>/etc/tor/torrc</code>
## * <code>/etc/torrc.d/95_whonix.conf</code>
## * <code>/usr/local/etc/torrc.d/40_tor_control_panel.conf</code>
## * <code>/usr/share/anon-gw-anonymizer-config/user_torrc_template.conf /usr/local/etc/torrc.d/50_user.conf</code>
##
## create folder <code>/var/lib/tor/authdir</code>
##
## with correct access rights from template folder <code>/usr/share/anon-gw-anonymizer-config</code>.
##
## Guarantee the existence of:
## /etc/tor/torrc
## /etc/torrc.d/95_whonix.conf
## /usr/local/etc/torrc.d/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
#### meta end

set -x
set -e

true "$0: START"

if [ ! -d /etc/tor ]; then
   /usr/bin/install -Z -m 00755 -o root -g root -d /etc/tor
fi

if [ ! -d /etc/torrc.d ]; then
   /usr/bin/install -Z -m 00755 -o root -g root -d /etc/torrc.d
fi

if [ ! -d /usr/local/etc/torrc.d ]; then
   /usr/bin/install -Z -m 00755 -o root -g root -d /usr/local/etc/torrc.d
fi

## files:

if test -f /usr/share/anon-gw-base-files/gateway ; then
  if [ ! -e /etc/tor/torrc ]; then
    /usr/bin/install -Z -m 00644 -o root -g root -T /usr/share/anon-gw-anonymizer-config/etc_tor_torrc_template.conf /etc/tor/torrc
  fi
  if [ ! -e /etc/torrc.d/95_whonix.conf ]; then
    /usr/bin/install -Z -m 00644 -o root -g root -T /usr/share/anon-gw-anonymizer-config/etc_torrc_d_template.conf /etc/torrc.d/95_whonix.conf
  fi
  if [ ! -e /usr/local/etc/torrc.d/40_tor_control_panel.conf ]; then
    /usr/bin/install -Z -m 00644 -o root -g root -T /usr/share/anon-gw-anonymizer-config/tor_control_panel_torrc_template.conf /usr/local/etc/torrc.d/40_tor_control_panel.conf
  fi
  if [ ! -e /usr/local/etc/torrc.d/50_user.conf ]; then
    /usr/bin/install -Z -m 00644 -o root -g root -T /usr/share/anon-gw-anonymizer-config/user_torrc_template.conf /usr/local/etc/torrc.d/50_user.conf
  fi
fi

## /etc/torrc.d/65_gateway.conf
## ClientOnionAuthDir /var/lib/tor/authdir
[ -n "$tor_user" ] || tor_user="debian-tor"
[ -n "$tor_group" ] || tor_group="debian-tor"
[ -n "$tor_dir" ] || tor_dir="/var/lib/tor"
[ -n "$client_onion_auth_dir" ] || client_onion_auth_dir="${tor_dir}/authdir"

if ! test -d "$client_onion_auth_dir"; then
   mkdir --parents -- "$client_onion_auth_dir"
   chmod 0700 -- "$client_onion_auth_dir"
fi

chown "${tor_user}:${tor_group}" -- "$client_onion_auth_dir"


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 --quiet -- '%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."
      printf '%s\n' "\
# 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..."
   printf '%s\n' "\
# 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 test -f "$torrcd_path" ; then
   true "INFO: file $torrcd_path already exists, ok."
else
   true "INFO: file $torrcd_path does not exist yet, creating."
   printf '%s\n' "\
# 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..."
   printf '%s\n' "\
# 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..."
   printf '%s\n' "\
# Tor user specific configuration file
#
# Add user modifications below this line:
############################################" | tee -- "$torrc_user_file_path" >/dev/null
fi

true "$0: END"
