#!/bin/bash

# notify users about more or less critical events with a popup. Only called from daemon
# called with one argument, displays the argument.

. "${0%/*}/scripts/helper_functions"
. "${SCRIPT_DIR}/scripts/x_helper_functions"

# this script does not use $SCRIPT_RETURN, so the exit trap is not needed.
trap '' EXIT

# we already log this at DIAG level in helper_functions
DEBUG "process script: do_x_notification" INFO

#######################################################################
# process arguments:
MESSAGE="$1";LEVEL="WARN"

if [ -z "$MESSAGE" ]; then
    MESSAGE="A powersave error occurred. For more information, use a graphical frontend like kpowersave."
fi

for x in ${NOTIFY_METHOD:-notify_popup_fallback notify_acoustic}; do
    [ -x $ZENITY_BIN ] && ZENITY=true || ZENITY=false
    case "$x" in
	notify_popup_window|notify_popup_fallback)
	    get_x_users
            DONE=false
            i=0
            while [ "${X_USERS[$i]}" ]; do
                X_USER=${X_USERS[$i]}; DISP=${DISPS[$i]}; KDE_RUNNING=${KDE_RUNNING[$i]:-false};
		# open the appropriate popup (x_helper_functions)
		choose_popup "$MESSAGE" "$LEVEL"
		[ $? -eq 0 ] && DONE=true
		let i++
            done
	    if ! $DONE && [ "$x" = "notify_popup_fallback" ]; then
		echo "$MESSAGE" | fmt | wall
	    fi
            ;;
	notify_console) ;;
	notify_acoustic) ;;
	*)
	    DEBUG "Wrong value $x in $SYSCONF_DIR common for variable NOTIFY_METHOD" ERROR
	    ;;
    esac
done

exit 0

