#!/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="<step_number>"
# arg_help is the help displayed at the end of the help.
arg_help="<step_number>	the init step we are entering (between 1 and 5)."

#
# 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
step=""

[[ $# -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
        ;;
        *) : "step"
            [[ -n "${step}" ]] && RET=1 && help && exit $RET
            step=$1
        ;;
    esac
    shift
done

: "Check the arguments"
#######################
[ $step -lt 1 -o $step -gt 5 ] && echoerror "$step is not bound inside 1 and 5" && RET=1 && exit $RET

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

: "Send command"
################
case "$step" in
    1) : "Nothing to do for step 1"
        RET=0
    ;;
    *) : "Step $step, send the message"
        send "$WAIT" "${tmpfile:-}" init_step int32:$step
        RET=$?
    ;;
esac

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

exit $RET
