#!/bin/bash

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

## This file gets sourced by msgdispatcher and msgprogressbar.
## Therefore the error_handler function has been already load.

## style-ok: no-strict
##
## No strict-mode preamble: this file is SOURCED, never executed.

## The dummy yad / kdialog / notify-send below SHADOW the real commands, so
## every later call to those names reaches these bodies.
# shellcheck disable=SC2317

# shellcheck source=../../../../helper-scripts/usr/libexec/helper-scripts/has.sh
source "${HELPER_SCRIPTS_PATH:-}/usr/libexec/helper-scripts/has.sh"

fallbacks() {
   trap "error_handler" ERR

   if [ "${DISPLAY:-}" = "" ] && [ "${WAYLAND_DISPLAY:-}" = "" ]; then
      no_gui="1"
   else
      no_gui="0"
   fi

   ## Check if 'yad' is installed.
   ## - This is not the case for cli users,
   ##   who removed yad or custom builds which never had 'yad' installed.
   if ! has yad || [ "${no_gui}" = "1" ]; then
      ## 'yad' is not installed or no Wayland / X server running.
      yad() {
         ## dummy
         true
      }
   fi

   if ! has kdialog || [ "${no_gui}" = "1" ]; then
      ## kdialog is not installed or no Wayland / X server running.
      kdialog() {
         ## dummy
         true
      }
   fi

   if ! has notify-send || [ "${no_gui}" = "1" ]; then
      ## notify-send is not installed or no Wayland / X server running.
      notify-send() {
         ## dummy
         true
      }
   fi
}
