#!/bin/sh

#
# CONFIG VARIABLES
##################
HELPERPATH=/usr/lib/send_infra

# arg_summary is the summary on the command line except for the "-w|--wait" option
arg_summary="<event> [[[string|int]:<arg>][;[string|int]<arg>]...]"
# arg_help is the help displayed at the end of the help.
arg_help="<message>	the message we want to display.\n<arg>		the type has to be either string or int."

#
# FUNCTION DEFINITION
#####################
source $HELPERPATH/.functions

############
### MAIN ###
############
: "Overwrite configuration varibles"
####################################
# sendAndWait will send a message and check the existence of the file each $sleeptime and at most for $timeout microseconds

# timeout in seconds
#timeout=5

# sleeptime in microseconds
#sleeptime=1000000

: "PROCESSING COMMAND LINE"
###########################
WAIT=0
RET=0
eventname=""
unset eventargs

[[ $# -eq 0 ]] && RET=1 && set -- -h

while [ $# -ne 0 ]; do
    case "$1" in 
        -h|--help) : "help"
            help
            exit $RET
        ;;
        -w|--wait) : "will wait"
            WAIT=1
        ;;
      	*) : "event name/arg"
      	    if [[ -z "$eventname" ]]; then
      	    	: "event name"
      	    	eventname=$1
      	    else
      	    	eventargs="${eventargs:+$eventargs;}$1"
      	    fi
        ;;
    esac
    shift
done

: "Check the arguments"
#######################
[ -z "$eventname" ] && echoerror "No event to send !!!" && RET=1 && exit $RET

: "Presending"
##############
# As we does not wait for the exit we overwite it here
WAIT=0

: "Send command"
################
send "$WAIT" "${tmpfile:-}" event string:$eventname ${eventargs:+string:"$eventargs"}
RET=$?

: "Post sending"
################

exit $RET
