#!/bin/sh
#
# /etc/init.d/nfcapd
#   and its symbolic link
# /(usr/)sbin/rceggdrop
#
### BEGIN INIT INFO
# Provides:          eggdrop
# Required-Start:    $syslog $network $remote_fs
# Required-Stop:     $syslog $network $remote_fs
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: Eggdrop IRC bot
# Description:       Eggdrop is the World's most popular Internet Relay Chat (IRC) bot. Eggdrop
#	is a feature rich program designed to be easily used and expanded upon by
#	both novice and advanced IRC users on a variety of hardware and software
#	platforms.
### END INIT INFO
DESCRIPTION="Eggdrop IRC bot"

# Check for missing binaries (stale symlinks should not happen)
# Note: Special treatment of stop for LSB conformance
DAEMON_BIN="/usr/lib64/eggdrop/eggdrop"
test -x ${DAEMON_BIN} || { echo "${DAEMON_BIN} not installed"; 
	if [ "$1" = "stop" ]; then exit 0;
	else exit 5; fi; }

# Check for existence of needed config file and read it
DAEMON_CONFIG="/etc/sysconfig/eggdrop"
test -r ${DAEMON_CONFIG} || { echo "${DAEMON_CONFIG} not existing";
	if [ "$1" = "stop" ]; then exit 0;
	else exit 6; fi; }

# Read config	
. ${DAEMON_CONFIG}

# Source LSB init functions
#. /lib/lsb/init-functions

# Source Shell functions
. /etc/rc.status

# Reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status
case "$1" in
    start)
	echo "Starting ${DESCRIPTION} "
	for BOT_NAME in ${EGGDROP_BOTS}; do
		if [ ! -d "${EGGDROP_DATADIR}/bots/${BOT_NAME}" ]; then
			/bin/mkdir -p ${EGGDROP_DATADIR}/bots/${BOT_NAME}
			if [ -n "${EGGDROP_USER}" ]; then
				/bin/chown ${EGGDROP_USER} "${EGGDROP_DATADIR}/bots/${BOT_NAME}"
			fi
			if [ -n "${EGGDROP_GROUP}" ]; then
				/bin/chgrp ${EGGDROP_GROUP} "${EGGDROP_DATADIR}/bots/${BOT_NAME}"
			fi
		fi
		if [ -d "${EGGDROP_DATADIR}/bots/${BOT_NAME}" ]; then
			echo -ne "\t${BOT_NAME}"
			DAEMON_PID="/var/run/eggdrop/${BOT_NAME}.pid"
			checkproc -p ${DAEMON_PID} ${DAEMON_BIN} && {
				echo $rc_running
			} || {
				if [ ! -f "${EGGDROP_DATADIR}/bots/${BOT_NAME}/${BOT_NAME}.user" ]; then
					EGGDROP_ARGS="-m"
				fi
				export EGG_LANGDIR="${EGGDROP_DATADIR}/language"
				/bin/su -m -c "cd ${EGGDROP_DATADIR}/bots/${BOT_NAME};/etc/eggdrop/${BOT_NAME}.conf ${EGGDROP_ARGS}" ${EGGDROP_USER} >/var/log/eggdrop/rc.eggdrop-${BOT_NAME} 2>&1
				rc_status -v
			}
		else
			rc_failed 6
			rc_status -v
			exit 6
		fi
	done
	;;
    stop)
	echo -n "Shutting down ${DESCRIPTION} "
	/sbin/killproc -TERM ${DAEMON_BIN}
	rc_status -v
	;;
    try-restart|condrestart)
	if test "$1" = "condrestart"; then
		echo "${attn} Use try-restart ${done}(LSB)${attn} rather than condrestart ${warn}(RH)${norm}"
	fi
	$0 status
	if test $? = 0; then
		$0 restart
	else
		rc_reset
	fi
	rc_status
	;;
    restart)
	$0 stop
	$0 start
	rc_status
	;;
    force-reload)
	echo -n "Reload service ${DESCRIPTION} "
	$0 try-restart
	rc_status
	;;
    reload)
	echo "Reload service ${DESCRIPTION} "
	for BOT_NAME in ${EGGDROP_BOTS}; do
		DAEMON_PID="/var/run/eggdrop/${BOT_NAME}.pid"
		echo -ne "\t${BOT_NAME}" 
		killproc -HUP -p ${DAEMON_PID} ${DAEMON_BIN}
		rc_status -v
	done
	;;
    status)
	echo "Checking for service ${DESCRIPTION} "
	## Check status with checkproc(8), if process is running
	## checkproc will return with exit status 0.

	# Return value is slightly different for the status command:
	# 0 - service up and running
	# 1 - service dead, but /var/run/  pid  file exists
	# 2 - service dead, but /var/lock/ lock file exists
	# 3 - service not running (unused)
	# 4 - service status unknown :-(
	# 5--199 reserved (5--99 LSB, 100--149 distro, 150--199 appl.)
	
	# NOTE: checkproc returns LSB compliant status values.
	for BOT_NAME in ${EGGDROP_BOTS}; do
		DAEMON_PID="/var/run/eggdrop/${BOT_NAME}.pid"
		checkproc -p ${DAEMON_PID} ${DAEMON_BIN}
		BOT_STATUS="$?"
		case "${BOT_STATUS}" in
			0)
				echo -ne "\t${BOT_NAME} is up and running" 
				rc_status -v
			;;
			1)
				echo -ne "\t${BOT_NAME} is dead but pid file exists"
				rc_failed ${BOT_STATUS}
				rc_status -v
			;;
			3)
				echo -ne "\t${BOT_NAME} is not running"
				rc_failed ${BOT_STATUS}
				rc_status -v
			;;
			4)
				echo -ne "\t${BOT_NAME} status unknown"
				rc_failed ${BOT_STATUS}
				rc_status -v
			;;
		esac
	done
	# NOTE: rc_status knows that we called this init script with
	# "status" option and adapts its messages accordingly.
	#rc_status -v
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload}"
	exit 1
	;;
esac
rc_exit
