#!/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
# description: Karaf startup script
# processname: ${KARAF_SERVICE_NAME}
# pidfile: ${KARAF_SERVICE_PIDFILE}
# config: ${KARAF_SERVICE_CONF}
#

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

# Location of JDK
if [ -n "$JAVA_HOME" ]; then
    export JAVA_HOME
fi

# Setup the JVM
if [ -z "$JAVA" ]; then
    if [ -n "$JAVA_HOME" ]; then
        JAVA="$JAVA_HOME/bin/java"
    else
        JAVA="java"
    fi
fi

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

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

prog=${KARAF_SERVICE_NAME}

do_start() {
    echo "Starting $prog: "
    if [ -f "$KARAF_SERVICE_PIDFILE" ]; then
        read ppid < "$KARAF_SERVICE_PIDFILE"
        if [ `ps -p $ppid 2> /dev/null | grep -c $ppid 2> /dev/null` -eq '1' ]; then
            echo "$prog is already running"
            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 [ "$KARAF_SERVICE_USER" = "root" ]; then
            JAVA_HOME=$JAVA_HOME 
            export JAVA_HOME

            "$KARAF_SERVICE_PATH/bin/$KARAF_SERVICE_EXECUTABLE" daemon >> "$KARAF_SERVICE_LOG" 2>&1 &

            echo $! > "$KARAF_SERVICE_PIDFILE"
        else
            su - $KARAF_SERVICE_USER \
                -c " { export KARAF_EXEC=exec; export JAVA_HOME=$JAVA_HOME; \"$KARAF_SERVICE_PATH/bin/$KARAF_SERVICE_EXECUTABLE\" daemon >> \"$KARAF_SERVICE_LOG\" 2>&1 & } ; echo \$! >| \"$KARAF_SERVICE_PIDFILE\" "
        fi

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

    RETVAL=$?
    return $RETVAL
}

do_stop() {
    echo $"Stopping $prog: "
    count=0;

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

        if [ "$KARAF_SERVICE_USER" = "root" ]; then
            JAVA_HOME=$JAVA_HOME
            export JAVA_HOME

            "$KARAF_SERVICE_PATH/bin/$KARAF_SERVICE_EXECUTABLE" stop >> "$KARAF_SERVICE_LOG" 2>&1
        else
            su - $KARAF_SERVICE_USER \
                -c "export JAVA_HOME=$JAVA_HOME; \"$KARAF_SERVICE_PATH/bin/$KARAF_SERVICE_EXECUTABLE\" stop >> \"$KARAF_SERVICE_LOG\" 2>&1"
        fi 

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

        if [ $count -gt $kwait ]; then
            if [ `ps -p $kpid 2> /dev/null | grep -c $kpid 2> /dev/null` -eq '1' ]; then
                kill -9 $kpid
            fi
        fi
    fi

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

do_status() {
    if [ -f "$KARAF_SERVICE_PIDFILE" ]; then
        read ppid < "$KARAF_SERVICE_PIDFILE"
        if [ `ps -p $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)
        do_start
        ;;
    stop)
        do_stop
        ;;
    restart)
        do_stop
        do_start
        ;;
    status)
        do_status
        ;;
    *)
        ## If no parameters are given, print which are avaiable.
        echo "Usage: $0 {start|stop|status|restart}"
        exit 1
        ;;
esac