#! /bin/sh
#
# bdii system startup script
#
# Original version David Groep, NIKHEF
#
# Restructured by L.Field (2004-01-22)
#
# Restructured by L.Field (2005-02-11)
#
# Restructured by M.Litmaath (2005-06-09)
#
# $Id: bdii,v 1.3 2008/11/12 14:41:22 lfield Exp $
#
# chkconfig: 345 95 5
#
# description: BDII
#
# config: /opt/bdii/etc/bdii.conf
#
if [ -f /etc/rc.d/init.d/functions ]; then
    . /etc/rc.d/init.d/functions
else
    if [ -f /etc/init.d/functions ]; then
	. /etc/init.d/functions
    else
	echo "Error: Cannot find standard functions." 1>&2

	failure(){
	    echo "        [FAIL]"
	} 

	sucess(){
	    echo "        [OK]"
	}
    fi
fi

BDII_CONF=${BDII_CONF:-/opt/bdii/etc/bdii.conf}

if [ -f "$BDII_CONF" ]; then
    . "$BDII_CONF"
else
    echo "Configuration file $BDII_CONF not found ..." 1>&2
    exit 1
fi

if [ "x${BDII_DIR}" == "x" ]; then
    echo "Error: BDII_DIR is not set." 
    exit 1
fi

case ${BDII_DIR} in
/?*/?*)
    # looks possible
    ;;
*)
    echo "Error: BDII_DIR does not have the required number of subdirectories." 1>&2
    exit 1
esac

if [ "x${BDII_VAR_DIR}" == "x" ]; then
    echo "Error: BDII_VAR_DIR is not set." 
    exit 1
fi

case ${BDII_VAR_DIR} in
/?*/?*)
    # looks possible
    ;;
*)
    echo "Error: BDII_VAR_DIR does not have the required number of subdirectories." 1>&2
    exit 1
esac

PID_FILE=${BDII_VAR_DIR}/bdii-update.pid

case "$1" in
start)

    #Check to see if the process is already started
    if [  -f "$PID_FILE" ] ; then
	$0 status > /dev/null
	result=$?
	if [ $result = 2 ]; then
	    echo "Stale lock file, removing ..."
	    rm -f "$PID_FILE"
	else
	    failure
	    echo "BDII already running" 1>&2
	    exit 1
	fi
    fi

    pass=`mkpasswd -s 0 2> /dev/null` || pass=$RANDOM$RANDOM
    for PORT in $BDII_PORTS_WRITE; do

	umask 022

        #Make the database directories.
	dir=${BDII_VAR_DIR}/${PORT}
	rm -rf "$dir"
	mkdir -p "$dir"
	mkdir -p "$dir/infosys"

	conf=$dir/bdii-slapd.conf

	cp ${BDII_DIR}/etc/DB_CONFIG $dir
	cp ${BDII_DIR}/etc/DB_CONFIG $dir/infosys

	# ensure the configuration file is not world-readable,
	# as it contains the slapd database password

	(umask 077; > $conf)

	if [ ! -f ${BDII_SLAPD_CONF} ]; then
            failure
            echo "Can not file slapd file: ${BDII_SLAPD_CONF}" 1>&2
            exit 1
	fi

	cat ${BDII_SLAPD_CONF} | sed "s/secret/${pass}/g" | sed "s#db_dir#${dir}#g"> $conf
	
        # Touch the pid files.
        touch "$dir/slapd.pid"
    done

    # provide proxy for queries to secure services
    ${BDII_DIR}/sbin/bdii-proxy ${BDII_CONF}

    cat <<EOF > /etc/cron.d/bdii-proxy
37 5,11,17,23 * * * root ${BDII_DIR}/sbin/bdii-proxy ${BDII_CONF}
EOF

    chown -R $BDII_USER "${BDII_VAR_DIR}/" "${BDII_DIR}/etc/" 

    if test "x$GLOBUS_LOCATION" = x
    then
	f=/etc/sysconfig/globus
	test -f $f && . $f
	: ${GLOBUS_LOCATION:=/opt/globus}
	export GLOBUS_LOCATION
    fi

    echo -n "Starting BDII"

    su - $BDII_USER -c "
	export LANG=C
	export X509_USER_PROXY=${BDII_PROXY:-$BDII_VAR_DIR/hostproxy}
	test -d $GLOBUS_LOCATION && export GLOBUS_LOCATION=$GLOBUS_LOCATION
	${BDII_DIR}/sbin/bdii-update ${BDII_CONF} &
	echo \$! > $PID_FILE
    "

    if [ $? = 0 ]; then
        success
	echo
    else
        failure
	echo
    fi

    ;;

stop)

    #Check to see if the process is already stopped
    if [ ! -f "$PID_FILE" ] ; then
	failure
	echo "Stopping BDII"
	exit 1
    fi

    #Try to term all slapd processes.
    su - $BDII_USER -c 'killall -15 bdii-fwd bdii-update' 2>/dev/null
    for PORT in $BDII_PORTS_WRITE; do
	if [ -f ${BDII_VAR_DIR}/${PORT}/slapd.pid ]; then 
	    su - $BDII_USER -c "kill -15 $(cat ${BDII_VAR_DIR}/${PORT}/slapd.pid)" 2>/dev/null
	fi
    done

    rm -f "$PID_FILE"
    rm -f /etc/cron.d/bdii-proxy

    sleep 5

    #Send kill -9 to any remaining slapd processes.
    su - $BDII_USER -c 'killall -9 bdii-fwd bdii-update' 2>/dev/null
    for PORT in $BDII_PORTS_WRITE; do
	if [ -f ${BDII_VAR_DIR}/${PORT}/slapd.pid ]; then 
	    su - $BDII_USER -c "kill -9 $(cat ${BDII_VAR_DIR}/${PORT}/slapd.pid)" 2>/dev/null
	fi
    done

    RETVAL=0
    for PORT in $BDII_PORTS_WRITE; do
	if [ -f ${BDII_VAR_DIR}/${PORT}/slapd.pid ]; then 
	    RETVAL=1 
	fi
    done
    if [ ${RETVAL} -eq 0 ]; then
	success
	echo "Stopping BDII"
    else
	failure
	echo "Stopping BDII"
    fi

    ;;

status)

    if [ ! -f "$PID_FILE" ] ; then
        echo "`basename $0` stopped" 1>&2
        exit 1
    fi

    PID=`cat "$PID_FILE"`
    ps=`ps $PID`

    case "$ps" in
    *bdii-update*)
	echo "`basename $0` OK"
	exit 0
    esac

    echo "lock file exists but PID $PID died" 1>&2
    exit 2

    ;;

restart)
    $0 stop
    $0 start
    ;;

condrestart)
    if [ -f "$PID_FILE" ] ; then
	$0 stop
	$0 start
    fi
    ;;

*)
    echo "Usage: $0 {start|stop|restart|condrestart|status}"
    ;;
esac

exit
