#!/bin/bash

## Copyright (C) 2012 - 2020 ENCRYPTED SUPPORT LP <adrelanos@riseup.net>
## See the file COPYING for copying conditions.

## Debugging
#set -x

set -o pipefail

export PATH="$PATH:/usr/lib/whonixsetup_/"

error_handler() {
   local MSG="\
###########################################################
## Something went wrong. Please report this bug!
##
## BASH_COMMAND: $BASH_COMMAND
###########################################################\
"
   echo "$MSG"
   exit 1
}

trap "error_handler" ERR

SCRIPTNAME="$(basename "$BASH_SOURCE")"

whonixsetup_whonix_repository() {
   ## /usr/lib/whonixsetup_cli_start_maybe will not start whonixcheck, if done
   ## file exists. So we won't needlessly always start the whonix_repository tool.

   if [ ! -f "/var/cache/whonix-setup-wizard/status-files/whonix_repository.done" ]; then
      if [ ! -f "/usr/share/whonix-setup-wizard/status-files/whonix_repository.skip" ]; then
         ## Legacy up to Whonix 9.x.
         if [ ! -f "/var/lib/whonix/do_once/whonixsetup.done" ]; then
            if [ -x "$(command -v whonix_repository)" ]; then
               echo "Consider running the Whonix Repository Tool!"
               echo "Otherwise you will not be receiving upgrades from the Whonix team!"
               echo "For example:"
               echo "whonix_repository --enable --repository stable"
               mkdir --parents "/var/cache/whonix-setup-wizard/status-files"
               touch "/var/cache/whonix-setup-wizard/status-files/whonix_repository.done"
            else
               echo "Not starting Whonix Repository Tool, because not installed. Skipping."
            fi
         fi
      fi
   fi
}

whonixsetup_end_success() {
   whonixsetup_whonix_repository
   echo "You can always enter whonixsetup again, by starting: sudo whonixsetup"
   exit 0
}

if [ "$(id -u)" != "0" ]; then
    echo "ERROR: This must be run as root (sudo)!"
    echo "INFO: You can start $SCRIPTNAME by entering..."
    echo "      sudo $SCRIPTNAME"
    exit 1
fi

## sanity tests
## >/dev/null hides stdout, but not stderr,
## i.e. there will be no results from command -v,
## except if there was an error.
command -v ft_disclaimer >/dev/null
command -v ft_main >/dev/null
command -v ft_m_0 >/dev/null
command -v ft_m_1 >/dev/null
command -v ft_m_2 >/dev/null
command -v ft_m_3 >/dev/null
command -v ft_m_4 >/dev/null
command -v ft_m_5 >/dev/null
command -v ft_m_6 >/dev/null
command -v ft_m_7 >/dev/null
command -v ft_m_8 >/dev/null
command -v ft_m_end >/dev/null

command -v dialog >/dev/null
command -v systemctl >/dev/null

if [ -f "/usr/share/anon-gw-base-files/gateway" ]; then
   if [ ! -f "/etc/tor/torrc" ]; then
      error "/etc/tor/torrc does not exist. Please report this bug!"
      exit 1
   fi
fi

if [ ! -f "/var/cache/whonix-setup-wizard/status-files/disclaimer.done" ]; then
   if [ ! -f "/usr/share/whonix-setup-wizard/status-files/disclaimer.skip" ]; then
      exit_code="0"
      ft_disclaimer || { exit_code="$?" ; true; };

      if [ ! "$exit_code" = "0" ]; then
         echo "You can always enter whonixsetup again, by starting: sudo whonixsetup"
         exit 0
      fi
      mkdir --parents "/var/cache/whonix-setup-wizard/status-files"
      touch "/var/cache/whonix-setup-wizard/status-files/disclaimer.done"
   fi
fi

## Start whonixcheck on Whonix-Workstation here.
## (On Whonix-Gateway ft_m_1 will start it.)
if [ -f "/usr/share/anon-ws-base-files/workstation" ]; then
   TITLE="whonixsetup - Success!"

   MSG="
Press Enter to run whonixcheck and exit.
"

   dialog --title "$TITLE" --msgbox "$MSG" 640 480

   ## Start whonixcheck.
   exit_code="0"
   ft_m_end || { exit_code="$?" ; true; };

   whonixsetup_end_success

elif [ -f "/usr/share/anon-gw-base-files/gateway" ]; then

   ## Start Whonix Connection Wizard.
   while true; do
      exit_code="0"
      ft_main || { exit_code="$?" ; true; };

      ## if the cancel button gets pressed, exit_code will be 0

      exit_code_ft_m_x="0"
      "ft_m_$exit_code" || { exit_code_ft_m_x="$?" ; true; };

      if [ ! "$exit_code_ft_m_x" = "0" ]; then
         break
      fi
   done

   whonixsetup_end_success

else

   echo "ERROR: This neither Whonix-Gateway nor Whonix-Workstation detected! Please report this Whonix bug!"
   echo "You can always enter whonixsetup again, by starting: sudo whonixsetup"
   exit 1

fi
