#!/bin/sh

PIDLIST=`ps wwwfax | grep 'AppServer' | grep -v grep | cut -c1-5`

WEBWARE=/usr/local/www/webkit
PYTHON=/usr/libexec/isilon/isi_webui_d
PYOPTS=""

PROCESS_LIMIT_KB="262144"

ERROR=0
RUNNING=0

ARGV="$@"
if [ "x$ARGV" = "x" ] ; then 
    ARGS="help"
fi

for ARG in $@ $ARGS
do

    # check for pidlist
    if [ "x$PIDLIST" != "x" ]; then
        RUNNING=1
    else
        RUNNING=0
    fi

    case $ARG in
    debug)
        continue
        ;;

    start)
        if [ $RUNNING -eq 1 ]; then
            echo "$0 $ARG: webware ($PIDLIST) already running"
            continue
        fi

        ulimit -m $PROCESS_LIMIT_KB

        cd $WEBWARE && $PYTHON $PYOPTS ./Launch.py ThreadedAppServer -i /var/run/webui_webware.pid -d /usr/local/www/webkit -o /var/log/webui_webware.log &

        sleep 2
        PIDLIST=`ps wwwfax | grep 'AppServer' | grep -v grep | cut -c1-5`
        if [ "x$PIDLIST" != "x" ]; then
            echo "$0 $arg: webware ($PIDLIST) started"
            RUNNING=1
        else
            echo "$0 $arg: webware failed to start"
            RUNNING=0
        ERROR=1
        fi
        ;;

    stop)
        if [ $RUNNING -eq 0 ]; then
            echo "$0 $ARG: webware not running"
            continue
        fi

        if kill $PIDLIST ; then
            echo "$0 $ARG: webware ($PIDLIST) stopped"
        else
            echo "$0 $ARG: webware could not be stopped"
            ERROR=4
        fi
        ;;

    restart)
        if [ $RUNNING -eq 1 ]; then
            if kill -9 $PIDLIST ; then
                echo "$0 $ARG: webware ($PIDLIST) stopped"
                RUNNING=0
                sleep 1

            else
                echo "$0 $ARG: webware ($PIDLIST) could not be stopped"
                ERROR=4
                break
            fi
        fi

        if [ $RUNNING -eq 1 ]; then
            echo "$0 $ARG: webware ($PIDLIST) already running"
            continue
        fi

        ulimit -m $PROCESS_LIMIT_KB

        cd $WEBWARE && $PYTHON $PYOPTS ./Launch.py ThreadedAppServer -i /var/run/webui_webware.pid -d /usr/local/www/webkit -o /var/log/webui_webware.log &

        sleep 2
        PIDLIST=`ps wwwfax | grep 'AppServer' | grep -v grep | cut -c1-5`
        if [ "x$PIDLIST" != "x" ]; then
            echo "$0 $ARG: webware ($PIDLIST) restarted"
            RUNNING=1
        else
            echo "$0 $ARG: webware failed to start"
            RUNNING=0
        ERROR=1
        fi
        ;;

    allstop)
        if [ $RUNNING -eq 0 ]; then
            echo "$0 $ARG: webware not running"
            continue
        fi

        if kill -9 $PIDLIST ; then
            echo "$0 $ARG: webware stopped"
        else
            echo "$0 $ARG: webware could not be stopped"
            ERROR=4
        fi
        ;;

    *)
        echo "usage: $0 (start|stop|help)"
        cat <<EOF

start      - start webware
stop       - stop webware.  Store session data.
allstop    - stop webware hard.  Invalidates sessions.
restart   -  restart webware; effectively stop then start.
help       - this screen

EOF
        ERROR=2
        ;;
    esac
done

exit $ERROR
