#!/bin/bash
### BEGIN INIT INFO
# Provides: saslauthd
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
### END INIT INFO
. /lib/lsb/init-functions

[ -e /etc/default/cyrus-sasl ] && source /etc/default/cyrus-sasl
[ $(/usr/bin/tty -s; echo $?) -gt 0 -o  "$(/usr/bin/tty)" == "/dev/console" ] && [ "$START_SASLAUTHD" == "FALSE" ] && exit 0

case "$1" in
	start)
		echo -n "Starting SASL authentication daemon: saslauthd"
		if start_daemon -p /var/run/saslauthd/saslauthd.pid /usr/sbin/saslauthd -a $SASLAUTHD_AUTHMECH
		then
			log_success_msg "."
		else
			log_failure_msg
		fi
		;;
	stop)
		echo -n "Stopping SASL authentication daemon: saslauthd"
		if killproc -p /var/run/saslauthd/saslauthd.pid /usr/sbin/saslauthd
		then
			log_success_msg "."
		else
			log_failure_msg
		fi
		;;
	restart|force-reload)
		$0 stop
		sleep 1
		$0 start
		;;
	try-restart)
		if pidofproc -p /var/run/saslauthd/saslauthd.pid /usr/sbin/saslauthd
		then
			$0 restart
		fi
		;;
	status)
		pidofproc -p /var/run/saslauthd/saslauthd.pid /usr/sbin/saslauthd
		exit $?
		;;
	*)
		echo "Usage: $0 {start|stop|restart|try-restart|force-reload|status}"
		exit 1
		;;
esac
