#!/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....

. "/usr/lib/powersave/scripts/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. "POWERSAVED_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 $POWERSAVE_NOTIFY_METHOD; do
    [ -x $ZENITY_BIN ] && ZENITY=true || ZENITY=false
    case "$x" in
	notify_popup_window|notify_popup_fallback)
	    $SCRIPT_RETURN "$4|2|$MESSAGE"
            ;;
	notify_console)
	    echo "$MESSAGE" | fmt |wall
	    ;;
	notify_acoustic)
	    ( for y in . . . ; do
		echo -en "\007" > /dev/tty0
		usleep 500000
	    done ) &
	    ;;
	*)
	    DEBUG "Wrong value $x in $POWERSAVE_SYSCONF_DIR common for variable POWERSAVE_NOTIFY_METHOD" ERROR
	    ;;
    esac
done

if [ "$3" != CONTINUE ]; then
    $SCRIPT_RETURN "$4|0|notify finished"
fi
exit 0

