#!/bin/sh
#
# Startup script for the OCSPD responder
#
# chkconfig: 345 85 15
# description: OpenCA OCSP Responder
# processname: openca-ocspd

prefix="/usr";
sbin="${prefix}/sbin";
etc="${prefix}/etc/ocspd";
run="${prefix}/var/run";

# Source function library.
# . /etc/rc.d/init.d/functions

ocspd="${sbin}/ocspd";
conf="${etc}/ocspd.xml";

# Take a look in your apache config and set it as it is set there.
pidfile="${run}/ocspd.pid";

# See how we were called.
case "$1" in
  start)
	echo -n "Starting OCSP Responder: "
	${ocspd} -c "${conf}" -d
	echo "Done."
	;;
  start-verbose)
	echo -n "Starting OCSP Responder: "
	${ocspd} -c "${conf}" -d -v
	echo "Done."
	;;
  start-debug)
	echo -n "Starting OCSP Responder: "
	${ocspd} -c "${conf}" -d -v -debug
	echo "Done."
	;;
  stop)
	echo -n "Shutting down OCSP Responder: "
	if [ -f "$pidfile" ] ; then
		pid=`cat $pidfile`;
		# if [ "x$pid" = "x" ] ; then
		# 	killall -15 ocspd
		# else
			kill -15 ${pid}
		# fi
		rm -f "$pidfile"
	else
		echo "Missing pidfile (already stopped?)"
	fi
	echo "Done."
	;;
  status)
	echo -n "OCSP Responder is "
	if ! [ -f "$pidfile" ] ; then
		echo "stopped."
	else
		pid=`cat $pidfile`;
		if test `ps -p ${pid}` ; then
			echo "running ( $pid ) ... "
		else
			echo "stopped."
		fi
	fi
	;;
  reload-crl)
	echo -n "Sending Reload CRL Signal to OCSP ... "
	if ! [ -f "$pidfile" ] ; then
		echo "stopped."
	else
		pid=`cat $pidfile`;
		if test `kill -HUP ${pid}` ; then
			echo "error."
		else
			echo "Ok."
		fi
	fi
	;;
  restart)
	$0 stop
	sleep 2
	$0 start
	;;
  *)
	echo "Usage: $0 {start|stop|status|reload-crl|restart}"
	exit 1
esac

exit 0
