#!/bin/sh

### BEGIN INIT INFO
# Provides:
# Required-Start:
# Default-Start: 3 4 5
# Default-Stop: 0 1 2
# Description: sasl2 authentication daemon
### END INIT INFO

NAME="Cyrus sasl2 authentication daemon"
DAEMON=/usr/cyrus-sasl2/sbin/saslauthd
LD_LIBRARY_PATH=/usr/cyrus-sasl2/lib:/usr/cyrus-sasl2/lib/sasl:/usr/lib:/lib

tmpfile=/tmp/pid.$$

ps -aef > $tmpfile

case "$1" in
 start)

  if grep -q "$DAEMON" $tmpfile 
  then
	echo "$NAME already running."
  else
	echo "Starting $NAME."
	# for now, we have our own cyrus library
	cd /var/run
	LD_LIBRARY_PATH="/usr/cyrus-sasl2/lib:/usr/cyrus-sasl2/lib/sasl:/usr/lib:/lib" $DAEMON -a pam &
	ret=$?
  fi

  ;;

 stop)
	pid=`grep "$DAEMON" $tmpfile | awk '{ if ($3 == 1) print $2;}'`
	if [ -n "$pid" ]; then
		echo "Shutting down $NAME."
		kill -1 $pid >/dev/null
	else
		echo "$NAME is not running."
	fi
  ret=0

  ;;

 force-reload|reload)
  echo "Reloading saslauthd configuration files: "
  ret=$?
  echo "done."
  ;;

 restart)
  $0 stop
  $0 start
  ret=$?
  ;;

 status)
  ret=3
  ;;

 *)
  echo "Usage: saslauthd {[re]start|stop|[force-]reload|status}"
  ret=2
  ;;

esac

rm -f $tmpfile
exit $ret
