#!/bin/bash

## Copyright (C) 2012 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

#### meta start
#### project Whonix
#### category networking
#### workstation_only yes
#### description
## <code>socat-unix-sockets</code> starter.
#### meta end

set -e

## Provides variable $GATEWAY_IP so it can be used by `source`d config snippets.
eval $(/usr/libexec/helper-scripts/settings_echo)

## Process configuration folders. Allows overwriting GATEWAY_IP.

shopt -s nullglob
for i in /etc/anon-ws-disable-stacked-tor.d/*.conf /rw/anon-ws-disable-stacked-tor.d/*.conf /usr/local/etc/anon-ws-disable-stacked-tor.d/*.conf; do
   bash_n_exit_code="0"
   bash_n_output="$(bash -n "$i" 2>&1)" || { bash_n_exit_code="$?" ; true; };
   if [ ! "$bash_n_exit_code" = "0" ]; then
      echo "Invalid config file: $i
bash_n_exit_code: $bash_n_exit_code
bash_n_output:
$bash_n_output" >&2
      exit 1
   fi
   source "$i"
done

## '|| true' in case no child process is run.
pid_list="$(pgrep -P "$$")" || true

if [ "$pid_list" = "" ]; then
   echo "INFO: No processes launched into background, ok."
   exit 125
fi

## Relying on systemd KillMode=control-group to kill the child processes as
## well as this script.
wait $pid_list
