# Init file for the NorduGrid/ARC Information System
#
# chkconfig: - 55 25
# description: NorduGrid 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: grid-infosys for ARC middleware
# Description:       ldap-based grid information system for the ARC middleware
### END INIT INFO

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

if [ -r /etc/default/grid-infosys ] ; then
    . /etc/default/grid-infosys
fi
if [ -r /etc/sysconfig/grid-infosys ] ; then
    . /etc/sysconfig/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 1
fi


debug_echo () {
    if test $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}
}

do_exit_start () {
    arg=$*
    if test $arg -gt 0; then
        echo "Information system failed to start"
    fi
    exit $*
}

do_exit_stop () {
    arg=$*
    if test $arg -gt 0; then
        echo "Information system failed to stop"
    fi
    exit $*
}
do_exit_status () {
    arg=$*
    if test $arg -gt 0; then
        echo "Information system is not running"
    fi
    exit $*
}

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  ######################

# Helper functions
if [ -f /etc/init.d/functions ]; then
  . /etc/init.d/functions
else
  success() { echo -n "OK"
  }
  failure() { echo -n "FAILURE"
  }
fi

# LSB init functions
if [ -f /lib/lsb/init-functions ]; then
    . /lib/lsb/init-functions
else
    echo "Error: Cannot source /lib/lsb/init-functions"
    exit 1
fi

# sysconfig files
if [ -r /etc/sysconfig/nordugrid ]; then
    . /etc/sysconfig/nordugrid
fi

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

# source the config parsing routines
if [ -r "$ARC_LOCATION/lib/arc/config_parser_compat.sh" ]; then
    . $ARC_LOCATION/lib/arc/config_parser_compat.sh || exit $?
else
    eval log_failure_msg "Error: could not find $ARC_LOCATION/lib/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
        debug_echo "Fallback /etc/nordugrid.conf was found, trying to use that one"
        ARC_CONFIG="/etc/nordugrid.conf"
    else
        echo -n "Starting $prog"
        failure
        eval log_failure_msg "Failed to find the fallback /etc/nordugrid.conf"
        eval log_failure_msg "arc.conf was not found. If this file is in a non-standard place,"
        eval log_failure_msg "  it can be set with the ARC_CONFIG environment variable"
        exit 1
    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
        echo -n "Starting $prog"
        failure
        eval log_failure_msg "TLS_CACERTDIR=${TLS_CACERTDIR} is set in /etc/openldap/ldap.conf but directory does not exist"
        eval log_failure_msg "This is a bug in OpenLDAP, please create the directory"
        exit 1
    fi
fi

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

# Read arc.conf
export pkglibdir=$ARC_LOCATION/lib/arc
config_parse_file $ARC_CONFIG || exit $?

# Check for infosys-block
if ! config_match_section infosys; then
    eval log_failure_msg "Missing infosys configuration block"
    do_exit_start 1
fi

config_hide_all
config_set_defaults
config_import_section common

# These must come from infosys section
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"
        do_exit_start 1
    fi
    file=$1
    user=$2

    res=$(find `dirname $file`/ -maxdepth 1 -user $user -name `basename $file`)
    if [ "x${res}" = "x" ]; then
        eval log_failure_msg "ERROR: ${file} is not owned by ${user}"
        do_exit_start 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 and no user specified in arc.conf"
	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/grid-infosys.log}
if [ ! -d `dirname $providerlog` ]; then
    mkdir -p `dirname $providerlog`
    debug_echo "Could not find directory for arc info-provider log: `dirname $providerlog`, creating it now."
fi
if [ ! -d `dirname $infosyslog` ]; then
    mkdir -p `dirname $infosyslog`
    debug_echo "Could not find directory for arc infosys log: `dirname $infosyslog`, creating it now."
fi

# Use this command to change user, needs to be paired with -s "/bin/sh" to handle users without a shell
if [ -x /sbin/runuser ] ; then
    RUNUSER=runuser
else
    RUNUSER=su
fi

