#!/bin/sh

PID_FILE="@IRCDDIR@/ircd.pid"
PID_BACKUP="@IRCDDIR@/ircd.pid.bak"
if [ "$1" = "start" ] ; then
	echo "Starting UnrealIRCd"
	if [ -r $PID_FILE ] ; then
		mv -f $PID_FILE $PID_BACKUP
	fi
	@BINDIR@
	sleep 1
	if [ ! -r $PID_FILE ] ; then
		echo "Possible error encountered (IRCd seemily not started)"	
		echo "====================================================="
		echo "Check above for possible errors, and this output of  "
		echo "ircd.log. If you cannot solve the problem, read"
		echo "Unreal.nfo on where to get support"
		echo "====================================================="
		tail -n 5 @IRCDDIR@/ircd.log
		if [ -r $PID_BACKUP ] ; then
			mv -f $PID_BACKUP $PID_FILE
		fi
	fi
elif [ "$1" = "stop" ] ; then
	echo "Stopping UnrealIRCd"
	kill -9 `cat $PID_FILE`
elif [ "$1" = "rehash" ] ; then
	echo "Rehashing UnrealIRCd"
	kill -1 `cat $PID_FILE`
elif [ "$1" = "restart" ] ; then
	echo "Restarting UnrealIRCd"
	kill -2 `cat $PID_FILE`
elif [ "$1" = "mkpasswd" ] ; then
	@BINDIR@ -P $2 $3
elif [ "$1" = "version" ] ; then
	@BINDIR@ -v
else
	echo "Usage: unreal start|stop|rehash|restart|mkpasswd|version"
fi
