#!/usr/bin/bash

# Helper script for the RTEpublisher service

case "$1" in
init)
	# init function for the RTEPublisher service publisher,
	# to export some variables

	# The first two can be overridden by external definitions of the variables,
	# e.g. if the advertised host name should be an alias
	RTEPUBLISHER_HOST=${RTEPUBLISHER_HOST:-$(hostname -f)}
	RTEPUBLISHER_PORT=${RTEPUBLISHER_PORT:-2811}

	# The path is currently hardwired - should be improved!
	RTEPUBLISHER_PATH=${RTEPUBLISHER_PATH:-/opt/glite/var/info}

	# Apparently there is no pid file - use the log file to get a start time?
	# GLOBUS_SYSCONFIG=${GLOBUS_SYSCONFIG:-/etc/sysconfig/globus}
	# if [ -f "$GLOBUS_SYSCONFIG" ]; then
	#     . "$GLOBUS_SYSCONFIG"
	# fi
	# GRIDFTP_SYSCONFIG=${GRIDFTP_SYSCONFIG:-/etc/sysconfig/globus-gridftp}
	# if [ -f "$GRIDFTP_SYSCONFIG" ]; then
	#     . "$GRIDFTP_SYSCONFIG"
	# fi
	# GRIDFTP_LOCK_FILE=${GRIDFTP_TRANSFER_LOG:-/var/log/globus-gridftp.log}

	# This seems to have changed since the last time I looked at it ...
	# GRIDFTP_LOCK_FILE=/var/lock/subsys/globus-gridftp
	# Third try: get the pid for the ftpd process and use the timestamp
	# on the /proc file
	# GRIDFTP_PROC_FILE=`/bin/ps axwww | awk '/[f]tpd/ { print "/proc/"$1 }' | head -n 1`
	# Fourth try: recent versions still have no pid/lock file and a different
	# process name.
	# GRIDFTP_PROC_FILE=`/bin/ps axwww | awk '/globus-gridftp-server/ { print "/proc/"$1 }' | head -n 1`
	# Fifth try: the previous line could pick up the awk line itself ...
	GRIDFTP_PROC_FILE=$(/bin/ps axwww | grep -v awk | awk '/globus-gridftp-server/ { print "/proc/"$1 }' | head -n 1)

	# Write to stdout - will be imported by the info provider
	echo RTEPUBLISHER_HOST="$RTEPUBLISHER_HOST"
	echo RTEPUBLISHER_PORT="$RTEPUBLISHER_PORT"
	echo RTEPUBLISHER_PATH="$RTEPUBLISHER_PATH"
	echo GRIDFTP_PROC_FILE="$GRIDFTP_PROC_FILE"
	echo SUBCLUSTERID="$SUBCLUSTERID"

	exit
	;;

subclusters)
	# SubCluster IDs are the names of the directories in the path
	# The expression below should list them in a form which will create
	# one ServiceData entry per directory with a Key of the form
	# GlueSubClusterUniqueID:<directory> and no Value.
	# This ls is specially crafted to handle spaces, so disabling SC2010
	# shellcheck disable=SC2010
	ls -pQ "$RTEPUBLISHER_PATH" | grep /$ | cut -d\" -f2 | sed s/^/GlueSubClusterUniqueID:/ | sed s/$/=/

	exit $?
	;;

executionenvironments)
	# This is a GLUE 2 equivalent for the subcluster publication. It's
	# assumed that the ID (GLUE2ResourceID) is the same as the SubCluster ID.
	# Also in GLUE 2 we can have multiple Extension objects with the same
	# Key, so we can publish them more sensibly.
	# This ls is specially crafted to handle spaces, so disabling SC2010
	# shellcheck disable=SC2010
	ls -pQ "$RTEPUBLISHER_PATH" | grep /$ | cut -d\" -f2 | sed s/^/GLUE2ResourceID=/

	exit $?
	;;

vos)
	# VO names are the names of directories one level below the subcluster
	# directories. The expression below should list all the VO names.
	# This ls is specially crafted to handle spaces, so disabling SC2010
	# shellcheck disable=SC2010
	ls -pQ "$RTEPUBLISHER_PATH"/* | grep /$ | cut -d\" -f2 | sort | uniq

	exit $?
	;;

esac

echo "Usage: glite-info-service-rtepublisher <init|subclusters|executionenvironments|vos>"
exit 1
