#!/bin/bash

# Helper script for the SRM/dCache service publisher
# First argument is init to set variables
#                   status to check the status
# Second argument is v1/v2

case "$1" in
    init)

    dir=${DCACHE_DIR:-/opt/d-cache}

    host=${DCACHE_HOST:-`grep ^ADMIN_NODE $dir/etc/node_config | cut -d= -f2`}
    host=${host:-`grep ^srmDatabaseHost $dir/config/dCacheSetup | cut -d= -f2`}
    host=${host:-`hostname -f`}

    port=`grep ^srmPort $dir/config/dCacheSetup | cut -d= -f2`
    port=${port:-8443}

    case "$2" in
	v1)
	version=1.1.0
	;;
	v2)
	version=2.2.0
	;;
	*)
	echo "Usage: glite-info-service-dcache <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
    echo DCACHE_PID_FILE=$dir/config/lastPid.dCache

    exit
    ;;

    status)

    if [ -r $DCACHE_PID_FILE ]; then
	pid=`cat $DCACHE_PID_FILE`
	if ps p $pid >/dev/null 2>&1; then
	    echo "The dCache server is running" 
	    exit 0
	fi
    fi

    echo "The dCache server is not running"
    exit 1

    ;;
esac

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