#!/bin/sh
# Init script for logstash
# Maintained by 
# Generated by pleaserun.
# Implemented based on LSB Core 3.1:
#   * Sections: 20.2, 20.3
#
### BEGIN INIT INFO
# Provides:          logstash
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: 
# Description:       logstash
### END INIT INFO

PATH=/sbin:/usr/sbin:/bin:/usr/bin
export PATH

name=logstash
program=/usr/share/logstash/bin/logstash
args=--path.settings\ /etc/logstash
pidfile="/var/run/$name.pid"
user="logstash"
group="logstash"
chroot="/"
chdir="/"
nice="19"
limit_open_files="16384"


# If this is set to 1, then when `stop` is called, if the process has
# not exited within a reasonable time, SIGKILL will be sent next.
# The default behavior is to simply log a message "program stop failed; still running"
KILL_ON_STOP_TIMEOUT=0

# When loading default and sysconfig files, we use `set -a` to make
# all variables automatically into environment variables.
set -a
[ -r /etc/default/logstash ] && . /etc/default/logstash
[ -r /etc/sysconfig/logstash ] && . /etc/sysconfig/logstash
set +a

[ -z "$nice" ] && nice=0

# Source function library.
. /etc/rc.d/init.d/functions
pidopts="-p $pidfile"

trace() {
  logger -t "/etc/init.d/logstash" "$@"
}

emit() {
  trace "$@"
  echo "$@"
}

rh_status() {
  status $pidopts $program
  RETVAL=$?
  return $RETVAL
}

start() {

  # Ensure the log directory is setup correctly.
  if [ ! -d "/var/log" ]; then 
    mkdir "/var/log"
    chown "$user":"$group" "/var/log"
    chmod 755 "/var/log"
  fi


  # Setup any environmental stuff beforehand
  ulimit -n ${limit_open_files}

  # Run the program!
  # TODO: Port this to use 'daemon'
  nice -n "$nice" \
  chroot --userspec "$user":"$group" "$chroot" sh -c "
    ulimit -n ${limit_open_files}
    cd \"$chdir\"
    exec \"$program\" $args
  " >> /var/log/logstash-stdout.log 2>> /var/log/logstash-stderr.log &

  # Generate the pidfile from here. If we instead made the forked process
  # generate it there will be a race condition between the pidfile writing
  # and a process possibly asking for status.
  echo $! > $pidfile

  emit "$name started"
  return 0
}

stop() {
  # Try a few times to kill TERM the program
  if rh_status ; then
    pid=$(cat "$pidfile")
    trace "Killing $name (pid $pid) with SIGTERM"
    killproc $pidopts $program
    # Wait for it to exit.
    for i in 1 2 3 4 5 ; do
      trace "Waiting $name (pid $pid) to die..."
      rh_status || break
      sleep 1
    done
    if rh_status ; then
      if [ "$KILL_ON_STOP_TIMEOUT" -eq 1 ] ; then
        trace "Timeout reached. Killing $name (pid $pid) with SIGKILL.  This may result in data loss."
        kill -KILL $pid
        emit "$name killed with SIGKILL."
      else
        emit "$name stop failed; still running."
      fi
    fi
  fi
}

force_stop() {
  if rh_status ; then
    stop
    rh_status && kill -KILL $(cat "$pidfile")
  fi
}


case "$1" in
  force-start|start|stop|force-stop|restart)
    trace "Attempting '$1' on logstash"
    ;;
esac

case "$1" in
  force-start)
    PRESTART=no
    exec "$0" start
    ;;
  start)
    rh_status
    code=$?
    if [ $code -eq 0 ]; then
      exit $code
    else
      start
      exit $?
    fi
    ;;
  stop) stop ;;
  force-stop) force_stop ;;
  status)
    rh_status
    code=$?
    exit $code
    ;;
  restart)
    
    stop && start
    ;;
  *)
    echo "Usage: $SCRIPTNAME {start|force-start|stop|force-start|force-stop|status|restart}" >&2
    exit 3
  ;;
esac

exit $?
