#!/bin/bash

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

# shellcheck source-path=SCRIPTDIR

## 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]}")"

# shellcheck disable=SC2317
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.
   return 0

   exit_code="0"
   ft_disclaimer || { exit_code="$?" ; true; };

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

   mkdir --parents -- "/var/cache/setup-dist/status-files"
   touch -- "/var/cache/setup-dist/status-files/disclaimer.done"
}

check_passwords() {
   local output
   output="$(leaprun get-password-status-list)"
   if printf '%s' "${output}" | grep --ignore-case -- 'Absent' >/dev/null 2>&1; then
      ## FIXME: This is throwing a shellcheck error in CI, we need to make sure
      ## helper-scripts is available when we run shellcheck.
      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
source "/usr/libexec/setup-dist/shared"
# shellcheck source=../../../helper-scripts/usr/libexec/helper-scripts/get_colors.sh
source "/usr/libexec/helper-scripts/get_colors.sh"
# shellcheck source=../../../helper-scripts/usr/libexec/helper-scripts/has.bsh
source "/usr/libexec/helper-scripts/has.bsh"

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
