#!/bin/sh
#
# ipropd	Heimdal Kerberos V replication daemons
# chkconfig:	2345 41 41
# description:	Heimdal Kerberos V replication daemons
# processname:	ipropd-master ipropd-slave
# config:	/etc/krb5.conf

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

# Get network config
. /etc/sysconfig/network

# Get service config
[ -f /etc/sysconfig/heimdal ] && . /etc/sysconfig/heimdal
SERVICE_RUN_NICE_LEVEL=${IPROPD_SERVICE_RUN_NICE_LEVEL:-0}

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
		msg_network_down "Kerberos Replication Daemons"
		exit 1
	fi
else
	exit 0
fi

start_master() {
	if [ -f /var/lock/subsys/ipropd-master ]; then
		msg_already_running "Kerberos Replication Daemon (Master)"
		return
	fi

	if ! is_yes "$MASTER_ENABLED"; then
		return
	fi

	msg_starting "Kerberos Replication Daemon (Master)"
	busy
	daemon ipropd-master --detach
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/ipropd-master
}

start_slave() {
	if [ -f /var/lock/subsys/ipropd-slave ]; then
		msg_already_running "Kerberos Replication Daemon (Slave)"
		return
	fi

	if ! is_yes "$SLAVE_ENABLED"; then
		return
	fi

	msg_starting "Kerberos Replication Daemon (Slave)"
	busy
	daemon ipropd-slave --detach $SLAVE_PARAMS
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/ipropd-slave
}

stop_master() {
	if [ ! -f /var/lock/subsys/ipropd-master ]; then
		if is_yes "$MASTER_ENABLED"; then
			msg_not_running "Kerberos Replication Daemon (Master)"
		fi
		return
	fi

	msg_stopping "Kerberos Replication Daemon (Master)"
	killproc ipropd-master
	rm -f /var/lock/subsys/ipropd-master >/dev/null 2>&1
}

stop_slave() {
	if [ ! -f /var/lock/subsys/ipropd-slave ]; then
		if is_yes "$SLAVE_ENABLED"; then
			msg_not_running "Kerberos Replication Daemon (Slave)"
		fi
		return
	fi

	msg_stopping "Kerberos Replication Daemon (Slave)"
	killproc ipropd-slave
	rm -f /var/lock/subsys/ipropd-slave >/dev/null 2>&1
}

start() {
	start_master
	start_slave
}

stop() {
	stop_master
	stop_slave
}

condrestart() {
	if [ ! -f /var/lock/subsys/ipropd-master -a ! -f /var/lock/subsys/ipropd-slave ]; then
		msg_not_running "Kerberos Replication Daemons"
		RETVAL=$1
		return
	fi

	stop
	start
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart|force-reload)
	stop
	start
	;;
  try-restart)
	condrestart 0
	;;
  force-reload)
	condrestart 7
	;;
  status)
	is_yes "$MASTER_ENABLED" && status ipropd-master
	is_yes "$SLAVE_ENABLED" && status ipropd-slave
	exit $?
	;;
  *)
	msg_usage "$0 {start|stop|restart|try-restart|force-reload|status}"
	exit 3
esac

exit $RETVAL
