#!/bin/bash

# Helper script for the SRM/DPM service publisher
# First argument is init to set variables
#                   status to check the status
# Second argument is v1/v2
# NB v2 really means 2.2 due to the way DPM is configured
# Unreliable to use shift.conf for the hostname, see bug #39375
# More robust selection of the port, from Michel Jouvin

case "$1" in
    init)

#    host=${DPM_HOST:-`grep DPM /etc/shift.conf | cut -d" " -f3`}
    host=${DPM_HOST:-`hostname -f`}

    case "$2" in
	v1)
. /etc/sysconfig/srmv1
	port=${SRMV1_PORT:-8443}
#	port=`grep SRMV1 /etc/shift.conf | cut -d" " -f3`
#	port=${port:-8443}
	version=1.1.0
	;;
	v2)
. /etc/sysconfig/srmv2.2
        port=${SRMV2_2_PORT:-8446}
#	port=`grep SRMV2_2 /etc/shift.conf | cut -d" " -f3`
#       port=${port:-8446}
	version=2.2.0
	;;
	*)
	echo "Usage: glite-info-service-dpm <init|status> <v1|v2>"
	echo "Second argument must be v1 or v2"
	exit 2
	;;
    esac

    echo GLITE_INFO_SERVICE_ENDPOINT=httpg://$host:$port/srm/manager$2
    echo GLITE_INFO_SERVICE_VERSION=$version
    exit
    ;;

    status)

    stat1=`/sbin/service dpm status`
    rc1=$?

    stat2=`/sbin/service dpnsdaemon status`
    rc2=$?

    case "$2" in
        v1)
	stat3=`/sbin/service srmv1 status`
	rc3=$?
        ;;
        v2)
        stat3=`/sbin/service srmv2.2 status`
        rc3=$?
        ;;
        *)
        echo "Internal error (unknown SRM version)"
        exit 3
        ;;
    esac

    echo -n "The DPM server is "
    [[ $rc1 != 0 ]] && echo -n "not "
    echo -n "running, the DPNS daemon is "
    [[ $rc2 != 0 ]] && echo -n "not "
    echo -n "running and the SRM is "
    [[ $rc3 != 0 ]] && echo -n "not "
    echo "running"
    [[ ($rc1 == 0) && ($rc2 == 0) && ($rc3 == 0) ]]
    exit $?

    ;;
esac

echo "Usage: glite-info-service-dpm <init|status> <v1|v2>"
exit 1