# If missing, we have a problem
USERSHELL=${USERSHELL:-"/bin/sh"}
if [ ! -x ${USERSHELL} ]; then
    error_echo "Could not find /bin/sh"
    exit 1
fi

# 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/bdii}
bdii_var_dir=${CONFIG_bdii_var_dir:-/var/run/bdii}
bdii_log_dir=${CONFIG_bdii_log_dir:-/var/log/arc/bdii}
bdii_log_file="${bdii_log_dir}/bdii-update.log"
if [ ! -d $bdii_log_dir ]; then
    error_echo "Could not find bdii log directory: $bdii_log_dir"
    mkdir -p $bdii_log_dir
fi
bdii_slapd_conf=$bdii_var_dir/bdii-slapd.conf

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

bdii_default_ldif_ng=${bdii_tmp_dir}/provider/arc-default.ldif.pl
bdii_ldif_dir=${bdii_tmp_dir}/ldif
bdii_provider_dir=${bdii_tmp_dir}/provider
bdii_plugin_dir=${bdii_tmp_dir}/plugin

bdii_port=${CONFIG_port:-2135}

#Using uppercase characters in bdii_bind will break infosys.
bdii_bind="o=grid"
#TODO add to BDII
bdii_db_config=${CONFIG_bdii_db_config:-"/etc/bdii/DB_CONFIG"}
bdii_database=${CONFIG_bdii_database:-"bdb"}
bdii_fix_glue=${CONFIG_fix_glue:-no}
bdii_archive_size=${CONFIG_bdii_archive_size:-0}

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:-120}
    bdii_read_timeout=${CONFIG_bdii_read_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_read_timeout=${CONFIG_bdii_read_timeout:-$max_cycle}
fi
bdii_delete_delay=${CONFIG_bdii_delete_delay:-0}

update_pid_file=${PID_FILE:-/var/run/bdii-update.pid}
slapd_pid_file=${slapd_pid_file:-$bdii_var_dir/db/slapd.pid}

update_lock_file=${update_lock_file:-/var/lock/subsys/bdii-update}
slapd_lock_file=${slapd_lock_file:-/var/lock/subsys/bdii-slapd}
lockfile=/var/lock/subsys/$prog

# 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
if [ "x$bdii_user" = "xroot" ] ; then
    registrationlog=${CONFIG_registrationlog:-/var/log/arc/inforegistration.log}
    if [ ! -d `dirname $registrationlog` ]; then
	debug_echo "Could not find registrationlog directory: `dirname $registrationlog`, creating it now."
	mkdir -p `dirname $registrationlog`
    fi

    touch ${registrationlog}
    chown root ${registrationlog}

    touch ${providerlog}
    chown root ${providerlog}

    arc_runtime_config="/var/run/nordugrid"
    mkdir -p ${arc_runtime_config}
    check_ownership ${arc_runtime_config} root
    check_ownership ${bdii_log_dir} root
    check_ownership ${providerlog} root
    check_ownership ${registrationlog} root
else
    registrationlog=${CONFIG_registrationlog:-/var/log/arc/${bdii_user}-inforegistration.log}
    if [ ! -d `dirname $registrationlog` ]; then
	debug_echo "Could not find registrationlog directory: `dirname $registrationlog`, creating it now."
	mkdir -p `dirname $registrationlog`
    fi
    
    touch ${registrationlog}
    chown ${bdii_user} ${registrationlog}

    touch ${providerlog}
    chown root ${providerlog}

    arc_runtime_config="/var/tmp/${bdii_user}-nordugrid"
    if [ -d ${arc_runtime_config} ]; then
        check_ownership ${arc_runtime_config} ${bdii_user}
    else
        mkdir -p ${arc_runtime_config}
        chown ${bdii_user} ${arc_runtime_config}
        check_ownership ${arc_runtime_config} ${bdii_user}
    fi

    check_ownership ${registrationlog} ${bdii_user}
    check_ownership ${bdii_log_dir} ${bdii_user}
    check_ownership ${providerlog} ${bdii_user}
fi

# If the new code path is selected...
if [ "x$infosys_compat" != "xenable" ]; then
    if [ ! -f "$pkglibdir/InfosysHelper.pm" ]; then
        eval log_failure_msg "A-REX is not installed. For operation without A-REX, set infosys_compat=enable"
        exit 1
    fi
elif [ "x$infosys_glue2_ldap" = "xenable" ]; then
    eval log_failure_msg "GLUE2 cannot be published when infosys_compat=enable"
    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
    eval 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 test -x "$dir/slapd"; then
            slapd_cmd="$dir/slapd"
            break
        fi
    done
    IFS=$O_IFS
else
    slapd_cmd=$CONFIG_slapd
fi

if [ ! -x $slapd_cmd ]; then
    eval log_failure_msg "Error, could not find ldap server binary, usually /usr/bin/slapd,"
    eval 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
        eval log_failure_msg "Error, 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
            eval log_failure_msg "Error, 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

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


fi

# 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:-/var/run/nordugrid/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
# Will create bdii.conf file in /var/run/nordugrid/bdii.conf
create_bdii_conf () {
    if [ $# != 7 ]; then
        error_echo "Wrong number of arguments to create_bdii_conf"
        error_echo "Arguments were: $*"
        do_exit_start 1
    fi
    bdii_location=$1
    bdii_var_dir=$2
    bdii_slapd_conf=$3
    bdii_port=$4
    bdii_ldif_dir=$5
    bdii_provider_dir=$6
    bdii_plugin_dir=$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_FILE=$bdii_log_file
	#BDII_LOG_LEVEL=ERROR
	BDII_LOG_LEVEL=DEBUG
	BDII_LDIF_DIR=$bdii_ldif_dir
	BDII_PROVIDER_DIR=$bdii_provider_dir
	BDII_PLUGIN_DIR=$bdii_plugin_dir
	BDII_PORT=$bdii_port
	BDII_USER=$bdii_user
	BDII_BIND=$bdii_bind
	BDII_BREATHE_TIME=$bdii_breathe_time
	BDII_READ_TIMEOUT=$bdii_read_timeout
	BDII_DELETE_DELAY=$bdii_delete_delay
	BDII_INFOSYS=$bdii_infosys
	BDII_ARCHIVE_SIZE=$bdii_archive_size
	FIX_GLUE=$bdii_fix_glue
	SLAPD_CONF=$bdii_slapd_conf
	EOF
}

# 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: $*"
        do_exit_start 1
    fi
    bdii_location=$1
    bdii_slapd_conf=$2

    for i in "/etc/bdii/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 $ARC_LOCATION/share/arc/ldap-schema/glue-1.2/Glue-CORE.schema
	include $ARC_LOCATION/share/arc/ldap-schema/glue-1.2/Glue-CE.schema
	include $ARC_LOCATION/share/arc/ldap-schema/glue-1.2/Glue-CESEBind.schema
	#glue2 schema
	include /etc/ldap/schema/GLUE20.schema

	$slapd_modulepath
	$slapd_moduleload
	$slapd_moduleload_index

	allow bind_v2

	pidfile         $bdii_var_dir/db/slapd.pid
	argsfile        $bdii_var_dir/db/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: $*"
        do_exit_start 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
	directory       $bdii_var_dir/db/arc

	# ${bdii_database} database definitions for o=glue
	database        ${bdii_database}
	cachesize       150000
	dbnosync
	suffix          "o=glue"
	rootdn          "o=glue"
	rootpw          secret
	directory       $bdii_var_dir/db/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
	directory       $bdii_var_dir/db/stats
	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: $*"
        do_exit_start 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: $*"
        do_exit_start 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
            if [ ! -f $providerlog ]; then
                touch $providerlog
            fi
            chown ${bdii_user}:${bdii_user} ${providerlog}
            config_import_section cluster
            cachetime=${CONFIG_cachetime:-$bdii_breathe_time}

            cat <<-EOF >> ${ldif_generator_file}
		$ARC_LOCATION/lib/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/lib/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
            if [ ! -f $providerlog ]; then
                touch $providerlog
            fi
            chown ${bdii_user}:${bdii_user} ${providerlog}
            config_import_section se/$se
            cachetime=${CONFIG_cachetime:-$bdii_breathe_time}

            cat <<-EOF >> ${ldif_generator_file}
		$ARC_LOCATION/lib/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: $*"
        do_exit_start 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/lib/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, '$pkglibdir'; }
	use InfosysHelper;
	exit 1 unless InfosysHelper::ldifIsReady('$arc_runtime_config', '$max_cycle');
	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: $*"
        do_exit_start 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,g;
         s,\$GLUESITEUNIQUEID,$gluesiteuniqueid,g;
	 s,\$PROVIDE_GLUE_SITE_INFO,$provide_glue_site_info,g;
        " $ARC_LOCATION/lib/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: $*"
        do_exit_start 1
    fi

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

    if [ ${#dir} -le "1" ]; then
        eval log_failure_msg "$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
        eval log_failure_msg "$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 bdii_port
    create_bdii_conf $bdii_location $bdii_var_dir $bdii_slapd_conf $bdii_port $bdii_ldif_dir $bdii_provider_dir $bdii_plugin_dir

    # 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}/provider/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}/provider/arc-nordugrid-bdii-ldif ${bdii_tmp_dir}/provider/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}/provider/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/lib/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 here that you may want to check if something"
    debug_echo "is not working: "
    debug_echo " ${bdii_log_dir}"
    debug_echo "Also, if the BDII run-directory is here:"
    debug_echo " ${bdii_var_dir}"
    debug_echo "That can also be worth checking up on if something is broken."
}

# Checks status, if unclean shutdown occured, clean up
check_clean_status () {
    if [ -f "${slapd_lock_file}" ]; then
        if [ -f "${slapd_pid_file}" ]; then
            slapd_pid=`cat "${slapd_pid_file}"`
            if ps -p "$slapd_pid" > /dev/null; then
                # running
                echo -n "Starting $prog: "
                failure
                eval log_failure_msg "$prog already started"
                do_exit_start 0
            else
                rm -f "${slapd_pid_file}"
            fi
        fi
        rm -f "${slapd_lock_file}"
    fi
    if [ -f "${update_lock_file}" ]; then
        if [  -f "${update_pid_file}" ]; then
            update_pid=`cat "${update_pid_file}"`
            if ps -p "$update_pid" > /dev/null; then
                # running
                echo -n "Starting $prog: "
                failure
                eval log_failure_msg "$prog already started"
                do_exit_start 0
            else
                rm -f "${update_pid_file}"
            fi
        fi
        rm -f "${update_lock_file}"         
    fi
}

start () {
    # Check status
    check_clean_status

    # 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_directory $bdii_tmp_dir/ldif "BDII_TMP_DIR/ldif"
    create_directory $bdii_tmp_dir/provider "BDII_TMP_DIR/provider"
    create_directory $bdii_tmp_dir/plugin "BDII_TMP_DIR/plugin"
    create_directory /var/lock/subsys "/var/lock/subsys"
    create_bdii_config_files

    #Initialize the database directory
    create_directory $bdii_var_dir/db/stats "BDII_VAR_DIR/db/stats"
    create_directory $bdii_var_dir/db/arc "BDII_VAR_DIR/db/arc"
    create_directory $bdii_var_dir/db/glue2 "BDII_VAR_DIR/db/glue2"
    create_directory $bdii_var_dir/archive "BDII_VAR_DIR/archive"
    chown -R $bdii_user:$bdii_user $bdii_var_dir
    chown -R $bdii_user:$bdii_user $bdii_tmp_dir
    if [ ! -f $bdii_log_file ]; then
        touch $bdii_log_file
    fi
    chown $bdii_user:$bdii_user $bdii_log_file
    $RUNUSER -s "$USERSHELL" -c "cp ${bdii_db_config} ${bdii_var_dir}/db/" $bdii_user
    $RUNUSER -s "$USERSHELL" -c "cp ${bdii_db_config} ${bdii_var_dir}/db/arc" $bdii_user
    $RUNUSER -s "$USERSHELL" -c "cp ${bdii_db_config} ${bdii_var_dir}/db/glue2" $bdii_user
    #Need to remove / from mkpasswd, otherwise sed will break.
    pass=`/usr/bin/mkpasswd -s 0 2> /dev/null` || pass=$RANDOM$RANDOM
    pass=${pass//\//x}

    (umask 077; >> $bdii_slapd_conf)

    if [ ! -f ${bdii_slapd_conf} ]; then
        eval log_failure_msg "Can not find slapd file: ${bdii_slapd_conf}"
        do_exit_start 1
    fi

    sed -i "s/secret/${pass}/g" $bdii_slapd_conf

    # 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

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

    echo -n "Starting $prog: "

    if [ "`config_subsections infosys/index`" ]; then
        #The -d 0 is important for the GIIS to ensure that the binary doesn't fork.
        ARC_LDAPLIB_SHELL=$ldaplib/back_shell.so \
            LD_PRELOAD=${giis_location}/lib/arc/arc-infoindex-slapd-wrapper.so \
            ${slapd_cmd} -d 0 -f ${bdii_slapd_conf} -h ldap://${slapd_hostnamebind}:${bdii_port} \
            -u ${bdii_user} </dev/null 1>/dev/null 2>/dev/null &
    else
        ${slapd_cmd} -f ${bdii_slapd_conf} -h ldap://${slapd_hostnamebind}:${bdii_port} -u ${bdii_user}
    fi

    touch ${slapd_lock_file}

    if [ ! -f "${slapd_pid_file}" ]; then
        sleep 2
    fi

    if [ -f "${slapd_pid_file}" ]; then
        ps $(cat ${slapd_pid_file}) >/dev/null 2>&1
        RETVAL=$?
    else
        RETVAL=1
    fi

    if [ ${RETVAL} -gt 0 ]; then
        failure
        eval log_failure_msg "$prog SLAPD failed to start"
        rm -f ${slapd_lock_file}
        eval log_failure_msg "Error was: "
        ${slapd_cmd} -d 1 -f ${bdii_slapd_conf} -h ldap://${slapd_hostnamebind}:${bdii_port} -u ${bdii_user}
        do_exit_start 1
    fi

    $RUNUSER -s "$USERSHELL" -c "${bdii_update_cmd} -c ${BDII_CONF} -d" ${bdii_user}
    touch ${update_lock_file}

    if [ ! -f ${bdii_var_dir}/bdii-update.pid ]; then
        sleep 2
    fi
    if [ -f ${bdii_var_dir}/bdii-update.pid ]; then
        touch ${update_pid_file}
        chown ${bdii_user} ${update_pid_file}
        mv ${bdii_var_dir}/bdii-update.pid ${update_pid_file}
        ps $(cat ${update_pid_file}) >/dev/null 2>&1
        RETVAL=$?
    else
        failure
        do_exit_start 1
    fi

    if [ ${RETVAL} -gt 0 ]; then
        failure
        eval log_failure_msg "$prog update process failed to start"
        rm -f ${update_lock_file}
        do_exit_start 1
    else
        touch $lockfile
        RETVAL=0
    fi

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

    success
    echo
}

stop () {
    # Stop GRIS

    # Check the existance of the lock file
    if [ ! -f "${slapd_lock_file}" ] &&  [ ! -f "${update_lock_file}" ]; then
        echo -n "Stopping $prog: "
        result=$($0 status)
        if [ $? -gt 0 ]; then
            failure
            echo ${result} 1>&2
            return 1
        else
            eval log_success_msg "$prog already stopped"
            return 0
        fi
    fi

    echo -n "Stopping $prog: "

    # Kill waiting provider script
    killall -u ${bdii_user} -15 arc-nordugrid-bdii-ldif 2>/dev/null

    if [ -f "${update_pid_file}" ]; then
        update_pid=$(cat ${update_pid_file})
    else
        failure
        eval log_failure_msg "$prog update process has no pid-file"
        return 1
    fi

    if [ -n "${update_pid}" ]; then
        $RUNUSER -s "$USERSHELL" -c "kill -15 ${update_pid} 2>/dev/null" ${bdii_user}

        ps ${update_pid} >/dev/null 2>&1
        if [ $? = 0 ]; then
            sleep 2
            ps ${update_pid} >/dev/null 2>&1
            if [ $? = 0 ]; then
                $RUNUSER -s "$USERSHELL" -c "kill -9 ${update_pid} 2>/dev/null" ${bdii_user}
                sleep 2
                ps ${update_pid} >/dev/null 2>&1
                if [ $? = 0 ]; then
                    RETVAL=1
                fi
            fi

        fi
    fi

    if [ ${RETVAL} = 0 ];  then
        rm -f ${update_pid_file}
        rm -f ${update_lock_file}
    else
        failure
        eval log_failure_msg "Could not kill $prog update process with pid ${update_pid}"
    fi

    if [ -f "${slapd_pid_file}" ]; then
        slapd_pid=$(cat ${slapd_pid_file})
    else
        failure
        eval log_failure_msg "$prog SLAPD process has no pid-file"
        return 1
    fi

    if [ -n "${slapd_pid}" ]; then
        $RUNUSER -s "$USERSHELL" -c "kill -15 ${slapd_pid} 2>/dev/null" ${bdii_user}
        ps ${slapd_pid} >/dev/null 2>&1
        if [ $? = 0 ]; then
            sleep 2
            ps ${slapd_pid} >/dev/null 2>&1
            if [ $? = 0 ]; then
                $RUNUSER -s "$USERSHELL" -c "kill -9 ${slapd_pid} 2>/dev/null" ${bdii_user}
                sleep 2
                ps ${slapd_pid} >/dev/null 2>&1
                if [ $? = 0 ]; then
                    RETVAL=2
                else
                    rm -f {slapd_pid_file}
                fi

            fi
        fi
    fi

    if [ ${RETVAL} = 2 ];  then
        failure
        eval log_failure_msg "Error, could not stop $prog slapd with pid: $slapd_pid"
    else
        rm -f ${slapd_lock_file}
    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

    if [ ! ${RETVAL} = 0 ];  then
        failure
        return 1
    else
        # lockfile is to make sure that bdii gets a clean shutdown with system
        rm -f $lockfile
    fi

    success
    echo
    return ${RETVAL}
}

status ()  {
    if [ ! -f "${slapd_lock_file}" ] && [ ! -f "${update_lock_file}" ]; then
        echo -n "BDII Stopped "
        success
        echo
        return 3
    fi

    if [ -f ${slapd_pid_file} ]; then
        ps $(cat ${slapd_pid_file}) >/dev/null 2>&1
        if [ ! $? = 0 ]; then
            eval log_failure_msg "slapd pid file exists: ${slapd_pid_file} but the process died"
            return 1
        fi
    else
        eval log_failure_msg "slapd lock file: ${slapd_lock_file} and/or"
        eval log_failure_msg "bdii-update lock file: ${update_lock_file} exists but"
        eval log_failure_msg "slapd pid file: ${slapd_pid_file} does not exist."
        eval log_failure_msg "Maybe you had an unclean shutdown, please remove"
        eval log_failure_msg "lockfiles and retry starting $prog"
        return 1
    fi

    if [ -f ${update_pid_file} ]; then
        ps $(cat ${update_pid_file}) >/dev/null 2>&1
        if [ ! $? = 0 ]; then
            eval log_failure_msg "bdii-update pid file exists: ${update_pid_file} but the update process died"
            return 1
        fi
    else
        eval log_failure_msg "slapd lock file: $slapd_lock_file and/or"
        eval log_failure_msg "bdii-update lock file: ${update_lock_file} exists but"
        eval log_failure_msg "update process pid file: ${update_pid_file} does not exist"
        eval log_failure_msg "maybe you had an unclean shutdown, please remove"
        eval log_failure_msg "lockfiles and retry starting $prog"
        return 1
    fi

    echo -n "BDII Runnning "
    success
    echo
    return 0
}
