#!/bin/sh
#
# sqlgrey:	Init script for sqlgrey postfix policy service
#
# adopted for ALTLinux init by LAKostis <lakostis at altlinux.ru>
#
# chkconfig: 345 90 10
# description:	SQLgrey is a postfix grey-listing policy service.
# processname: sqlgrey
# config: /etc/sqlgrey/sqlgrey.conf
# pidfile: /run//sqlgrey/sqlgrey.pid

# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

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

PIDFILE=/run/sqlgrey/sqlgrey.pid
LOCKFILE=/run/lock/subsys/sqlgrey
WORKDIR=/var/lib/sqlgrey
RETVAL=0

start()
{
	cd "$WORKDIR" && \
	start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user sqlgrey -- sqlgrey -d
	RETVAL=$?
	return $RETVAL
}

stop()
{
	stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user sqlgrey --name sqlgrey -- sqlgrey -k
	RETVAL=$?
	return $RETVAL
}

restart()
{
	stop
	sleep 1 # hack: missing REUSEADDR from Net::Server?
	start
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	status)
		status --pidfile "$PIDFILE" --expect-user sqlgrey --name sqlgrey -- sqlgrey
		RETVAL=$?
		;;
	reloaddb)
		kill -SIGUSR1 `cat $PIDFILE`
		RETVAL=$?
		;;
	*)
		msg_usage "${0##*/} {start|stop|restart|condstop|condrestart|status|reloaddb}"
		RETVAL=1
esac

exit $RETVAL
