#!/bin/bash

# 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.
#
# Starts Zookeeper server
#
# chkconfig: 345 80 20
# description: Summary: ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services. All of these kinds of services are used in some form or another by distributed applications. Each time they are implemented there is a lot of work that goes into fixing the bugs and race conditions that are inevitable. Because of the difficulty of implementing these kinds of services, applications initially usually skimp on them ,which make them brittle in the presence of change and difficult to manage. Even when done correctly, different implementations of these services lead to management complexity when the applications are deployed.
#
### BEGIN INIT INFO
# Provides:          zookeeper-server
# Required-Start:    $network $local_fs
# Should-Start:      $named
# Required-Stop:     $syslog $remote_fs
# Should-Stop:
# Default-Start:     3 4 5
# Default-Stop:      0 1 2 6
# Short-Description: ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services.
### END INIT INFO

. /lib/lsb/init-functions

# Autodetect JAVA_HOME if not defined
. /usr/lib/bigtop-utils/bigtop-detect-javahome

. /etc/default/zookeeper

STATUS_RUNNING=0
STATUS_DEAD=1
STATUS_DEAD_AND_LOCK=2
STATUS_NOT_RUNNING=3

ERROR_PROGRAM_NOT_INSTALLED=5

RETVAL=0
EXEC_PATH="/usr/bin/zookeeper-server"
NAME=zookeeper-server
DESC="$NAME"
PIDFILE=/var/run/zookeeper/zookeeper_server.pid
LOCKFILE="/var/lock/subsys/zookeeper"
USER=zookeeper

install -d -m 0755 -o zookeeper -g zookeeper /var/run/zookeeper/


start() {
  [ -x $exec ] || exit $ERROR_PROGRAM_NOT_INSTALLED
  log_success_msg "Starting $DESC: "
  start_daemon -u $USER $EXEC_PATH start
  RETVAL=$?
  echo
  [ $RETVAL -eq 0 ] && touch $LOCKFILE
  return $RETVAL
}

stop() {
  log_success_msg "Stopping $DESC: "
  start_daemon -u $USER $EXEC_PATH stop
  RETVAL=$?
  sleep 5
  echo
  [ $RETVAL -eq 0 ] && rm -f $LOCKFILE $PIDFILE
}

restart() {
  stop
  start
}

checkstatus(){
  pidofproc -p $PIDFILE java > /dev/null
  status=$?

  case "$status" in
    $STATUS_RUNNING)
      log_success_msg "$NAME is running"
      ;;
    $STATUS_DEAD)
      log_failure_msg "$NAME is dead and pid file exists"
      ;;
    $STATUS_DEAD_AND_LOCK)
      log_failure_msg "$NAME is dead and lock file exists"
      ;;
    $STATUS_NOT_RUNNING)
      log_failure_msg "$NAME is not running"
      ;;
    *)
      log_failure_msg "$NAME status is unknown"
      ;;
  esac
  return $status
}

condrestart(){
  [ -e $LOCKFILE ] && restart || :
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
    checkstatus
    ;;
  restart)
    restart
    ;;
  condrestart|try-restart)
    condrestart
    ;;
  init)
    checkstatus
    if [ "$?" = "$STATUS_RUNNING" ] ; then
      echo "Error: $DESC is running. Stop it first." >&2
      exit 1
    else
      shift
      su -s /bin/bash $USER -c "zookeeper-server-initialize $*"
    fi
    ;;
  *)
    echo $"Usage: $0 {start|stop|status|restart|try-restart|condrestart|init}"
    exit 1
esac

exit $RETVAL
