#! /bin/bash
#
# 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)
#
# Restructured by Anders Waananen (2009-07-15)
#
# $Id: bdii,v 1.3 2008/11/12 14:41:22 lfield Exp $
#
# chkconfig: 2345 95 5
#
# description: BDII
#
# config: /etc/bdii4/bdii.conf
#
### BEGIN INIT INFO
# Provides:          bdii4
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: BDII
# Description:       Berkeley Database Information Index
### END INIT INFO

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
	failure(){
	    echo "        [FAIL]"
	} 

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

RUN=yes

if [ -r /etc/default/bdii4 ] ; then
    . /etc/default/bdii4
fi
if [ -r /etc/sysconfig/bdii4 ] ; then
    . /etc/sysconfig/bdii4
fi

if [ "x$RUN" != "xyes" ] ; then
    echo "bdii4 disabled, please adjust the configuration to your needs "
    echo "and then set RUN to 'yes' in /etc/default/bdii4 to enable it."
    exit 0
fi

BDII_CONF=${BDII_CONF:-/etc/bdii4/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

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

if [ ! -d "${BDII_VAR_DIR}" ]; then
    mkdir -p "${BDII_VAR_DIR}"
    if [ ! -d "${BDII_VAR_DIR}" ]; then
	echo "Error: Can not create directory ${BDII_VAR_DIR}"
	exit 1
    fi
fi

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

# For SELinux we need to use 'runuser' not 'su'
if [ -x /sbin/runuser ]
then
    SU=runuser
else
    SU=su
fi

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 /etc/bdii4/DB_CONFIG $dir
	cp /etc/bdii4/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"

        chown -R $BDII_USER $dir
    done

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

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

    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/bdii4-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 bdii4-fwd bdii4-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/bdii4-proxy

    sleep 5

    #Send kill -9 to any remaining slapd processes.
    $SU - $BDII_USER -c 'killall -9 bdii4-fwd bdii4-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
    *bdii4-update*)
	echo "`basename $0` OK"
	exit 0
    esac

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

    ;;

reload)
    ;;

restart | force-reload)
    $0 stop
    $0 start
    ;;

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

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

exit
