#!/bin/sh

# chkconfig: 345 98 05

. /etc/init.d/functions
alias echo_success="success; echo"
alias echo_failure="failure; echo"
alias echo_warning="warning; echo"

PIDFILE=/var/run/indexer_proxy/indexer_proxy.pid
UWSGI=/usr/bin/uwsgi
CONFIG=/etc/indexer_proxy/uwsgi.xml
RUN="${UWSGI} --pidfile=${PIDFILE} -d -x ${CONFIG}"

case "$1" in
	start)
		if [ -f $PIDFILE ] ; then
			echo_failure "UWSGI is already running"
			exit 0
		fi
		$RUN
		sleep 3
		if checkpid `cat $PIDFILE`  ; then
			echo_success "Started"
		else
			echo_failure "Failed to start"
		fi
	;;
	stop)
		if [ -f $PIDFILE ] ; then
			kill -INT `cat $PIDFILE`
			echo -n "Stopping "
			while [ -x $PIDFILE ] ; do
				echo -n '.'
				sleep 1
			done
			echo
			sleep 1
			echo_success "Stopped"
		else
			echo_failure "${PIDFILE} not found"
			
		fi
	;;
	restart)
		$0 stop
		$0 start
	;;
	*)
        	N=/etc/init.d/$NAME
        	echo "Usage: $N {start|stop|restart}" >&2
        	exit 1
        	;;
esac
