#!/bin/sh

# HISTORY
#
#
##################################################
. /etc/init.d/rc.config
SEND_INFRA=/usr/bin/send_infra
JPG2FB=/usr/sbin/jpg2fb
timeout=0
level=7
buttons=""
uniqid="chpup`date "+%Y%m%d%H%M%S"`$$"
idgiven="no"
dokill="no"

usage() {
    echo "usage : displaypopup [-i <id>] [-k | [-b <buttons>] [-l<level>] [-t<timeout>]] <message>"
    echo "        buttons can be in Ok,Cancel,Yes,No  (comma separated)"
    echo "        timeout in seconds"
    echo "        level can be a number or in none,debug,information,notice,"
    echo "                      warning,error,fatalerror"
    echo "        id is the unique identifier of the popup."
    echo "        -k kills the unique id given"

}

OPTS=`getopt -o "b:hi:kl:t:" -- "$@"`
eval set -- "$OPTS"
while true ; do
    case "$1" in 
        -b) buttons=$2; shift 2;;
        -l) level=$2; shift 2;;
        -t) timeout=$2; shift 2;;
        -h) usage; exit;;
        -i) uniqid=$2; idgiven="yes"; shift 2;;
        -k) dokill="yes"; shift;;
        --)shift; break;;
    esac
done
case $level in
	debug) level=0;;
	information) level=1;;
	notice) level=2;;
	warning) level=3;;
	error) level=4;;
	fatalerror) level=5;;
	none) level=6;;
esac
timeout=`expr $timeout \* 1000`
ENDPARAMS="$*"
events=""
if [ "$buttons" != "" ]; then
    events=`echo $buttons | sed -e 's/Ok/Ok:oknone/' -e 's/Cancel/Cancel:cannone/' -e 's/Yes/Yes:yesnone/' -e 's/No/No:nonone/' `
fi


if [[ -f $applicationManagerPid ]] && [[ -d /proc/$(cat $applicationManagerPid) ]]; then
	# application manager running
	if [ "$dokill" = "no" ]; then
            if [ "$idgiven" = "no" ]; then
	        echo popupid is $uniqid
            fi
	    PARAMS="id=${uniqid}"
	    PARAMS=${PARAMS}";"int:level=${level}
	    PARAMS=${PARAMS}";"string:text="$ENDPARAMS"
	    if [ $timeout -ne 0 ]; then
	        PARAMS=${PARAMS}";"int:timeout=${timeout}
                if [ "$events" != "" ]; then
	            events=${events}","
	        fi
	        events=${events}"timeout:none"
	    fi
	    if [ "$events" != "" ]; then
                PARAMS=${PARAMS}";"string:events="{"${events}"}"
	    fi
	    ###echo $SEND_INFRA generic_event SHOW_POPUP_EVENT string:"$PARAMS"
	    $SEND_INFRA generic_event SHOW_POPUP_EVENT string:"$PARAMS"
	else
	    PARAMS="id=${uniqid}"
	    $SEND_INFRA generic_event HIDE_POPUP_EVENT string:"$PARAMS"
	fi
else
    /usr/sbin/displaymsg "$ENDPARAMS"
fi
