#!/bin/sh -e
#
# eZ Find init script for debian.
#
# Usage:
#
# Set the correct SOLR_HOME value, and copy this file to /etc/init.d
# Symlink to /etc/init.d/solr to /etc/rc3.d/S70solr and /etc/rc5.d/S70solr
#
# Example:
# cp solr /etc/init.d/solr
# cd /etc/init.d && chmod 755 solr
# cd /etc/rc2.d && ln -s ../init.d/solr S70solr
# cd /etc/rc5.d && ln -s ../init.d/solr S70solr
# cd /etc/rc2.d && ln -s ../init.d/solr K70solr
# cd /etc/rc5.d && ln -s ../init.d/solr K70solr


. /lib/lsb/init-functions

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="Solr indexing server"
NAME=solr
SOLR_HOME=/opt/solr/java
DAEMON=/usr/bin/java
DAEMON_ARGS="-jar start.jar"
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME
SOLR_ARGS="-Dsolr.solr.home=/opt/solr/cores -Xms2048m -Xmx2048m -Djetty.logs=/opt/solr/logs"

# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0

#
#	Function that starts the daemon/service.
#
d_start() {
	#start-stop-daemon --start --pidfile $PIDFILE --chdir $SOLR_HOME --background --make-pidfile --exec $DAEMON -- $PARAMETERS
	# Return
	#   0 if daemon has been started
	#   1 if daemon was already running
	#   2 if daemon could not be started
	echo $SOLR_ARGS
	start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test -- $DAEMON_ARGS > /dev/null \
		|| return 1
	start-stop-daemon --start --quiet --pidfile $PIDFILE --chdir $SOLR_HOME --background --make-pidfile --exec $DAEMON -- \
		$DAEMON_ARGS $SOLR_ARGS \
		|| return 2
    sleep 2
}
#
#	Function that stops the daemon/service.
#
d_stop() {
	start-stop-daemon --stop --quiet --pidfile $PIDFILE --name java
        rm -f $PIDFILE
}

#
#	Function that checks if solr is running
#	Returns success (0) if solr is running, failure (1) if not running
#
d_status() {
    if [ -f "$PIDFILE" ] && ps `cat $PIDFILE` >/dev/null 2>&1; then
		return 0 # EXIT_SUCCESS
    else
	  	return 1 # EXIT_FAILURE
    fi
}

case "$1" in
  start)
	echo " * Starting $DESC: $NAME"
	if d_status; then
		echo "   ...$NAME is already running."
		return 1
	fi
	d_start
	if d_status; then
		echo "   ...done."
	else
		echo "   ...failed to start solr"
		exit 1
	fi
	;;
  stop)
	echo " * Stopping $DESC: $NAME"
	if d_status; then
		d_stop
		if d_status; then
			echo "   ...$NAME is still running."
			exit 1
		else
			echo "   ...done."
		fi
	else
		echo "   ...$NAME is not running."
	fi
	;;
  status)
	if d_status; then
		echo " * $NAME is running (PID: `cat $PIDFILE`)"
	else
		echo " * $NAME is not running."
	fi
	;;
  restart|force-reload)
	echo -n "Restarting $DESC: $NAME"
	d_stop
	sleep 1
	d_start
	echo "."
	;;
  *)
	echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0