#!/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="<webapp>"
# arg_help is the help displayed at the end of the help.
arg_help="<webapp>	webapp to show."

tmpfile=/tmp/show_webapp

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

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

: "Check the arguments"
#######################

: "Presending"
##############
toset=$(getnexttmpfile $tmpfile)
echo "$webapp" > $toset

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

: "Send command"
################
send "$WAIT" "${tmpfile:-}" show_webapp string:Settings
RET=$?

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

exit $RET
