#! /bin/sh
# Copyright (c) 2000-2001 SuSE GmbH, Nuernberg, Germany.
# Copyright (c) 2002-2003 SuSE Linux AG Nuernberg, Germany.
# All rights reserved.
#
# Author: Marius Tomaschewski <mt@suse.de>, 2000-2003
#
# /etc/init.d/web2ldap
#
### BEGIN INIT INFO
# Provides:		web2ldap
# Required-Start:	$network $named
# Required-Stop:
# Default-Start:	3 5
# Default-Stop:		0 1 2 6
# Description:		starts web2ldap interface
### END INIT INFO
#
# Return values acc. to LSB for all commands but status:
# 0       - success
# 1       - generic or unspecified error
# 2       - invalid or excess argument(s)
# 3       - unimplemented feature (e.g. "reload")
# 4       - user had insufficient privileges
# 5       - program is not installed
# 6       - program is not configured
# 7       - program is not running
# 8--199  - reserved (8--99 LSB, 100--149 distrib, 150--199 appl)
# 
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signaling is not supported) are
# considered a success.
#
[ -f /etc/sysconfig/web2ldap ] && . /etc/sysconfig/web2ldap

# Check for missing binaries (stale symlinks should not happen)
WEB2LDAP_BIN="/usr/lib/web2ldap/sbin/web2ldap.py"
test -x "$WEB2LDAP_BIN" || { echo "$WEB2LDAP_BIN not installed" ; exit 5 ; }

# Check for existence of needed config file and read it
WEB2LDAP_CFG="/etc/web2ldap/web2ldapcnf/standalone.py"
test -f "$WEB2LDAP_CFG" || { echo "$WEB2LDAP_CFG not existing" ;  exit 6 ; }

# for reload probe bellow:
WEB2LDAP_PID="/var/run/web2ldap/standalone.pid"
WEB2LDAP_LST="/etc/web2ldap/web2ldapcnf/__init__.py
              /etc/web2ldap/web2ldapcnf/hosts.py
              /etc/web2ldap/web2ldapcnf/misc.py
              /etc/web2ldap/web2ldapcnf/standalone.py"

#
# start quietly with clean environment,
# except user want to see output using:
#    rcweb2ldap start -v
#
quiet_start="-seq"
case $2 in
  -v) quiet_start="" ;;
esac

# Source status functions:
. /etc/rc.status

# Reset status of this service
rc_reset

case "$1" in
start)
	echo -n "Starting web2ldap gateway "
	# compilation may need time...
	startproc $quiet_start $WEB2LDAP_BIN $WEB2LDAP_PARAMS
	rc_status -v
;;

stop)
	echo -n "Shutting down web2ldap gateway "
	killproc -t5 -TERM $WEB2LDAP_BIN
	rc_status -v
	rm -f $WEB2LDAP_PID 2>/dev/null
;;

try-restart)
	$0 status
	if test $? = 0; then
		$0 restart
	else
		rc_reset   # Not running is not a failure.
	fi
	rc_status
;;

restart)
	$0 stop
	$0 start
	rc_status
;;

force-reload)
	# signaling supported, but
	# needed for log rotation:
	$0 try-restart
	rc_status
;;

reload)
	# rereads its config files
	killproc -HUP $WEB2LDAP_BIN
	touch $WEB2LDAP_PID
	rc_status -v
;;

status)
	echo -n "Checking for service web2ldap "
	checkproc $WEB2LDAP_BIN
	rc_status -v
;;

probe)
	for cfg in $WEB2LDAP_LST ; do
	  if test $cfg -nt $WEB2LDAP_PID ; then
	    echo reload
	    break
	  fi
	done
;;

*)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
	exit 1
;;
esac

rc_exit

