#!/bin/sh
#
# eggdrop	Eggdrop is an IRC bot, written in C
#
# chkconfig: - 90 10
# description:	Eggdrop is an IRC bot, written in C
# processname: eggdrop
# config: /etc/eggdrop.conf
# pidfile: /var/run/eggdrop/eggdrop.pid

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

PIDFILE=/var/run/eggdrop/eggdrop.pid
LOCKFILE=/var/lock/subsys/eggdrop
BINARY=/var/lib/eggdrop/eggdrop.conf
RUNAS=_eggdrop

SourceIfNotEmpty /etc/sysconfig/eggdrop

RETVAL=0

check()
{
	USERFILE=$(egrep "set userfile " /var/lib/eggdrop/eggdrop.conf |cut -d ' ' -f3 |sed -e "s/\"//g")
	[ -e /var/lib/eggdrop/"$USERFILE" ] && continue || NEED_USERFILE=1
}

start()
{
	check
	[ "$NEED_USERFILE" = 1 ] && OPTIONS="-m" ||:
	[ "$NEED_USERFILE" = 1 ] && echo "STARTING BOT IN USERFILE CREATION MODE.
	Telnet to the bot and enter 'NEW' as your nickname.
	OR go to IRC and type:  /msg Lamestbot hello
	This will make the bot recognize you as the master."

	start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --displayname eggdrop \
	--user "$RUNAS" -- "$BINARY" "$OPTIONS"
	RETVAL=$?
	return $RETVAL
}

stop()
{
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --displayname eggdrop \
	--expect-user "$RUNAS" -- eggdrop
	RETVAL=$?
	return $RETVAL
}

restart()
{
	stop
	start
}

reload()
{
		restart
} 

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	reload)
		reload
		;;
	restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	condreload)
		if [ -e "$LOCKFILE" ]; then
			reload
		fi
		;;
	status)
		status --pidfile "$PIDFILE" --expect-user "$RUNAS" -- eggdrop
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
		RETVAL=1
esac

exit $RETVAL
