#! /bin/bash
#
# elasticsearch <summary>
#
# chkconfig:   - 80 20
# description: Starts and stops a single elasticsearch instance on this system 
#
### BEGIN INIT INFO
# Provides:          elasticsearch
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts elasticsearch
# Description: Elasticsearch
### END INIT INFO

#
# init.d / servicectl compatibility (openSUSE)
#
if [ -f /etc/rc.status ]; then
    . /etc/rc.status
    rc_reset
fi

#
# Source function library.
#
if [ -f /etc/init.d/functions ]; then
    . /etc/init.d/functions
fi

exec="/usr/share/java/elasticsearch/bin/elasticsearch"
prog="elasticsearch"
pidfile=/var/run/elasticsearch/${prog}.pid

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

export ES_HEAP_SIZE
export ES_HEAP_NEWSIZE
export ES_DIRECT_SIZE
export ES_JAVA_OPTS

ES_HOME=/usr/share/java/elasticsearch
ES_USER=elasticsearch

DAEMON=${ES_HOME}/bin/elasticsearch
NAME=elasticsearch
PID_FILE=${PIDFILE:-/var/run/${NAME}/${NAME}.pid}
LOCK_FILE=${LOCKFILE:-/var/lock/subsys/${NAME}}
NFILES=${NFILES:-65000}

ES_PATH_LOG=${ES_PATH_LOG:-/var/log/${NAME}}
ES_PATH_DATA=${ES_PATH_DATA:-/var/lib/${NAME}}
ES_PATH_WORK=${ES_PATH_WORK:-/tmp/${NAME}}
ES_PATH_CONF=${ES_PATH_CONF:-/etc/${NAME}}
ES_PATH_PLUGINS=${ES_PATH_PLUGINS:-${ES_HOME}/plugins}
ES_CONFIG=${ES_CONFIG:-${ES_PATH_CONF}/elasticsearch.yml}

DAEMON_OPTS="-p ${PID_FILE} \
    -Des.config=${ES_CONFIG} \
    -Des.path.conf=${ES_PATH_CONF} \
    -Des.path.home=${ES_HOME} \
    -Des.path.logs=${ES_PATH_LOG} \
    -Des.path.data=${ES_PATH_DATA} \
    -Des.path.work=${ES_PATH_WORK} \
    -Des.path.plugins=${ES_PATH_PLUGINS}"

# These environment variables are passed over.
ES_MIN_MEM=${ES_MIN_MEM:-256m}
ES_MAX_MEM=${ES_MAX_MEM:-1g}
ES_INCLUDE=${ES_INCLUDE:-${ES_HOME}/bin/elasticsearch.in.sh}

get_pid()
{
    pid=$(pgrep -f "$ES_HOME")
    if [ ! -f "$PID_FILE" ]; then
        return 3
    elif [ "$pid" = "$(cat $PID_FILE)" ]; then
        echo "$pid"
        return 0
    fi
    return 3
}

start()
{
    echo -n $"Starting ${NAME} "
    get_pid >/dev/null
    if [ $? = 0 ]; then
        echo -n "... already running"
        return 5
    else
        mkdir -p $ES_PATH_WORK
        ulimit -n $NFILES

        ES_HOME=$ES_HOME \
        ES_INCLUDE=$ES_INCLUDE \
        ES_MIN_MEM=$ES_MIN_MEM \
        ES_MAX_MEM=$ES_MAX_MEM \
        startproc -t 5 -p $PID_FILE -f -u $ES_USER $DAEMON $DAEMON_OPTS
        RET=$?

        if [ $RET = 7 ]; then
            for i in {0..10}; do
                test -f "$PID_FILE" && return 0
                sleep 0.5
            done
        fi
        return $RET
    fi
}

stop()
{
    echo -n $"Stopping ${NAME} "
    pid=$(get_pid)
    if [ -n "$pid" ]; then
        kill $pid
        for i in {0..10}; do
            test -z "$(get_pid)" && return 0
            sleep 0.5
        done
        return 1
    else
        echo -n "... not running"
    fi
}

case "$1" in
    start)
        start
        rc_status -v
        ;;
    stop)
        stop
        rc_status -v
        ;;
    status)
        echo -n "Checking for service ${NAME} "
        get_pid >/dev/null
        rc_status -v
        ;;
    restart|force-reload)
        stop
        rc_status -v
        start
        rc_status -v
        ;;
    *)
        N=/etc/init.d/${NAME}
        echo "Usage: $N {start|stop|status|restart|force-reload}" >&2
        RETVAL=2
        ;;
esac
rc_exit
