#!/bin/bash

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

## Gets started by '/etc/profile.d/30_setup.sh'.

## Using this script and not directly using '/etc/profile.d/30_setup.sh',
## because '/etc/profile.d/30_setup.sh' gets `source`d, not executed, by
## '/etc/profile'. We would have to use "return". "exit" would not be
## available (without stopping '/etc/profile' to source the rest of the
## '/etc/profile.d' folder). Using this script has also the advantage,
## that /bin/bash's features can be used.

#set -x
set -e
set -o errtrace
set -o pipefail

source "/usr/libexec/helper-scripts/get_colors.sh"

error_handler_setup() {
   echo "[${red}${bold}ERROR${nocolor}] [setup-dist] \
ret: $? | BASH_COMMAND: $BASH_COMMAND | Please report this bug!"
}

trap "error_handler_setup" ERR

sigterm_setup() {
   #echo "[${green}INFO${nocolor}] [setup-dist] SIGTERM received!"
   true "INFO: [setup-dist] SIGTERM received!"
}

trap "sigterm_setup" SIGTERM
trap "sigterm_setup" SIGINT

maybe_start_setup() {
   if [ -e "/run/rads/start_dm.status" ]; then
      true "[${green}INFO${nocolor}] [setup-dist] Not starting setup-dist in \
terminal, because rads reports, that X is going to be started. (Autostarting \
setup-dist in X is handled by: \
/etc/xdg/autostart/setup-wizard-dist.desktop )"
      exit 0
   elif [ -e "/run/rads/no_start_dm.status" ]; then
      true "[${green}INFO${nocolor}] [setup-dist] rads appears to be \
installed. /run/rads/no_start_dm.status exists."
   else
      true "[${green}INFO${nocolor}] [setup-dist] rads appears not to be \
in use."
   fi

   if [ -f "/usr/share/setup-dist/status-files/setup-dist.skip" ]; then
      true "[${green}INFO${nocolor}] [setup-dist] skip file \
/usr/share/setup-dist/status-files/setup-dist.skip exists. Exiting."
      exit 0
   fi

   if [ -f "/usr/share/whonix-setup-wizard/status-files/whonixsetup.skip" ]; then
      true "[${green}INFO${nocolor}] [setup-dist] skip file \
/usr/share/whonix-setup-wizard/status-files/whonixsetup.skip exists. Exiting."
      exit 0
   fi

   if [ -f "/usr/share/qubes/marker-vm" ]; then
      true "[${green}INFO${nocolor}] [setup-dist] skip file \
/usr/share/qubes/marker-vm exists. Exiting."
      exit 0
   fi

   ## Now, the script is either:
   ## - Running on Whonix-Gateway, where Tor has been previously already
   ##   enabled (custom builds or derivatives).
   ## - Running on Whonix-Workstation.

   if [ -f "/var/cache/setup-dist/status-files/setup-dist.done" ]; then
      true "[${green}INFO${nocolor}] [setup-dist] done file \
/var/cache/setup-dist/status-files/setup-dist.done exists. Exiting."
      exit 0
   fi
   if [ -f "/var/cache/whonix-setup-wizard/status-files/whonixsetup.done" ]; then
      true "[${green}INFO${nocolor}] [setup-dist] done file \
/var/cache/whonix-setup-wizard/status-files/whonixsetup.done exists. Exiting."
      exit 0
   fi
   ## Legacy up to build version 9.x.
   if [ -f "/var/lib/whonix/do_once/whonixsetup.done" ]; then
      true "[${green}INFO${nocolor}] [setup-dist] legacy done file \
/var/lib/whonix/do_once/whonixsetup.done exists. Exiting."
      exit 0
   fi

   sleep 5 &
   wait "$!" || true

   echo "[${green}INFO${nocolor}] [setup-dist] Starting setup-dist..."

   sleep 5 &
   wait "$!" || true

   setup-dist
}

maybe_start_setup
