#!/bin/bash
#
# Init file for the NorduGrid/ARC Information System
#
# chkconfig: - 55 25
# description: NorduGrid/ARC Information system
#
# Originally based on globus-mds v0.27.
#
# Written by:
#    Anders Waananen <waananen@nbi.dk>
#    Daniel Johansson <daniel@nsc.liu.se>
#    Mattias Ellert <mattias.ellert@fysast.uu.se>
#
# description:
#   generates BDII config and starts up the BDII
#   starts up the GIIS replacement server
#   generates GIIS registration config and starts up the registration
#
# config: /etc/sysconfig/nordugrid
# config: /etc/arc.conf
#
######################################################################

### BEGIN INIT INFO
# Provides:          grid-infosys
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Stop:      0 1 2 3 4 5 6
# Short-Description: NorduGrid/ARC Information system
# Description:       LDAP based grid information system for the ARC middleware
### END INIT INFO

# ARC_LOCATION
ARC_LOCATION=${ARC_LOCATION:-/usr}
if [ ! -d "$ARC_LOCATION" ]; then
    echo "ARC_LOCATION ($ARC_LOCATION) not found"
    exit 1
fi
export ARC_LOCATION

if [ -f /usr/sbin/bdii-update ]; then
    . $ARC_LOCATION/share/arc/grid-infosys-bdii5 $1
elif [ -f /usr/sbin/bdii4-update ]; then
    . $ARC_LOCATION/share/arc/grid-infosys-bdii4 $1
else
    echo "You don't have BDII 4 or 5 installed"
    echo "This is a necessary component for the ARC LDAP information system"
    exit 1
fi

case "$1" in
    start)
	start
	;;
    stop)
	stop
	;;
    restart | force-reload)
	stop
	# avoid race
	sleep 3
	start
	;;
    reload)
	;;
    status)
	status
	;;
    condrestart | try-restart)
	if [ -f /usr/sbin/bdii-update ]; then
	    if [ -f ${slapd_lock_file} ] || [ -f ${update_lock_file} ]; then
		stop
		# avoid race
		sleep 3
		start
	    fi
	else
	    if [ -f "$PID_FILE" ] ; then
		stop
		# avoid race
		sleep 3
		start
	    fi
	fi
	;;
    *)
	echo "Usage: $0 {start|stop|restart|force-reload|reload|condrestart|try-restart|status}"
	exit 1
	;;
esac

exit $RETVAL
