#!/bin/sh
#
# myproxy-server - Server for X.509 Public Key Infrastructure (PKI) security credentials
#
# chkconfig: - 55 25
# description:  Server for X.509 Public Key Infrastructure (PKI) security credentials
#
### BEGIN INIT INFO
# Provides: myproxy-server
# Required-Start:  $local_fs $network $syslog
# Required-Stop:  $local_fs $syslog
# Should-Start:  $syslog
# Should-Stop:  $network $syslog
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Short-Description: Startup the MyProxy server daemon
# Description: Server for X.509 Public Key Infrastructure (PKI) security credentials
### END INIT INFO

# Source function library.
. /etc/rc.status

exec="/usr/sbin/myproxy-server"
prog=$(basename $exec)
pidfile=/var/run/myproxy-server.pid

# Defaults
MYPROXY_USER=myproxy
MYPROXY_OPTIONS=""
X509_USER_CERT=/etc/grid-security/myproxy/hostcert.pem
X509_USER_KEY=/etc/grid-security/myproxy/hostkey.pem

# Override defaults here.
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog

# A few sanity checks 
[ ! -r $X509_USER_KEY ]  && echo -n "$prog: No hostkey file"  && failure && echo && exit 5
[ ! -r $X509_USER_CERT ] && echo -n "$prog: No hostcert file" && failure && echo && exit 5

case "$1" in
    start)
	    echo -n "Starting myproxy-server: "
	    export $X509_USER_CERT
	    export $X509_USER_KEY
	    startproc -u $MYPROXY_USER -p ${pidfile} $exec $MYPROXY_OPTIONS -P ${pidfile}
	    #daemon --user $MYPROXY_USER X509_USER_CERT=$X509_USER_CERT X509_USER_KEY=$X509_USER_KEY $exec $MYPROXY_OPTIONS
	    rc_status -v
        ;;
    stop)
	echo -n "Stopping myproxy-server: "
	killproc -p ${pidfile} -TERM $exec
	rc_status -v
	;;
    restart)
	$0 stop
	$0 start
	rc_status
        ;;
    force-reload)
	$0 stop
	$0 start
	rc_status
        ;;
    status)
	echo -n "Checking for myproxy-server"
	checkproc -p ${pidfile} $exec
	rc_status -v
        ;;
    try-restart|condrestart)
        if checkproc -p${pidfile} ${exec} > /dev/null ; then
            echo "" # Do nothing
	else
	    $0 stop
	    $0 start
	    rc_status
        fi
	;;
    reload)
	echo -n "Reloading MyProxy configuration: "
	kllproc -p ${pidfile} -HUP $exec
	rc_status -v
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|reload|try-restart|force-reload}"
        exit 2
esac
