#!/usr/bin/bash
#set -x

# startup [ -e <property=value> ] [ -f ] [ -q ] <server process>
# shutdown [ -q ] [ -f ] <running server process> ...
# status [ -q ] [ -verbose ] <server process> ...

rc=99

while [[ "$#" -gt 0 ]]; do
    case "$1" in
        startup | shutdown | status ) action="$1" ;;
        -f ) # just be able to ingnore it 
                true
                ;;
        -q ) # just be able to ingnore it
                true
                ;;
        -verbose ) # just be able to ingnore it
                true
                ;;
        * ) comp="$1" ;;
    esac
    shift
done

lid="$$"

services="platform ui"

logger --id "$lid" -t "mzsh" "mzsh version 0.3.2 - action=$action"
logger --id "$lid" -t "mzsh" "MZSH=$0 JAVA_HOME=$JAVA_HOME MZ_HOME=$MZ_HOME MZ_PLATFORM=$MZ_PLATFORM"
logger --id "$lid" -t "mzsh" "MZSH=$0 RA_JAVA_HOME=$JAVA_HOME RA_MZ_HOME=$MZ_HOME RA_MZ_PLATFORM=$MZ_PLATFORM"

file_of_fail_base="/dev/shm/mzsh_fail_"
file_of_status="/dev/shm/mzsh_status_"

# action could be status, startup or shutdown
if [ -e "${file_of_fail_base}${action}" ]; then
    is_fail="yes"
    logger --id -t "mzsh" "mzsh action=$action file_of_fail found :) (removing)"
    rm "${file_of_fail_base}${action}"
else
    is_fail="no"
fi 

case "$action" in
    status )  
            # TODO: also simulate answer like '<component> is running without'
            # TODO: rc-handling (still to be confirmed)
            # TODO: also take component as optional parameter (still to be confirmed)
            # TODO: simulate already running: '<component> already running' (format still to be confirmed)
            logger --id "$lid" -t "mzsh" "mzsh process action=$action comp=$comp"
            if [[ "$is_fail" == "yes" ]]; then
                sleep 60
                rc=2
            else
                if [[ -n "$comp" ]]; then
                        if [ -e "${file_of_status}${comp}" ]; then
                            cat "${file_of_status}${comp}"
                            if grep -q "$comp is running"  "${file_of_status}${comp}"; then
                                rc=0
                            else
                                rc=2
                            fi
                        else
                            echo "$comp is not running"
                            rc=2
                        fi
                else
                    for srv in $services; do
                        if [ -e "${file_of_status}${srv}" ]; then
                            cat "${file_of_status}${srv}"
                            if grep -q "$comp is running"  "${file_of_status}${srv}"; then
                                rc=0
                            else
                                rc=2
                            fi
                        else
                            echo "$srv is not running"
                            rc=2
                        fi
                    done
                fi
            fi
            ;;
    startup )
            # TODO: number of dots in 'starting %s...' can vary
            # TODO: simulate FAILURES (format of different output strings still to be confirmed)
            # TODO: rc-handling (still to be confirmed)
            logger --id "$lid" -t "mzsh" "mzsh process action=$action component=$comp"
            if [[ "$is_fail" == "no" ]]; then
                printf "Starting %s..." "$comp"
                sleep 10
                echo "done."
                echo "$comp is running" > "${file_of_status}${comp}"
                rc=0
            else
                printf "Starting %s..." "$comp"
                sleep 30
                rc=1
            fi
            ;;
    shutdown )
            # TODO: siluate differnt output if compunent is already down: '<component> is not running' (format still to be confirmed)
            # TODO: simulate stop with escalation to kill (was a multi line output seen on our test cluster; format still to be confirmed)
            #       something like:
            #       Shutting down %s...
            #       Escalated to call kill
            #       done.
            # TODO: rc-handling (still to be confirmed)
            logger --id "$lid" -t "mzsh" "mzsh process action=$action component=$comp"
            if [[ "$is_fail" == "no" ]]; then
                printf "Shutting down %s..." "$comp"
                sleep 5
                echo "done."
                echo "$comp is not running" > "${file_of_status}${comp}"
                rc=0
            else
                printf "Shutting down %s..." "$comp"
                sleep 30
                rc=1
            fi
            ;;
esac
# TODO: simulation of restart
# TODO: simulation of 'kill'


# meaningfull  rc
exit "$rc"
