#!/bin/sh
#    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.
#
# Karaf control script
#
# chkconfig: - 80 20
# description: Karaf startup script
# processname: ${KARAF_SERVICE_NAME}
# pidfile: ${KARAF_SERVICE_PIDFILE}
# config: ${KARAF_SERVICE_CONF}
#

# Source function library.
. /etc/init.d/functions

# Load Java configuration.
[ -r /etc/java/java.conf ] && . /etc/java/java.conf
export JAVA_HOME

if [ -r "${KARAF_SERVICE_CONF}" ]; then
    . "${KARAF_SERVICE_CONF}"
else
    echo "Error KARAF_SERVICE_CONF not defined"
    exit -1
fi

if [ -z "$STARTUP_WAIT" ]; then
    STARTUP_WAIT=30
fi

if [ -z "$SHUTDOWN_WAIT" ]; then
    SHUTDOWN_WAIT=30
fi

prog=${KARAF_SERVICE_NAME}
currenttime=$(date +%s%N | cut -b1-13)

start() {
    echo -n "Starting $prog: "
    if [ -f "$KARAF_SERVICE_PIDFILE" ]; then
        read ppid < "$KARAF_SERVICE_PIDFILE"
        if [ `ps --pid $ppid 2> /dev/null | grep -c $ppid 2> /dev/null` -eq '1' ]; then
            echo -n "$prog is already running"
            failure
            echo
            return 1
        else
            rm -f "$KARAF_SERVICE_PIDFILE"
        fi
    fi

    LOG_PATH=`dirname "$KARAF_SERVICE_LOG"`
    mkdir -p "$LOG_PATH"
    cat /dev/null > "$KARAF_SERVICE_LOG"
    chown $KARAF_SERVICE_USER:$KARAF_SERVICE_GROUP  "$KARAF_SERVICE_LOG"

    PID_PATH=`dirname "$KARAF_SERVICE_PIDFILE"`
    mkdir -p "$PID_PATH"
    chown $KARAF_SERVICE_USER:$KARAF_SERVICE_GROUP "$PID_PATH" || true

    if [ ! -z "$KARAF_SERVICE_USER" ]; then
        if [ -r /etc/rc.d/init.d/functions ]; then
            daemon \
                --user="$KARAF_SERVICE_USER" \
                --pidfile="$KARAF_SERVICE_PIDFILE" \
                " { \"$KARAF_SERVICE_PATH/bin/$KARAF_SERVICE_EXECUTABLE\" daemon >> \"$KARAF_SERVICE_LOG\" 2>&1 & } ; echo \$! >| \"$KARAF_SERVICE_PIDFILE\" "
        else
            su - $KARAF_SERVICE_USER \
                -c " { \"$KARAF_SERVICE_PATH/bin/$KARAF_SERVICE_EXECUTABLE\" daemon >> \"$KARAF_SERVICE_LOG\" 2>&1 & } ; echo \$! >| \"$KARAF_SERVICE_PIDFILE\" "
        fi

        if [ -f "$KARAF_SERVICE_PIDFILE" ]; then
            chown $KARAF_SERVICE_USER:$KARAF_SERVICE_GROUP "$KARAF_SERVICE_PIDFILE"
        fi
    fi

    RETVAL=$?
    echo

    if [ $RETVAL -eq 0 ]; then
        touch $KARAF_LOCKFILE
    fi

    return $RETVAL
}

stop() {
    echo -n $"Stopping $prog: "
    count=0;

    if [ -f "$KARAF_SERVICE_PIDFILE" ]; then
        read kpid < "$KARAF_SERVICE_PIDFILE"
        let kwait=$SHUTDOWN_WAIT

        # Try issuing SIGTERM
        su - $KARAF_SERVICE_USER \
            -c "export JAVA_HOME=$JAVA_HOME; \"$KARAF_SERVICE_PATH/bin/$KARAF_SERVICE_EXECUTABLE\" stop"

        until [ `ps --pid $kpid 2> /dev/null | grep -c $kpid 2> /dev/null` -eq '0' ] || [ $count -gt $kwait ]
        do
            sleep 1
            let count=$count+1;
        done

        if [ $count -gt $kwait ]; then
            kill -9 $kpid
        fi
    fi

    rm -f "$KARAF_SERVICE_PIDFILE"
    rm -f "$KARAF_LOCKFILE"

    success
    echo
}

status() {
    if [ -f "$KARAF_SERVICE_PIDFILE" ]; then
        read ppid < "$KARAF_SERVICE_PIDFILE"
        if [ `ps --pid $ppid 2> /dev/null | grep -c $ppid 2> /dev/null` -eq '1' ]; then
            echo "$prog is running (pid $ppid)"
            return 0
        else
            echo "$prog dead but pid file exists"
            return 1
        fi
    fi
    echo "$prog is not running"
    return 3
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        $0 stop
        $0 start
        ;;
    status)
        status
        ;;
    *)
        ## If no parameters are given, print which are avaiable.
        echo "Usage: $0 {start|stop|status|restart}"
        exit 1
        ;;
esac
