#! /bin/bash

# default libexecdir used to bootstrap scripts
libexecdir=${GLOBUS_LOCATION}/libexec

# load GRIS common code and initialization
. ${libexecdir}/grid-info-common
. ${sysconfdir}/grid-info.conf

######################################################################

emit_resource_mds_summary ()
{
    # $1 is dn suffix
    # $2 .. $N are additional object entries
    :
}

emit_resource_mds_swd ()
{

cat <<EOF
${_line_class_dn}dn: Mds-Software-deployment=MDS, $1
${_line_class_oc}objectclass: MdsSoftware
${_line_class_oc}objectclass: MdsService
${_line_class_oc}objectclass: MdsServiceLdap
${_line_class_av}Mds-Software-deployment: MDS
${_line_class_av}Mds-Service-type: ldap
${_line_class_av}Mds-Service-hn: `${bindir}/globus-hostname`
${_line_class_av}Mds-Service-port: ${GRID_INFO_PORT}
${_line_class_av}Mds-Service-Ldap-timeout: ${GRID_INFO_TIMEOUT}
${_line_class_av}Mds-Service-admin-contact: ${GRID_INFO_ADMINISTRATOR}
${_line_class_av}Mds-Service-Executable-PID: `cat ${localstatedir}/resourceslapd.pid`
${_line_class_av}Mds-Service-Path: ${GLOBUS_LOCATION}
EOF

perl -e 'while($a=<STDIN>){ if(! ($a =~ /^#/) ) { print "Mds-Service-admin-comment: $a"; }; };' < ${sysconfdir}/grid-info-deployment-comments.conf

ldapsearch -x -h ${GRID_INFO_HOST} -p ${GRID_INFO_PORT} -s base -b '' '(objectClass=*)' namingContexts | perl -e 'while($a=<STDIN>){ if($a =~ /^namingContexts: (.*)$/ ) { print "Mds-Service-Ldap-suffix: $1\n"; }; };'

emit_mds_object_timestamps
# and a blank line to terminate it
echo

}

emit_resource_mds_descriptions ()
{
    probe_mds_object_timestamps

    if [ "X${_dump_host_object}" = "Xtrue" ]
    then
	emit_resource_mds_summary "${_suffix}"
    fi

    if [ "X${_dump_devclass_object}" = "Xtrue" ]
    then
	emit_resource_mds_swd "${_suffix}"
    fi
}

probe_mds_core ()
{
    if [ "X${GRID_INFO_TIMEOUT}" = "X" ]
    then
	GRID_INFO_TIMEOUT=30
    fi

    if [ "X${GRID_INFO_BASEDN}" = "X" ]
    then
	GRID_INFO_BASEDN="Mds-Vo-name=local, o=Grid"
    fi

    if [ "X${GRID_INFO_PORT}" = "X" ]
    then
	GRID_INFO_PORT=2135
    fi

    # run this in initialized subshell environment
    emit_resource_mds_descriptions
}

#############
# do the work
probe_mds_core
