#!/bin/sh
#
# zookeeper ZooKeeper Server
#
# chkconfig: - 80 05
# description: Enable ZooKeeper Server
#

### BEGIN INIT INFO
# Provides:          zookeeper
# Default-Start:
# Default-Stop:
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Description:       zookeeper Server
# Short-Description: Enable zookeeper  Server
### END INIT INFO

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

prog=<%= scope.lookupvar("zookeeper::service_name") %>
desc="Zookeeper Service"

[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

lockfile="/var/lock/subsys/$prog"
pidfile=<%= scope.lookupvar("zookeeper::pid_path") %>

[ "x$JMXLOCALONLY" = "x" ] && JMXLOCALONLY=false

if [ "x$JMXDISABLE" = "x" ]
then
    # for some reason these two options are necessary on jdk6 on Ubuntu
    #   accord to the docs they are not necessary, but otw jconsole cannot
    #   do a local attach
    ZOOMAIN="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=$JMXLOCALONLY <%= scope.lookupvar("zookeeper::zoo_main") %>"
else
    ZOOMAIN=<%= scope.lookupvar("zookeeper::zoo_main") %>
fi

ZOOBINDIR="<%= scope.lookupvar("zookeeper::service::_zoo_dir") %>/bin"
ZOOCFGDIR=<%= scope.lookupvar("zookeeper::cfg_dir") %>
ZOOCFG="$ZOOCFGDIR/zoo.cfg"
ZOO_LOG_DIR=<%= scope.lookupvar("zookeeper::log_dir") %>

[ -e "$ZOOCFGDIR/java.env" ] && . "$ZOOCFGDIR/java.env"
[ -e "$ZOOCFGDIR/<%= scope.lookupvar("zookeeper::environment_file") %>" ] && . "$ZOOCFGDIR/<%= scope.lookupvar("zookeeper::environment_file") %>"

[ "x$ZOO_LOG4J_PROP" = "x" ] && ZOO_LOG4J_PROP="<%= scope.lookupvar("zookeeper::log4j_prop") %>"

for f in ${ZOOBINDIR}/../zookeeper-*.jar
do
    CLASSPATH="$CLASSPATH:$f"
done

ZOOLIBDIR=${ZOOLIBDIR:-$ZOOBINDIR/../lib}
for i in "$ZOOLIBDIR"/*.jar
do
    CLASSPATH="$CLASSPATH:$i"
done

#add the zoocfg dir to classpath
CLASSPATH=$ZOOCFGDIR:$CLASSPATH

cmd="java  \"-Dzookeeper.log.dir=${ZOO_LOG_DIR}\" \"-Dzookeeper.root.logger=${ZOO_LOG4J_PROP}\" -cp ${CLASSPATH} ${JAVA_OPTS} ${JVMFLAGS} ${ZOOMAIN} ${ZOOCFG} & echo \$! > ${pidfile}"


start() {
    echo $pidfile
    echo -n "Starting $desc $prog: "
    touch $pidfile && chown <%= scope.lookupvar("zookeeper::user") %> $pidfile
    daemon --user <%= scope.lookupvar("zookeeper::user") %> --pidfile $pidfile "$cmd"
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc -p $pidfile  $prog
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    stop
    start
}

reload() {
    restart
}

get_status() {
    if [ -f $pidfile ]
    then
      PID=`cat $pidfile`
      if [ -z "`ps -ef | awk '{print $2}' | grep "^$PID$"`" ]
      then
        echo "$prog stopped but pid file exists"
        exit 1
      else
        echo "$prog running with pid $PID"
        exit 0
      fi
    else
      echo "$prog stopped"
      exit 1
    fi
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    reload)
        reload
        ;;
    condrestart)
        [ -e /var/lock/subsys/$prog ] && restart
        RETVAL=$?
        ;;
    status)
        get_status
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|reload|condrestart|status}"
        RETVAL=1
esac

exit $RETVAL
