#!/bin/bash

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

## Debugging
#set -x

set -o errexit
set -o nounset
set -o pipefail
set -o errtrace
shopt -s inherit_errexit
shopt -s shift_verbose

export PATH="${PATH}:/usr/libexec/setup-dist/"

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

trap "error_handler" ERR

SCRIPTNAME="$(basename -- "${BASH_SOURCE[0]}")"

run_disclaimer_maybe() {
   if [ -f "/var/cache/setup-dist/status-files/disclaimer.done" ]; then
      return 0
   fi
   if [ -f "/usr/share/setup-dist/status-files/disclaimer.skip" ]; then
      return 0
   fi
   if [ -f "/var/cache/whonix-setup-wizard/status-files/disclaimer.done" ]; then
      return 0
   fi
   if [ -f "/usr/share/whonix-setup-wizard/status-files/disclaimer.skip" ]; then
      return 0
   fi

   ## Disable disclaimer. Everything below is unreachable and kept as
   ## documentation of what the disclaimer flow did, exactly like the
   ## short-circuit in developer-meta-files' dev_clearnet.
   return 0

   # shellcheck disable=SC2317
   exit_code="0"
   # shellcheck disable=SC2317
   ft_disclaimer || { exit_code="$?" ; true; };

   # shellcheck disable=SC2317
   if [ ! "${exit_code}" = "0" ]; then
      printf '%s\n' "You can always enter setup-dist again, by starting: setup-dist"
      exit 0
   fi

   # shellcheck disable=SC2317
   mkdir --parents -- "/var/cache/setup-dist/status-files"
   # shellcheck disable=SC2317
   touch -- "/var/cache/setup-dist/status-files/disclaimer.done"
}

check_passwords() {
   local output
   output="$(leaprun get-password-status-list)"
   if printf '%s' "${output}" | grep --quiet --ignore-case -- 'Absent'; then
      ## yellow/nocolor come from get_colors.sh, sourced below. shellcheck
      ## cannot follow that file: it lives in the helper-scripts package, and CI
      ## checks out this repo alone, so the relative source= path resolves to
      ## nothing there.
      # shellcheck disable=SC2154
      printf '%b\n' "[${yellow}NOTICE${nocolor}] Some user accounts on this system are passwordless. Run 'systemcheck' for more information."
   fi
}

start_systemcheck() {
   TITLE="setup-dist - Success!"
   MSG="
Press Enter to run systemcheck and exit.
"
   dialog_wrapper --title "${TITLE}" --msgbox "${MSG}" 640 480
   ## Start systemcheck.
   exit_code="0"
   ft_m_end || { exit_code="$?" ; true; };
}

if [ "$(id -u)" = "0" ]; then
    printf '%s\n' "ERROR: This must NOT be run as root (sudo)!"
    printf '%s\n' "INFO: You can start ${SCRIPTNAME} by entering..."
    printf '%s\n' "      ${SCRIPTNAME}"
    exit 1
fi

# shellcheck source=../libexec/setup-dist/shared
# shellcheck disable=SC1091
source "/usr/libexec/setup-dist/shared"
## Provides yellow/nocolor, used by check_passwords below. Sourced BEFORE any
## use of them -- under nounset an unset colour variable is a hard abort, not a
## missing escape sequence.
# shellcheck source=../../../helper-scripts/usr/libexec/helper-scripts/get_colors.sh
# shellcheck disable=SC1091
source "/usr/libexec/helper-scripts/get_colors.sh"
# shellcheck source=../../../helper-scripts/usr/libexec/helper-scripts/has.sh
# shellcheck disable=SC1091
source "/usr/libexec/helper-scripts/has.sh"

has dialog
has dialog_wrapper

run_disclaimer_maybe
check_passwords

if [ -f "/usr/share/anon-ws-base-files/workstation" ]; then
   start_systemcheck
elif [ -f "/usr/share/anon-gw-base-files/gateway" ]; then
   start_systemcheck
fi
