# Init file for the NorduGrid/ARC Information System
#
# chkconfig: 2345 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-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: NorduGrid/ARC Information system
# Description:       LDAP based grid information system for the ARC middleware
### END INIT INFO

# Helper functions
if [ -f /etc/init.d/functions ]; then
    . /etc/init.d/functions
    log_success_msg() {
	echo -n "$@"
	success "$@"
	echo
    }
    log_warning_msg() {
	echo -n "$@"
	warning "$@"
	echo
    }
    log_failure_msg() {
	echo -n "$@"
	failure "$@"
	echo
    }
elif [ -f /lib/lsb/init-functions ]; then
    . /lib/lsb/init-functions
else
    echo "Error: Cannot source neither init.d nor lsb functions"
    exit 1
fi

# Overall defaults
RETVAL=0
prog=grid-infosys
debug=${debug:-0}
RUN=yes

# sysconfig files
if [ -r /etc/sysconfig/nordugrid ]; then
    . /etc/sysconfig/nordugrid
elif [ -r /etc/default/nordugrid ]; then
    . /etc/default/nordugrid
fi
if [ -r /etc/sysconfig/grid-infosys ]; then
    . /etc/sysconfig/grid-infosys
elif [ -r /etc/default/grid-infosys ]; then
    . /etc/default/grid-infosys
fi

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

debug_echo () {
    if [ $debug -gt 0 ]; then
	info_string="`date +'%b %d %H:%M:%S'` `hostname` `basename $0`[$$]"
	echo "${info_string} $*" >> ${infosyslog}
    fi
}

error_echo () {
    info_string="`date +'%b %d %H:%M:%S'` `hostname` `basename $0`[$$]"
    echo "${info_string} $*" >> ${infosyslog}
}

std_header () {
    debug_echo "Creating file: $1"
    echo "# This file was automatically generated by the $prog startup script." > $1
    echo "# Do not modify." >> $1
    echo >> $1
}

printregldif () {

    cat <<-EOF

	# Registration "$rootdn" -> "$targetsuffix"
	dn: Mds-Vo-Op-name=register, $targetsuffix
	regtype: mdsreg2
	reghn: $reghn
	regport: $regport
	regperiod: $regperiod
	type: ldap
	hn: $hn
	port: $port
	rootdn: $rootdn
	ttl: $ttl
	timeout: $timeout
	mode: cachedump
	cachettl: $cachettl
	bindmethod: $bindmethod
	EOF
}

