#!/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="<settingfile>"
# arg_help is the help displayed at the end of the help.
arg_help="<settingfile>	the xml file to be parsed."

tmpfile=/tmp/settingfile

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

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

# timeout in seconds
timeout=60

# sleeptime in microseconds
#sleeptime=1000000

: "${attr}PROCESSING COMMAND LINE${default}"
############################################
WAIT=0
RET=0
file=""

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

while [ $# -ne 0 ]; do
    case "$1" in 
        -h|--help) : "${attr}help${default}"
            help
            exit $RET
        ;;
        -w|--wait) : "${attr}will wait${default}"
            WAIT=1
        ;;
        *) : "${attr}setting file${default}"
            [[ -n "${file:-}" ]] && RET=1 && help && exit $RET
            file=$1
        ;;
    esac
    shift
done

: "${attr}Check the arguments${default}"
########################################
[ -z "$file" ] && echoerror "No file given" && RET=1 && exit $RE
[ ! -e $file ] && echoerror "$file does not exists" && RET=1 && exit $RET

: "${attr}Pre sending${default}"
################################
toset=$(getnexttmpfile $tmpfile)
ln -s $file $toset

: "${attr}If already running stop here${default}"
#################################################
isRunning $tmpfile
[[ $? -eq 0 ]] && exit 0

: "${attr}Send command${default}"
#################################
send "$WAIT" "${tmpfile:-}" DM_SETTINGS_CHANGED
RET=$?

: "${attr}Post sending${default}"
#################################

exit $RET
