#!/bin/bash

# notify users about more or less critical events. This script only displays non-graphical
# notifications. do_x_notification cares about graphical ones.
# called with one argument (can not be set via configuration ATM), displays the argument.
# called with 2 arguments is a bit magic:
#  - 2nd argument is "ERROR", "WARN" or "INFO": determines the emergency
#  - nothing of the above: current scheme -> display the current powersaved event.
# the 2nd argument handling is a bit ugly. Compatibility....

. "${0%/*}/helper_functions"

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

#######################################################################
# process arguments:
MESSAGE="";LEVEL="INFO"
# invoked from script
if [ -n "$1" -a \( -z "$3" -o "$3" == CONTINUE \) ]; then
    MESSAGE="`echo $1`"
    LEVEL="$2"
fi

# invoked from daemon, e.g. "EVENT_BUTTON_POWER=notify"
if [ -z "$MESSAGE" ]; then
    MESSAGE="Powersaved event: ${EVENT_TYPE}${EVENT_SUBTYPE:+" $EVENT_SUBTYPE"}${EVENT_EXT:+" $EVENT_EXT"}"
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)
	    $SCRIPT_RETURN $EV_ID 2 "$MESSAGE"
            ;;
	notify_console)
	    echo "$MESSAGE" | fmt |wall
	    ;;
	notify_acoustic)
	    ( for y in . . . ; do
		echo -en "\007" > /dev/tty0
		usleep 500000
	    done ) &
	    ;;
        none)   # do nothing
            ;;
	*)
	    DEBUG "Wrong value $x in $SYSCONF_DIR common for variable NOTIFY_METHOD" ERROR
	    ;;
    esac
done

if [ "$3" != CONTINUE ]; then
    $SCRIPT_RETURN $EV_ID 0 "notify finished"
fi
EXIT 0