# Function checks if we are using a nordugrid-patched bdii Returns
# true (1) if bdii is unpatched. Returns false (0) if it is patched.
# In case it is patched, we should disable o=infosys as default.  If
# it is not patched, then keep o=infosys since plain bdii does not
# support turning this feature off.
check_if_bdii_unpatched () {
    if [ $# != 1 ]; then
	error_echo "check_bdii called with too few arguments, wanted bdii_update_file_path"
	exit 1
    fi
    bdii_update_file=$1
    grep -q "bdii_infosys" $bdii_update_file
    if [ $? -eq 0 ]; then
	debug_echo "You are using a patched version of BDII."
	debug_echo "Will default to turning off o=infosys unless specifically handled in arc.conf"
	return 0
    else
	debug_echo "Warning: You are using a pure version of BDII."
	debug_echo "Will not try to turn o=infosys off since this is not supported."
	return 1
    fi
}

###############  Standard configuration  ######################

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

# source the config parsing routines
if [ -r "$ARC_LOCATION/share/arc/config_parser_compat.sh" ]; then
    . $ARC_LOCATION/share/arc/config_parser_compat.sh || exit $?
else
    log_failure_msg "Could not find $ARC_LOCATION/share/arc/config_parser_compat.sh"
    exit 1
fi

ARC_CONFIG=${ARC_CONFIG:-/etc/arc.conf}

if [ ! -r "$ARC_CONFIG" ]; then
    echo "ARC configuration file arc.conf was not found (usually /etc/arc.conf)"
    if [ -r "/etc/nordugrid.conf" ]; then
	echo "Fallback /etc/nordugrid.conf was found, trying to use that one"
	ARC_CONFIG="/etc/nordugrid.conf"
    else
	log_warning_msg "arc.conf was not found. If this file is in a non-standard place,"
	log_warning_msg "  it can be set with the ARC_CONFIG environment variable"
	exit 0
    fi
fi

# Check for OpenLDAP bug existing on some RHEL4 installations and warn about it
if [ -s /etc/openldap/ldap.conf ]; then
    TLS_CACERTDIR=`cat /etc/openldap/ldap.conf | grep -E "^TLS_CACERTDIR" | sed -e 's/TLS_CACERTDIR[ \t]*//g'`
    if [ "x${TLS_CACERTDIR}" != "x" ] && [ ! -d ${TLS_CACERTDIR} ]; then
	log_warning_msg "TLS_CACERTDIR=${TLS_CACERTDIR} is set in /etc/openldap/ldap.conf but directory does not exist"
	log_warning_msg "This is a bug in OpenLDAP, please create the directory"
	exit 0
    fi
fi

config_set_defaults () {
    CONFIG_hostname=`hostname`
    CONFIG_port="2135"
    CONFIG_provider_loglevel=1
}

# Read arc.conf
export pkgdatadir=$ARC_LOCATION/share/arc
config_parse_file $ARC_CONFIG || exit $?

# Check for infosys-block
if ! config_match_section infosys; then
    log_warning_msg "Missing infosys configuration block"
    exit 0
fi

config_hide_all
config_set_defaults
config_import_section common

# These options need to come from the infosys-block, not from common
unset CONFIG_logfile
unset CONFIG_user
unset CONFIG_port

config_import_section infosys

debug=${CONFIG_debug:-$debug}

check_ownership () {
    if [ $# != 2 ]; then
	error_echo "Wrong number of arguments to check_ownership"
	exit 1
    fi
    file=$1
    user=$2

    res=$(find `dirname $file`/ -maxdepth 1 -user $user -name `basename $file`)
    if [ "x${res}" = "x" ]; then
	log_failure_msg "ERROR: ${file} is not owned by ${user}"
	exit 1
    fi
}

get_ldap_user () {
    ldap_user=`getent passwd | grep ldap | sed 's/\([az]*\):.*/\1/g'`
    if [ ! "xldap" = "x$ldap_user" ] && [ ! "xopenldap" = "x$ldap_user" ]; then
	debug_echo "Warning, could not find ldap or openldap user"
	debug_echo "resorting to using the root user."
	ldap_user=root
    fi
}

# These values may be set in arc.conf, otherwise use sensible defaults

slapd_loglevel=${CONFIG_slapd_loglevel:-0}
hostname_f=$(hostname -f)
hostname=${CONFIG_hostname:-$hostname_f}
slapd_hostnamebind=${CONFIG_slapd_hostnamebind:-$hostname}
threads=${CONFIG_threads:-32}
providerlog=${CONFIG_providerlog:-/var/log/arc/infoprovider.log}
infosyslog=${CONFIG_logfile:-/var/log/arc/$prog.log}

mkdir -p `dirname $providerlog`
mkdir -p `dirname $infosyslog`

# Get ldap user from passwd
get_ldap_user
bdii_user=${CONFIG_user:-$ldap_user}

bdii_location=${CONFIG_bdii_location:-/usr}
giis_location=${CONFIG_giis_location:-$ARC_LOCATION}

bdii_tmp_dir=${CONFIG_bdii_tmp_dir:-/var/tmp/bdii4}
bdii_var_dir=${CONFIG_bdii_var_dir:-/var/run/bdii4}
bdii_log_dir=${CONFIG_bdii_log_dir:-/var/log/arc/bdii4}

mkdir -p $bdii_log_dir

bdii_slapd_conf=$bdii_var_dir/arc-slapd.conf

bdii_update_cmd=${CONFIG_bdii_update_cmd:-"${bdii_location}/sbin/bdii4-update"}
if [ ! -e $bdii_update_cmd ]; then
    log_failure_msg "Can not find bdii4-update command at: $bdii_update_cmd."
    log_failure_msg "Please set bdii_update_cmd in arc.conf"
    exit 1
fi

bdii_default_ldif_ng=${bdii_tmp_dir}/arc-default.ldif.pl

bdii_port_read=${CONFIG_port:-2135}
bdii_ports_write="$(( $bdii_port_read + 1 )) $(( $bdii_port_read + 2 ))"

bdii_bind="o=Grid"
bdii_search_filter='*'
bdii_auto_update=${CONFIG_bdii_auto_update:-no}
bdii_auto_modify=${CONFIG_bdii_auto_modify:-no}
bdii_modify_dn=${CONFIG_bdii_modify_dn:-no}
bdii_is_cache=${CONFIG_bdii_is_cache:-yes}
bdii_update_url=${CONFIG_bdii_update_url:-"http://"}
bdii_update_ldif=${CONFIG_bdii_update_ldif:-"http://"}
bdii_database=${CONFIG_bdii_database:-"bdb"}

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

infosys_compat=${CONFIG_infosys_compat:-"disable"}
infosys_nordugrid=${CONFIG_infosys_nordugrid:-"enable"}
infosys_glue12=${CONFIG_infosys_glue12:-"disable"}
infosys_glue2_ldap=${CONFIG_infosys_glue2_ldap:-"disable"}

provider_timeout=${CONFIG_provider_timeout:-300}
gm_wakeupperiod=$(config_print_option grid-manager wakeupperiod)
gm_wakeupperiod=${gm_wakeupperiod:-120}

if [ "x$infosys_compat" = "xenable" ]; then
    bdii_breathe_time=${CONFIG_bdii_breathe_time:-30}
    bdii_search_timeout=${CONFIG_bdii_search_timeout:-300}
else
    # The infoprovider does the waiting, no need for BDII to do it too.  Use
    # some small timeout to protect the system in case there is a problem with
    # the provier
    bdii_breathe_time=${CONFIG_bdii_breathe_time:-10}

    # $provider_timeout refers to a-rex's infoprovider. BDII must also include
    # the wait time till the next info collection cycle of a-rex.
    max_cycle=$(( $provider_timeout + $gm_wakeupperiod ))
    bdii_search_timeout=${CONFIG_bdii_search_timeout:-$max_cycle}
fi

# o=infosys should be disabled by default if possible.
infosys_debug=${CONFIG_infosys_debug:-"disable"}
bdii_infosys="no"
if [ "x$infosys_debug" = "xenable" ]; then
    bdii_infosys="yes"
fi

# Check directories and permissions
registrationlog=${CONFIG_registrationlog:-/var/log/arc/inforegistration.log}
mkdir -p `dirname $registrationlog`

touch ${registrationlog}
chown ${bdii_user} ${registrationlog}

touch ${providerlog}
chown ${bdii_user} ${providerlog}

chown ${bdii_user} ${bdii_log_dir}

arc_runtime_config="/var/run/arc/infosys"
mkdir -p ${arc_runtime_config}
chown ${bdii_user} ${arc_runtime_config}

arc_runtime_exec="/var/tmp/arc/infosys"
mkdir -p ${arc_runtime_exec}

check_ownership ${arc_runtime_config} ${bdii_user}
check_ownership ${arc_runtime_exec} ${bdii_user}
check_ownership ${registrationlog} ${bdii_user}
check_ownership ${bdii_log_dir} ${bdii_user}
check_ownership ${providerlog} ${bdii_user}

# If the new code path is selected...
if [ "x$infosys_nordugrid" = "xenable" ] || [ "x$infosys_glue12" = "xenable" ];
 then
    if [ "x$infosys_compat" != "xenable" ] && [ ! -f "$pkgdatadir/InfosysHelper.pm" ]; then
	log_warning_msg "A-REX is not installed"
	log_warning_msg "For operation without A-REX, disable publishing of cluster information"
	log_warning_msg "(infosys_nordugrid, infosys_glue12 and infosys_glue2_ldap)"
	log_warning_msg "or enable infosys_compat in configuration"
	exit 0
    fi
fi

if [ "x$infosys_glue2_ldap" = "xenable" ]; then
    log_failure_msg "BDII4 is not compatible with GLUE2. BDII >= 5.1.19 is required"
    exit 1
fi

# Check for existance of core ldap schema
coreschema=$(find /etc/ -name core.schema -printf "%h/%f\n" 2>/dev/null)
if [ "x" = "x$coreschema" ]; then
    log_failure_msg "Error, could not find ldap core schema file under /etc"
    exit 1
fi

# Check for existence of a system ldap, this command will be used by bdii
slapd_cmd=""
if [ "x" = "x$CONFIG_slapd" ]; then
    O_IFS=$IFS
    IFS=:
    for dir in $PATH; do
	if [ -x "$dir/slapd" ]; then
	    slapd_cmd="$dir/slapd"
	    break
	fi
    done
    IFS=$O_IFS
else
    slapd_cmd=$CONFIG_slapd
fi

#This is the slapd command that should be used by bdii
if [ "`config_subsections infosys/index`" ]; then
    if [ -r ${giis_location}/lib64/arc/arc-infoindex-slapd-wrapper.so ]; then
	pkglibdir=${giis_location}/lib64/arc
    elif [ -r ${giis_location}/lib/arc/arc-infoindex-slapd-wrapper.so ]; then
	pkglibdir=${giis_location}/lib/arc
    else
	error_echo "Error, could not find infoindex slapd wrapper"
	exit 1
    fi
    rm -f ${arc_runtime_exec}/slapd
    cat <<-EOF > ${arc_runtime_exec}/slapd
	#!/bin/sh
	LD_PRELOAD=${pkglibdir}/arc-infoindex-slapd-wrapper.so setsid ${slapd_cmd} -d 0 "\$@" </dev/null 1>/dev/null 2>/dev/null &
	sid=\$!
	dir=\$(dirname \$2)
	echo \$sid > \$dir/slapd.pid
	EOF
    chmod +x ${arc_runtime_exec}/slapd
    bdii_slapd_cmd=${arc_runtime_exec}/slapd
else
    bdii_slapd_cmd=${slapd_cmd}
fi

if [ ! -x $slapd_cmd ]; then
    log_failure_msg "Could not find ldap server binary, usually /usr/bin/slapd"
    log_failure_msg "Please update your PATH-variable."
    exit 1
fi


# If gris modules are not compiled into slapd, we will need to add variables to the config-file.
if [ $(grep -Ec "${bdii_database}_db_init|${bdii_database}_back_db_init" "$slapd_cmd") -eq 0 ]; then

    # Guess a likely directory for ldap modules
    ldaplib=$(find /usr/lib -name "back_${bdii_database}.so" -printf "%h/\n" 2>/dev/null)
    if [ "x" = "x$ldaplib" ]; then
	log_failure_msg "Could not find ldap libraries under /usr/lib"
	exit 1
    fi

    # This will get written to bdii slapd.conf:
    slapd_modulepath="modulepath      $ldaplib"
    slapd_moduleload="moduleload      back_${bdii_database}"
fi

# If giis modules are not compiled into slapd, we will need to add variables to the config-file.
if [ "`config_subsections infosys/index`" ]; then
    if [ $(grep -Ec "shell_db_init|shell_back_db_init" "$slapd_cmd") -eq 0 ]; then

	# Guess a likely directory for ldap modules
	ldaplib=${ldaplib:-$(find /usr/lib -name "back_shell.so" -printf "%h/\n" 2>/dev/null)}
	if [ "x" = "x$ldaplib" ]; then
	    log_failure_msg "Could not find ldap libraries under /usr/lib"
	    exit 1
	fi

	# This will get written to bdii slapd.conf:
	slapd_modulepath="modulepath      $ldaplib"
	slapd_moduleload_index="moduleload      back_shell"
    fi
fi

slapadd=${CONFIG_slapadd:-/usr/sbin/slapadd}

if [ ! -x $slapadd ]; then
    error_echo "slapadd command was not found"
    exit 1
fi

# If we are running on an unpatched BDII, handle it gracefully by
# having o=infosys enabled.
check_if_bdii_unpatched $bdii_update_cmd
if [ $? -gt 0 ]; then
    infosys_debug=enable
    bdii_infosys=yes
fi

resource_location=""
resource_latitude=""
resource_longitude=""
cpuscalingreferencesi00=""
processorotherdescription=""
gluesiteweb=""
gluesiteuniqueid=""
provide_glue_site_info="true"

if [ "x$infosys_glue12" = "xenable" ]; then

    if ! config_match_section infosys/glue12 ; then
	log_failure_msg "infosys_glue12 is set to enable, but infosys/glue12 block is missing."
	exit 1
    fi

    config_import_section infosys/glue12
    resource_location=${CONFIG_resource_location}
    resource_latitude=${CONFIG_resource_latitude}
    resource_longitude=${CONFIG_resource_longitude}
    cpuscalingreferencesi00=${CONFIG_cpu_scaling_reference_si00}
    processorotherdescription=${CONFIG_processor_other_description}
    gluesiteweb=${CONFIG_glue_site_web}
    gluesiteuniqueid=${CONFIG_glue_site_unique_id}
    provide_glue_site_info=${CONFIG_provide_glue_site_info:-$provide_glue_site_info}

    if [ "x$resource_location" = "x" ]; then
	log_failure_msg "If infosys_glue12 is enabled, then resource_location must be set."
	log_failure_msg "It should be set to a free-form string describing the location,"
	log_failure_msg "for example: 'Kastrup, Denmark'"
	exit 1
    fi
    if [ "x$resource_latitude" = "x" ]; then
	log_failure_msg "If infosys_glue12 is enabled, then resource_latitude must be set."
	log_failure_msg "It should be set to the latitude for the location,"
	log_failure_msg "for example: '55.75000'"
	exit 1
    fi
    if [ "x$resource_longitude" = "x" ]; then
	log_failure_msg "If infosys_glue12 is enabled, then resource_longitude must be set."
	log_failure_msg "It should be set to the longitude for the location,"
	log_failure_msg "for example: '12.41670'"
	exit 1
    fi
    if [ "x$cpuscalingreferencesi00" = "x" ]; then
	log_failure_msg "If infosys_glue12 is enabled, then cpu_scaling_reference_si00 must be set."
	log_failure_msg "It should be set to the SI00 value,"
	log_failure_msg "for example: '2400'"
	exit 1
    fi
    if [ "x$processorotherdescription" = "x" ]; then
	log_failure_msg "If infosys_glue12 is enabled, then processor_other_description must be set."
	log_failure_msg "It should be set to a value like in the example,where cores is the average number"
	log_failure_msg "of cores in the machine"
	log_failure_msg "for example: 'Cores=3,Benchmark=9.8-HEP-SPEC06'"
	exit 1
    fi
    if [ "x$gluesiteweb" = "x" ]; then
	log_failure_msg "If infosys_glue12 is enabled, then glue_site_web must be set."
	log_failure_msg "It should be set to a url for the website belonging to the institution holding the resource,"
	log_failure_msg "for example: 'http://www.ndgf.org'"
	exit 1
    fi
    if [ "x$gluesiteuniqueid" = "x" ]; then
	log_failure_msg "If infosys_glue12 is enabled, then glue_site_unique_id must be set."
	log_failure_msg "It should be set to a unique id to the resource, this should be entered into the GocDb"
	log_failure_msg "for example: 'NDGF-T1'"
	exit 1
    fi

fi

# bdii-update.conf is put into nordugrid runtime directory
bdii_update_conf=${CONFIG_bdii_update_conf:-$arc_runtime_config/bdii-update.conf}
# bdii.conf is put into nordugrid runtime directory
# together with other temporary files, needs to be exported so bdii can start
BDII_CONF=${CONFIG_bdii_conf:-${arc_runtime_config}/bdii.conf}
export BDII_CONF

giis_fifo=${CONFIG_giis_fifo:-$arc_runtime_config/giis-fifo}

# Call with: create_bdii_conf bdii_dir var_dir slapd_conf default_ldif port_read ports_write
# Will create bdii.conf file in /var/run/arc/infosys/bdii.conf
create_bdii_conf () {
    if [ $# != 7 ]; then
	error_echo "Wrong number of arguments to create_bdii_conf"
	error_echo "Arguments were: $*"
	exit 1
    fi
    bdii_location=$1
    bdii_var_dir=$2
    bdii_slapd_conf=$3
    bdii_default_ldif=$4
    bdii_port_read=$5
    bdii_ports_write="$6 $7"

    rm -f ${BDII_CONF}
    std_header ${BDII_CONF}
    cat <<-EOF >> ${BDII_CONF}
	BDII_DIR=$bdii_location
	BDII_VAR_DIR=$bdii_var_dir
	BDII_LOG_DIR=$bdii_log_dir
	BDII_UPDATE_CONF=$bdii_update_conf
	BDII_SLAPD_CONF=$bdii_slapd_conf
	BDII_DEFAULT_LDIF=$bdii_default_ldif

	BDII_PORT_READ=$bdii_port_read
	BDII_PORTS_WRITE="$bdii_ports_write"
	BDII_USER=$bdii_user
	BDII_BIND=$bdii_bind
	BDII_SEARCH_FILTER=$bdii_search_filter
	BDII_SEARCH_TIMEOUT=$bdii_search_timeout
	BDII_BREATHE_TIME=$bdii_breathe_time
	BDII_AUTO_UPDATE=$bdii_auto_update
	BDII_AUTO_MODIFY=$bdii_auto_modify
	BDII_MODIFY_DN=$bdii_modify_dn
	BDII_IS_CACHE=$bdii_is_cache
	BDII_UPDATE_URL=$bdii_update_url
	BDII_UPDATE_LDIF=$bdii_update_ldif
	BDII_INFOSYS=$bdii_infosys
	SLAPD=$bdii_slapd_cmd
	SLAPADD=$slapadd
	SLAPD_DEBUG_LEVEL=0
	EOF
}

# Call with: create_bdii_update_conf bdii_dir ldif_generator_file_ng ldif_generator_file_glue
# Will create file /var/run/nordugrid/bdii-update.conf
create_bdii_update_conf () {
    if [ $# != 3 ]; then
	error_echo "Wrong number of arguments to create_bdii_update_conf"
	error_echo "Arguments were: $*"
	exit 1
    fi
    bdii_location=$1
    ldif_generator_file_ng=$2
    ldif_generator_file_glue=$3

    rm -f ${bdii_update_conf}
    std_header ${bdii_update_conf}

    if [ "x$infosys_nordugrid" = "xenable" ] || [ "x$infosys_compat" != "xenable" ]; then
	echo "ARC-PROVIDER file://${ldif_generator_file_ng}" >> \
	    ${bdii_update_conf}
    fi
    if [ "x$infosys_glue12" = "xenable" ] && [ "x$infosys_compat" = "xenable" ]; then
	echo "GLUE-PROVIDER file://${ldif_generator_file_glue}" >> \
	    ${bdii_update_conf}
    fi
}

# Call with: create_arc_slapd_conf bdii_dir bdii_slapd_conf_file
# Will create file $bdii_slapd_conf, normally located at:
# $bdii_var_dir/arc-slapd.conf
create_arc_slapd_conf () {
    if [ $# != 2 ]; then
	error_echo "Wrong number of arguments to create_arc_slapd_conf"
	error_echo "Arguments were: $*"
	exit 1
    fi
    bdii_location=$1
    bdii_slapd_conf=$2

    for i in "/etc/bdii4/BDII.schema" \
	     "${bdii_location}/etc/BDII.schema" \
	     "$ARC_LOCATION/share/arc/ldap-schema/BDII.schema"; do

	if [ -r $i ]; then
	    bdii_schema=$i
	    break
	fi
    done

    rm -f $bdii_slapd_conf

    # ensure the configuration file is not world-readable,
    # as it contains the slapd database password
    (umask 077; > $bdii_slapd_conf)
    std_header $bdii_slapd_conf
    cat <<-EOF >> $bdii_slapd_conf
	include ${coreschema}

	#bdii specific schemas
	include $bdii_schema

	#nordugrid specific schemas
	include $ARC_LOCATION/share/arc/ldap-schema/nordugrid.schema
	#glue schemas
	include /etc/ldap/schema/Glue-CORE.schema
	include /etc/ldap/schema/Glue-CE.schema
	include /etc/ldap/schema/Glue-CESEBind.schema
	##glue2 schema
	#include /etc/ldap/schema/GLUE20.schema

	$slapd_modulepath
	$slapd_moduleload
	$slapd_moduleload_index

	allow bind_v2

	pidfile         db_dir/slapd.pid
	argsfile        db_dir/slapd.args
	loglevel        $slapd_loglevel
	threads         $threads
	idletimeout     120
	sizelimit       unlimited
	timelimit       2400
	EOF
}

add_info_service () {
    if [ $# != 2 ]; then
	error_echo "Wrong number of arguments to add_info_service"
	error_echo "Arguments were: $*"
	exit 1
    fi
    suffix=$1
    rootdn=$2
    cat <<-EOF >> $bdii_slapd_conf

	# ${bdii_database} database definitions for $rootdn
	database        ${bdii_database}
	cachesize       150000
	dbnosync
	suffix          "$suffix"
	rootdn          "$rootdn"
	rootpw          secret
	readonly        yes
	directory       db_dir

	## ${bdii_database} database definitions for o=glue
	#database        ${bdii_database}
	#cachesize       150000
	#dbnosync
	#suffix          "o=glue"
	#rootdn          "o=glue"
	#rootpw          secret
	#readonly        yes
	#directory       db_dir/glue2
	EOF

    if [ "x$infosys_debug" = "xenable" ]; then
    cat <<-EOF >> $bdii_slapd_conf
	# Infosys database definitions
	database        ${bdii_database}
	cachesize       150000
	dbnosync
	suffix          "o=infosys"
	rootdn          "o=infosys"
	rootpw          secret
	readonly        yes
	directory       db_dir/infosys
	EOF
    fi
}

# Call with: create_default_ldif bdii_default_ldif_file
# Will create file $bdii_default_ldif, normally located at:
# ${bdii_tmp_dir}/arc-default.ldif.pl
create_default_ldif () {
    if [ $# != 1 ]; then
	error_echo "Wrong number of arguments to create_default_ldif"
	error_echo "Arguments were: $*"
	exit 1
    fi

    bdii_default_ldif=$1

    debug_echo Creating file: $bdii_default_ldif

    cat <<-EOF > $bdii_default_ldif
	#!/usr/bin/perl

	# This file was automatically generated by $prog startup script.
	# Do not modify.

	use POSIX;

	print "\n";
	print "dn: o=Grid\n";
	print "objectClass: organization\n";
	print "o: Grid\n";
	print "\n";

	print "dn: Mds-Vo-name=local,o=Grid\n";
	print "objectClass: Mds\n";
	print "objectClass: MdsVo\n";
	print "Mds-Vo-name: local\n";
	print "Mds-validfrom: " . strftime("%Y%m%d%H%M%SZ\n", gmtime());
	print "Mds-validto: " . strftime("%Y%m%d%H%M%SZ\n", gmtime(time() + 3600));
	print "\n";

	print "dn: Mds-Vo-name=resource,o=Grid\n";
	print "objectClass: Mds\n";
	print "objectClass: MdsVo\n";
	print "Mds-Vo-name: resource\n";
	print "Mds-validfrom: " . strftime("%Y%m%d%H%M%SZ\n", gmtime());
	print "Mds-validto: " . strftime("%Y%m%d%H%M%SZ\n", gmtime(time() + 3600));
	print "\n";

	#print "dn: o=glue\n";
	#print "objectClass: top\n";
	#print "objectClass: organization\n";
	#print "o: glue\n";
	EOF

    chmod +x $bdii_default_ldif
}

# Call with create_arc_ldif_generator_compat bdii_tmp_dir ldif_generator_file_ng
# Will create $ldif_generator_file_ng, normally located at:
# ${bdii_tmp_dir}/arc-nordugrid-bdii-ldif
create_arc_ldif_generator_compat () {
    if [ $# != 2 ]; then
	error_echo "Wrong number of arguments to create_arc_ldif_generator_compat"
	error_echo "Arguments were: $*"
	exit 1
    fi

    bdii_tmp_dir=$1
    ldif_generator_file=$2

    rm -f ${ldif_generator_file}
    touch ${ldif_generator_file}
    std_header ${ldif_generator_file}

    if config_match_section cluster; then
	(
	    config_hide_all
	    config_set_defaults
	    config_import_section common
	    config_import_section infosys
	    config_import_section cluster
	    cachetime=${CONFIG_cachetime:-$bdii_breathe_time}

	    cat <<-EOF >> ${ldif_generator_file}
		$ARC_LOCATION/share/arc/cluster.pl --valid-to $cachetime --config $ARC_CONFIG --loglevel $CONFIG_provider_loglevel --dn nordugrid-cluster-name=$hostname,Mds-Vo-name=local,o=Grid 2>> "$providerlog"
		EOF

	    # Create entries for all queues
	    for queue in `config_subsections queue`; do
		(
		    config_import_section queue/$queue
		    cachetime=${CONFIG_cachetime:-$bdii_breathe_time}

		    cat <<-EOF >> ${ldif_generator_file}
			$ARC_LOCATION/share/arc/qju.pl --valid-to $cachetime --config $ARC_CONFIG --loglevel $CONFIG_provider_loglevel --dn nordugrid-queue-name=$queue,nordugrid-cluster-name=$hostname,Mds-Vo-name=local,o=Grid --queue $queue 2>> "$providerlog"
			EOF
		)
	    done
	)
    fi

    for se in `config_subsections se`; do
	(
	    config_hide_all
	    config_set_defaults
	    config_import_section common
	    config_import_section infosys
	    config_import_section se/$se
	    cachetime=${CONFIG_cachetime:-$bdii_breathe_time}

	    cat <<-EOF >> ${ldif_generator_file}
		$ARC_LOCATION/share/arc/se.pl -valid-to $cachetime -config $ARC_CONFIG -dn nordugrid-se-name=$se:$hostname,Mds-Vo-name=local,o=Grid -se $se 2>> "$providerlog"
		EOF
	)
    done

    chmod +x ${ldif_generator_file}
}

# Call with create_arc_ldif_generator arc_runtime_config ldif_generator_file_ng ldif_generator_file_glue
# Will create $ldif_generator_file, normally located at:
# ${bdii_tmp_dir}/arc-nordugrid-bdii-ldif
create_arc_ldif_generator () {
    if [ $# != 3 ]; then
	error_echo "Wrong number of arguments to create_arc_ldif_generator"
	error_echo "Arguments were: $*"
	exit 1
    fi

    arc_runtime_config=$1
    ldif_generator_file=$2
    ldif_glue12_generator=$3

    ldif_script="$arc_runtime_config/ldif-provider.sh"

    rm -f ${ldif_generator_file}
    touch ${ldif_generator_file}

    cat <<-EOF > ${ldif_generator_file}
	#!/usr/bin/perl

	# This file was automatically generated by the $prog startup script.
	# Do not modify.
	EOF

    for se in `config_subsections se`; do
	(
	    config_hide_all
	    config_set_defaults
	    config_import_section common
	    config_import_section infosys
	    if [ ! -f $providerlog ]; then
		touch $providerlog
	    fi
	    chown ${bdii_user}:${bdii_user} ${providerlog}
	    config_import_section se/$se
	    cachetime=${CONFIG_cachetime:-$max_cycle}

	    cat <<-EOF >> ${ldif_generator_file}
			BEGIN {
			    print "\n";
			    system('$ARC_LOCATION/share/arc/se.pl -valid-to $cachetime -config $ARC_CONFIG -dn nordugrid-se-name=$se:$hostname,Mds-Vo-name=local,o=Grid -se $se 2>> "$providerlog"');
			}
			EOF
	)
    done

    # NG and GLUE2 come directly from a-rex infoprovider
    cat <<-EOF >> ${ldif_generator_file}

	BEGIN { unshift @INC, '$pkgdatadir'; }
	use InfosysHelper;
	exit 1 unless InfosysHelper::ldifIsReady('$arc_runtime_config', '$max_cycle', 1);
	EOF

    if [ "x$infosys_nordugrid" = "xenable" ] || [ "x$infosys_glue2_ldap" = "xenable" ]; then
	echo "system('$ldif_script');" >> ${ldif_generator_file}
    fi

    if [ "x$infosys_glue12" = "xenable" ]; then
	# Call with create_glue_ldif_generator bdii_tmp_dir ldif_generator_file_ng ldif_generator_file_glue
	create_glue_ldif_generator $bdii_tmp_dir $ldif_script $ldif_glue12_generator
	echo "system('$ldif_glue12_generator');" >> ${ldif_generator_file}
    fi

    chmod +x ${ldif_generator_file}
}

create_registration_config_file () {
    std_header $arc_runtime_config/grid-info-resource-register.conf
    (
	config_hide_all
	config_set_defaults
	config_import_section common
	config_import_section infosys

	# Start with the Cluster registration blocks
	for p in `config_subsections infosys/cluster/registration`; do
	(
	    config_import_section infosys/cluster/registration/$p

	    targetsuffix=${CONFIG_targetsuffix:-"Mds-Vo-name=$p,o=Grid"}
	    reghn=${CONFIG_targethostname:-"targethostname.not.set"}
	    regport=${CONFIG_targetport:-2135}
	    hn=${CONFIG_registranthostname:-$hostname}
	    port=${CONFIG_registrantport:-$CONFIG_port}
	    rootdn=${CONFIG_registrantsuffix:-"nordugrid-cluster-name=$hn,Mds-Vo-name=local,o=Grid"}
	    regperiod=${CONFIG_regperiod:-120}
	    ttl=${CONFIG_ttl:-$(( $regperiod * 2 ))}
	    timeout=${CONFIG_timeout:-45}
	    cachettl=${CONFIG_cachettl:-0}
	    sizelimit=${CONFIG_sizelimit:-0}
	    bindmethod=${CONFIG_bindmethod:-ANONYM-ONLY}

	    debug_echo "Cluster Tree on $hn:$port/$rootdn REGISTERS to $reghn:$regport/$targetsuffix"
	    printregldif >> $arc_runtime_config/grid-info-resource-register.conf

	)
	done

	# Now process the SE registration blocks
	for seentry in `config_subsections infosys/se`; do
	(
	    config_import_section infosys/se
	    CONFIG_name=
	    config_import_section se/$seentry
	    sename=${CONFIG_name:-$seentry}
	    config_import_section infosys/se/$seentry

	    for p in `config_subsections infosys/se/$seentry/registration`; do
	    (
		config_import_section infosys/se/$seentry/registration
		config_import_section infosys/se/$seentry/registration/$p

		targetsuffix=${CONFIG_targetsuffix:-"Mds-Vo-name=$p,o=Grid"}
		reghn=${CONFIG_targethostname:-"targethostname.not.set"}
		regport=${CONFIG_targetport:-2135}
		hn=${CONFIG_registranthostname:-$hostname}
		port=${CONFIG_registrantport:-$CONFIG_port}
		rootdn=${CONFIG_registrantsuffix:-"nordugrid-se-name=$sename:$hn,Mds-Vo-name=local,o=Grid"}
		regperiod=${CONFIG_regperiod:-120}
		ttl=${CONFIG_ttl:-$(( $regperiod * 2 ))}
		timeout=${CONFIG_timeout:-45}
		cachettl=${CONFIG_cachettl:-0}
		sizelimit=${CONFIG_sizelimit:-0}
		bindmethod=${CONFIG_bindmethod:-ANONYM-ONLY}

		debug_echo "$seentry SE Tree on $hn:$port/$rootdn REGISTERS to $reghn:$regport/$targetsuffix"
		printregldif >> $arc_runtime_config/grid-info-resource-register.conf

	    )
	    done

	)
	done # loop over SEs
    )
}

add_index_services () {
    (
	config_hide_all
	config_set_defaults
	config_import_section common
	config_import_section infosys

	# Defaults for the Index Service slapd.conf block

	access="access to * by * write"

	for vo in `config_subsections infosys/index`; do
	(
	    debug_echo "Enabling the Index Service $vo"

	    CONFIG_name=
	    config_import_section infosys/index/$vo
	    indexname=${CONFIG_name:-$vo}

	    cat <<-EOF >> $bdii_slapd_conf

		# Index Service: $vo
		database        shell
		suffix          "Mds-Vo-name=$indexname,o=Grid"
		bind            $giis_location/sbin/arc-infoindex-relay $giis_fifo
		add             $giis_location/sbin/arc-infoindex-relay $giis_fifo
		search          $giis_location/sbin/arc-infoindex-relay $giis_fifo
		$access
		EOF

	    # Registrations of the Index Services

	    # grid-info-resource-register.conf

	    for r in `config_subsections infosys/index/$vo/registration`; do
	    (
		config_import_section infosys/index/$vo/registration/$r

		targetsuffix=${CONFIG_targetsuffix:-"Mds-Vo-name=$r,o=Grid"}
		reghn=$CONFIG_targethostname
		regport=${CONFIG_targetport:-2135}
		hn=${CONFIG_registranthostname:-$hostname}
		port=${CONFIG_registrantport:-$CONFIG_port}
		rootdn=${CONFIG_registrantsuffix:-"Mds-Vo-name=$indexname,o=Grid"}
		regperiod=${CONFIG_regperiod:-120}
		ttl=${CONFIG_ttl:-$(( $regperiod * 2 ))}
		timeout=${CONFIG_timeout:-120}
		cachettl=${CONFIG_cachettl:-0}
		sizelimit=${CONFIG_sizelimit:-0}
		bindmethod=${CONFIG_bindmethod:-ANONYM-ONLY}

		if [ -n "$targetsuffix" -a -n "$reghn" ]; then
		    printregldif >> $arc_runtime_config/grid-info-resource-register.conf
		    debug_echo "Index Service $hn:$port/$rootdn REGISTERS to $reghn:$regport/$targetsuffix"
		fi

	    )
	    done

	)
	done
    )
}

# Call with create_glue_ldif_generator bdii_tmp_dir ldif_generator_file_ng ldif_generator_file_glue
# Will create $ldif_generator_file_glue, normally located at:
# ${bdii_tmp_dir}/arc-glue-bdii-ldif
create_glue_ldif_generator () {
    if [ $# != 3 ]; then
	error_echo "Wrong number of arguments to create_glue_ldif_generator"
	error_echo "Arguments were: $*"
	exit 1
    fi

    bdii_tmp_dir=$1
    ldif_generator_file_ng=$2
    ldif_generator_file_glue=$3

    rm -f ${ldif_generator_file_glue}
    touch ${ldif_generator_file_glue}

    # We use , instead of / here to allow for / in path, resource_location though, can contain commas..
    sed "s,\$LDIF_GENERATOR_FILE_NG,$ldif_generator_file_ng,g;
	 s/\$LOC/\"$resource_location\"/g;
	 s/\$LAT/$resource_latitude/g;
	 s/\$LONG/$resource_longitude/g;
	 s/\$CPUSCALINGREFERENCESI00/$cpuscalingreferencesi00/g;
	 s/\$PROCESSOROTHERDESCRIPTION/$processorotherdescription/g;
	 s,\$GLUESITEWEB,$gluesiteweb,g;
	 s,\$BDIIPORT,$bdii_port_read,g;
	 s,\$GLUESITEUNIQUEID,$gluesiteuniqueid,g;
	 s,\$PROVIDE_GLUE_SITE_INFO,$provide_glue_site_info,g;
	" $ARC_LOCATION/share/arc/glue-generator.pl > ${ldif_generator_file_glue}

    chmod +x ${ldif_generator_file_glue}
}

# Function to create a directory, removes it if it exist and then
# creates it and checks permissions.
# Call with create_directory dir context
create_directory () {
    if [ $# != 2 ]; then
	error_echo "Wrong number of arguments to create_directory"
	error_echo "Arguments were: $*"
	exit 1
    fi

    dir=$1
    string=$2
    id=`id -u`

    if [ ${#dir} -le "1" ]; then
	error_echo "$string error: directory string: $dir is too short"
	exit 1
    fi

    if [ -e ${dir} ]; then
	rm -rf $dir
    fi

    #set rights
    umask 0022
    mkdir -p ${dir}

    #check ownership just to be sure
    if [ ! -O ${dir} ]; then
	error_echo "$string error: $dir should be owned by uid:$id"
	exit 1
    fi
}

# A call to this function will create necessary config-files for bdii to be used
create_bdii_config_files () {

    # Create bdii.conf
    # Call with: create_bdii_conf bdii_dir var_dir slapd_conf default_ldif port_read ports_write
    create_bdii_conf $bdii_location $bdii_var_dir $bdii_slapd_conf $bdii_default_ldif_ng $bdii_port_read $bdii_ports_write

    # Create bdii-update.conf - only points to scripts for generating
    # ldifs, depending on the $infosys_nordugrid and $infosys_glue12 variables,
    # different lines will be written to bdii-update.conf
    # Call with: create_bdii_update_conf bdii_dir ldif_generator_file_ng ldif_generator_file_glue
    create_bdii_update_conf $bdii_location ${bdii_tmp_dir}/arc-nordugrid-bdii-ldif ${bdii_tmp_dir}/arc-glue-bdii-ldif

    # Create arc-slapd.conf, will be copied to dirs where slapd is run
    # Call with: create_arc_slapd_conf bdii_dir bdii_slapd_conf_file
    create_arc_slapd_conf $bdii_location $bdii_slapd_conf

    # Create bdii default ldif
    # Call with: create_default_ldif bdii_default_ldif_file
    create_default_ldif $bdii_default_ldif_ng

    if [ "x$infosys_compat" = "xenable" ]; then

	# Create arc-nordugrid-bdii-ldif, generates ldif-files for nordugrid schema.
	if [ "x$infosys_nordugrid" = "xenable" ]; then
	    # Call with create_arc_ldif_generator_compat bdii_tmp_dir ldif_generator_file
	    create_arc_ldif_generator_compat $bdii_tmp_dir ${bdii_tmp_dir}/arc-nordugrid-bdii-ldif
	fi
    
	# Create arc-glue-bdii-ldif, generates ldif-files for glue schema
	if [ "x$infosys_glue12" = "xenable" ]; then
	    # Call with create_glue_ldif_generator bdii_tmp_dir ldif_generator_file_ng ldif_generator_file_glue
	    create_glue_ldif_generator $bdii_tmp_dir ${bdii_tmp_dir}/arc-nordugrid-bdii-ldif ${bdii_tmp_dir}/arc-glue-bdii-ldif
	fi

    else

	# Call with create_arc_ldif_generator arc_runtime_config ldif_generator_file_ng ldif_generator_file_glue
	create_arc_ldif_generator $arc_runtime_config ${bdii_tmp_dir}/arc-nordugrid-bdii-ldif ${arc_runtime_config}/arc-glue-bdii-ldif

    fi

    # Site BDII
    for site_bdii in `config_subsections infosys/site`; do

      config_import_section infosys/site/$site_bdii
      unique_id=${CONFIG_unique_id:-$site_bdii}
      site_config="${bdii_tmp_dir}/${site_bdii}.conf"
      site_provider="$bdii_provider_dir/site_${site_bdii}.sh"
      # We should have a loop over urls here
      # LOOP START
	url=${CONFIG_url}
	echo "$unique_id $url" > "$site_config"
      # LOOP END 
      # Create script
      printf "#!/bin/sh\n/$ARC_LOCATION/share/arc/glite-info-provider-ldap -m \"$site_bdii\" -c $site_config\n" > $site_provider
      chmod +x $site_provider
    done

    # Resource (cluster or SE) registrations
    # grid-info-resource-register.conf
    create_registration_config_file

    # Index Services: generating the config files
    add_index_services

    # Local Information Service
    add_info_service $bdii_bind $bdii_bind

}

# Print some information about where to find bdii logs
notify_about_bdii () {
    debug_echo "Nordugrid ARC as of version 0.8 uses BDII for"
    debug_echo "information handling instead of the globus based infosys"
    debug_echo "that was used before. The change is backwards compatible."
    debug_echo "BDII writes logs in two places, these might be worth checking"
    debug_echo "if you encounter any problems."
    debug_echo " ${bdii_var_dir}/tmp/stderr.log"
    debug_echo " ${bdii_log_dir}"
}

start () {

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

    # Print help-message about new logs
    notify_about_bdii

    # Create directories for storing temporary scripts and check permissions etc
    create_directory $bdii_var_dir "BDII_VAR_DIR"
    create_directory $bdii_tmp_dir "BDII_TMP_DIR"

    create_bdii_config_files

    # Finished with the config file generation.
    # Time to start the whole infosys

    # If index-server configured, start it
    if [ "`config_subsections infosys/index`" ]; then
	$giis_location/sbin/arc-infoindex-server -c $ARC_CONFIG -f $giis_fifo
    fi

    # Start BDII

    pass=`/usr/bin/mkpasswd -s 0 2> /dev/null` || pass=$RANDOM$RANDOM
    pass=${pass//\//x}

    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/glue2"
	mkdir -p "$dir/infosys"

	conf=$dir/bdii-slapd.conf

	cp /etc/bdii4/DB_CONFIG $dir
	#cp /etc/bdii4/DB_CONFIG $dir/glue2
	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
	    log_failure_msg
	    echo "Can not find 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

    chown -R $bdii_user "${bdii_var_dir}/"

    echo -n "Starting $prog"

    su -s "/bin/sh" -m $bdii_user -c "(
	export LANG=C

	#This is to make infoindex relocateable.
	export ARC_LDAPLIB_SHELL=$ldaplib/back_shell.so

	# It should be irrelevant where you are standing where you start the script
	cd /

	${bdii_update_cmd} ${BDII_CONF} &
	echo \$! > $PID_FILE
    )"

    if [ $? = 0 ]; then
	log_success_msg
    else
	log_failure_msg
    fi

    # Info registration
    if [ "x$infosys_nordugrid" = "xenable" ]; then
	$ARC_LOCATION/share/arc/grid-info-soft-register -log $registrationlog -f $arc_runtime_config/grid-info-resource-register.conf -p `cat $bdii_var_dir/bdii-update.pid` 1>/dev/null 2>/dev/null &
    fi
}

stop () {
    # Stop GRIS

    #Check to see if the process is already stopped
    if [ ! -f "$PID_FILE" ]; then
	log_success_msg "Stopping $prog"
	RETVAL=0
	return
    fi

    #Try to term all slapd processes.
    su -s "/bin/sh" -m $bdii_user -c 'killall -15 bdii4-fwd bdii4-update' 2>/dev/null
    # ... and waiting provider scripts
    su -s "/bin/sh" -m $bdii_user -c 'killall -15 arc-nordugrid-bdii-ldif' 2>/dev/null
    for port in $bdii_ports_write; do
	if [ -s ${bdii_var_dir}/${port}/slapd.pid ]; then
	    su -s "/bin/sh" -m $bdii_user -c "kill -15 $(cat ${bdii_var_dir}/${port}/slapd.pid)" 2>/dev/null
	fi
    done

    rm -f "$PID_FILE"

    sleep 5

    #Send kill -9 to any remaining slapd processes.
    su -s "/bin/sh" -m $bdii_user -c 'killall -9 bdii4-fwd bdii4-update' 2>/dev/null
    # ... and waiting provider scripts
    su -s "/bin/sh" -m $bdii_user -c 'killall -9 arc-nordugrid-bdii-ldif' 2>/dev/null
    for port in $bdii_ports_write; do
	if [ -s ${bdii_var_dir}/${port}/slapd.pid ]; then
	    su -s "/bin/sh" -m $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 [ -s ${bdii_var_dir}/${port}/slapd.pid ]; then
	    RETVAL=1
	fi
    done
    if [ ${RETVAL} -eq 0 ]; then
	log_success_msg "Stopping $prog"
    else
	log_failure_msg "Stopping $prog"
    fi

    # Stop GIIS
    [ -p $giis_fifo ] && echo STOP > $giis_fifo

    # Clean up
    rm -rf ${bdii_var_dir}
    rm -rf ${bdii_tmp_dir}
    rm -f ${arc_runtime_config}/arc-glue-bdii-ldif
}

status ()  {
    if [ ! -f "$PID_FILE" ]; then
	echo "`basename $0` stopped" 1>&2
	RETVAL=1
	return
    fi

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

    bdii_update_base=$(basename $bdii_update_cmd)
    case "$ps" in
	*$bdii_update_base*)
	    echo "`basename $0` OK"
	    RETVAL=0
	    ;;
	*)
	    echo "lock file exists but PID $PID died" 1>&2
	    RETVAL=2
	    ;;
    esac
}
