#!/bin/bash
#
# chef-expander Startup script for the Chef search indexer
#
# chkconfig: - 98 02
# description: Search indexer component of the Chef Server

### BEGIN INIT INFO
# Provides: chef-expander
# Required-Start: $local_fs $network $remote_fs chef-solr rabbitmq-server
# Required-Stop: $local_fs $network $remote_fs chef-solr rabbitmq-server
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Should-Start: $named $time
# Should-Stop: $named $time
# Short-Description: Startup script for the Chef search indexer
# Description: Search indexer component of the Chef Server.
### END INIT INFO

# Source function library
. /etc/rc.status

exec="/usr/bin/chef-expander"
prog="chef-expander"

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

config=${CONFIG-/var/opt/chef-server/chef-expander/etc/expander.rb}
pidfile=${PIDFILE-/var/run/chef/expander.pid}
logfile=${LOGFILE-/var/log/chef-server/chef-expander/expander.log}
options=${OPTIONS-}

DAEMON_OPTS="-d -c $config -P $pidfile -L $logfile"

case "$1" in
    start)
	echo -n $"Starting $prog: "
        if [ ! -d /var/run/chef ]; then
            mkdir -p /var/run/chef
            chmod 0750 /var/run/chef
            chown chef:chef /var/run/chef
        fi

	start_daemon -q $exec $DAEMON_OPTS
	# As with rubygem-chef startup, need to double-check it's actually
	# running before we return success.
	for i in 1 2 3 4 5 ; do
		sleep 1
		rc_reset
		checkproc -p $pidfile $prog
		rc_status && break
	done
	rc_status -v
        ;;
    stop)
	echo -n $"Stopping $prog: "
	killproc $prog -p "$pidfile"
        rc_status -v
        ;;
    restart)
	$0 stop
	$0 start
        ;;
    reload)
        ;;
    force-reload)
        force_reload
        ;;
    status)
	echo -n $"Checking $prog: "
        checkproc -p $pidfile $prog
        rc_status -v
        ;;
    condrestart|try-restart)
        $0 status
        if test $? = 0; then
                $0 restart
        else
                rc_reset        # Not running is not a failure.
        fi
        # Remember status and be quiet
        rc_status
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac
exit $?
