#!/bin/bash

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

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

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

trap "error_handler" ERR

maybe_start_systemcheck() {
   systemcheck_bin="systemcheck"
   systemcheck_bin_command_v_exit_code="0"
   systemcheck_bin_path="$(command -v "$systemcheck_bin" 2>/dev/null)" || { systemcheck_bin_command_v_exit_code="$?" ; true; };

   if [ "$started_by_setup_wizard_dist" = "true" ]; then
      ## '--cli' optional.
      gui_option_maybe="--gui --cli"
   else
      if ! tty --quiet ; then
         echo "$0: INFO: Skipping starting $systemcheck_bin, because started non-interactive."
         echo "$0: INFO: Running $systemcheck_bin recommended!"
         return 0
      fi
   fi

   if [ ! "$systemcheck_bin_command_v_exit_code" = "0" ]; then
      echo "$0: INFO: Skipping starting $systemcheck_bin, because $systemcheck_bin is unavailable."
      return 0
   fi
   if [ ! -x "$systemcheck_bin_path" ]; then
      echo "$0: INFO: Skipping starting $systemcheck_bin, because $systemcheck_bin_path is not executable."
      return 0
   fi
   echo "$0: INFO: Executing: $systemcheck_bin $gui_option_maybe"

   exit_code="0"
   "$systemcheck_bin" $gui_option_maybe || { exit_code="$?" ; true; };

   echo "$0: INFO: End of $systemcheck_bin. You can always start $systemcheck_bin again with:
    $systemcheck_bin"
}

#sudo --non-interactive /usr/libexec/setup-dist/setup-dist-done
leaprun setup-dist-done

maybe_start_systemcheck

exit "$exit_code"
