#! /bin/bash

# chkconfig: 90 20
# description: Provides womonitor service

### BEGIN INIT INFO
# Provides:          womonitor
# Required-Start:    $all
# Required-Stop:
# Default-Start:     3 5 6
# Default-Stop:      0 1 2 6
# Short-Description: Starts the WebObjects Monitor
# Description:       Starts the WebObjects Monitor which is necessary to run
#       WebObjects Applications and control them via multiple hosts.
### END INIT INFO

USER=appserver
NEXT_ROOT="/opt"
export NEXT_ROOT
TIME="[`date +%d.%m.%Y\ %H:%M:%S`]:"

###############################################################################

# Create Logfile if not already exists and setup permissions.
if [ ! -e "/var/log/womonitor.log" ]; then
    touch "/var/log/womonitor.log"
    chown $USER "/var/log/womonitor.log"
    chgrp root "/var/log/womonitor.log"
    chmod 640 "/var/log/womonitor.log"
fi

# Create Logroate Job if available.
if [ -e "/etc/logrotate.d" ] && [ ! -e "/etc/logrotate.d/womonitor" ]; then
    echo '/var/log/womonitor.log {' >> /etc/logrotate.d/womonitor
    echo '    daily'                >> /etc/logrotate.d/womonitor
    echo '    rotate 5'             >> /etc/logrotate.d/womonitor
    echo '    compress'             >> /etc/logrotate.d/womonitor
    echo '    delaycompress'        >> /etc/logrotate.d/womonitor
    echo '    missingok'            >> /etc/logrotate.d/womonitor
    echo '    notifempty'           >> /etc/logrotate.d/womonitor
    echo '}'                        >> /etc/logrotate.d/womonitor
fi

###############################################################################

# See how we were called.
case "$1" in
    status)
        WOMONITOR_PID=`ps aux | awk '/WOPort 56789/ && !/awk/ {print $2}'`
        if [ "$WOMONITOR_PID" != "" ]; then
            echo "$TIME Service is running with PID $WOMONITOR_PID" 2>&1 | tee -a /var/log/womonitor.log
        else
            echo "$TIME Service is not running" 2>&1 | tee -a /var/log/womonitor.log
        fi
        ;;
    start)
        echo "$TIME Starting womonitor: " 2>&1 | tee -a /var/log/womonitor.log
        su $USER -c -l "$NEXT_ROOT/Local/Library/WebObjects/JavaApplications/JavaMonitor.woa/JavaMonitor -WOPort 56789 2>&1 | tee -ai /var/log/womonitor.log &"
        ;;
    stop)
        echo "$TIME Shutting down womonitor: " 2>&1 | tee -a /var/log/womonitor.log
        WOMONITOR_PID=`ps aux | awk '/WOPort 56789/ && !/awk/ {print $2}'`
        kill $WOMONITOR_PID
        ;;
    restart)
        $0 stop
        $0 start
        ;;
esac

if [ $# -gt 1 ]; then
    shift
    $0 $*
fi

exit 0