#!/bin/bash

### BEGIN INIT INFO
# Provides:          cloud usage
# Required-Start:    $network $local_fs
# Required-Stop:     $network $local_fs
# Default-Start:     3 4 5
# Default-Stop:      0 1 2 6
# Short-Description: Start/stop Apache CloudStack Usage Monitor
# Description: This scripts Starts/Stops the Apache CloudStack Usage Monitor
##  The CloudStack Usage Monitor is a part of the Apache CloudStack project and is used
##  for storing usage statistics from instances.
## JSVC (Java daemonizing) is used for starting and stopping the usage monitor.
### END INIT INFO

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

. /etc/rc.d/init.d/functions

SHORTNAME="cloudstack-usage"
PIDFILE=/var/run/"$SHORTNAME".pid
LOCKFILE=/var/lock/subsys/"$SHORTNAME"
LOGDIR=/var/log/cloudstack/usage
LOGFILE=${LOGDIR}/usage.log
PROGNAME="CloudStack Usage Monitor"
CLASS="com.cloud.usage.UsageServer"
PROG="jsvc"
DAEMON="/usr/bin/jsvc"
USER=cloud

unset OPTIONS
[ -r /etc/sysconfig/default/"$SHORTNAME" ] && source /etc/sysconfig/default/"$SHORTNAME"

# The first existing directory is used for JAVA_HOME (if JAVA_HOME is not defined in $DEFAULT)
JDK_DIRS="/usr/lib/jvm/java-7-openjdk /usr/lib/jvm/java-7-openjdk-i386 /usr/lib/jvm/java-7-openjdk-amd64 /usr/lib/jvm/java-7-sun /usr/lib/jvm/jre-1.7.0 /usr/lib/j2sdk1.5-sun /usr/lib/jre-openjdk"

for jdir in $JDK_DIRS; do
    if [ -r "$jdir/bin/java" -a -z "${JAVA_HOME}" ]; then
        JAVA_HOME="$jdir"
    fi
done
export JAVA_HOME

SCP=""
DCP=""
UCP=`ls /usr/share/cloudstack-usage/cloud-usage-*.jar`":"`ls /usr/share/cloudstack-usage/lib/*.jar | tr '\n' ':'`
JCP="/usr/share/java/commons-daemon.jar":"/usr/share/java/mysql-connector-java.jar"

# We need to append the JSVC daemon  and mysql-connector JAR to the classpath
# AgentShell implements the JSVC daemon methods
export CLASSPATH="$SCP:$DCP:$UCP:$JCP:/etc/cloudstack/usage"

start() {
    if [ -s "$PIDFILE" ] && kill -0 $(cat "$PIDFILE") >/dev/null 2>&1; then
        echo "$PROGNAME apparently already running"
        exit 0
    fi

    if hostname --fqdn >/dev/null 2>&1 ; then
        true
    else
        echo "The host name does not resolve properly to an IP address. Cannot start $PROGNAME"
        exit 1
    fi

    echo -n "Starting $PROGNAME" "$SHORTNAME"

    if daemon --pidfile $PIDFILE $DAEMON -cp "$CLASSPATH" -pidfile "$PIDFILE" -user "$USER" \
      -errfile $LOGDIR/cloudstack-usage.err -outfile $LOGDIR/cloudstack-usage.out -Dpid=$$ $CLASS
        RETVAL=$?
    then
        rc=0
        sleep 1
        if ! kill -0 $(cat "$PIDFILE") >/dev/null 2>&1; then
            failure
            rc=1
        fi
    else
        rc=1
    fi

    if [ $rc -eq 0 ]; then
        success
    else
        failure
        rm -f "$PIDFILE"
    fi
    echo
}

stop() {
    echo -n "Stopping $PROGNAME" "$SHORTNAME"
    killproc -p $PIDFILE $DAEMON
    if [ "$?" -eq 0 ]; then
        success
    else
        failure
    fi
    rm -f "$PIDFILE"
    echo
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    status)
        status -p $PIDFILE $SHORTNAME
        RETVAL=$?
        ;;
    restart | force-reload)
        stop
        sleep 3
        start
        ;;
    *)
    echo "Usage: $0 {start|stop|restart|force-reload|status}"
    RETVAL=3
esac

exit $RETVAL

