#!/bin/bash

# Create info registration config for the NorduGrid/ARC information system

ARC_LOCATION=${ARC_LOCATION:-/usr}
if [ ! -d "$ARC_LOCATION" ]; then
    echo "ARC_LOCATION ($ARC_LOCATION) not found"
    exit 1
fi

# 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
    echo "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_CONFIG) not found"
    echo "If this file is in a non-standard place it can be set with the"
    echo "  ARC_CONFIG environment variable"
    exit 1
fi

# Read arc.conf
config_parse_file $ARC_CONFIG || exit $?

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

config_hide_all
config_import_section common
config_import_section cluster

# 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

# Get ldap user from passwd
ldap_user=`getent passwd | grep ldap | sed 's/\([az]*\):.*/\1/g'`
if [ ! "xldap" = "x$ldap_user" ] && [ ! "xopenldap" = "x$ldap_user" ]; then
    echo "Could not find ldap or openldap user, using root"
    ldap_user=root
fi
bdii_user=${CONFIG_user:-$ldap_user}

# directories
registrationlog=${CONFIG_registrationlog:-/var/log/arc/inforegistration.log}
mkdir -p `dirname $registrationlog`
touch ${registrationlog}
chown ${bdii_user}: ${registrationlog}

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

registrationconf=$arc_runtime_config/grid-info-resource-register.conf

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
}

echo "# This file was automatically generated by $0." > $registrationconf
echo "# Do not modify." >> $registrationconf
echo >> $registrationconf

(
    config_hide_all
    config_import_section common
    config_import_section cluster
    config_import_section infosys

    # 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:-$CONFIG_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}

	    printregldif >> $registrationconf
	)
    done

    # SE registration blocks
    for seentry in `config_subsections infosys/se`; do
	(
	    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/$p

		    targetsuffix=${CONFIG_targetsuffix:-"Mds-Vo-name=$p,o=Grid"}
		    reghn=${CONFIG_targethostname:-"targethostname.not.set"}
		    regport=${CONFIG_targetport:-2135}
		    hn=${CONFIG_registranthostname:-$CONFIG_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}

		    printregldif >> $registrationconf
		)
	    done
	)
    done

    # Registrations of the Index Services
    for vo in `config_subsections infosys/index`; do
	(
	    CONFIG_name=
	    config_import_section infosys/index/$vo
	    indexname=${CONFIG_name:-$vo}

	    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:-"targethostname.not.set"}
		    regport=${CONFIG_targetport:-2135}
		    hn=${CONFIG_registranthostname:-$CONFIG_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}

		    printregldif >> $registrationconf
		)
	    done
	)
    done
)

exit 0
