#!/bin/bash
#
# Author:
#     James Casey <james.casey@cern.ch>
#     Logic taken from yaim component "config_gip"
#     Updates for GLUE2 - Oliver Keeble 2011
#
# Provide LFC service information for BDII
#

base=$(cd $(dirname $0); pwd)

USAGE=$(basename $0)"  --site site_name [--alias alias_name] [--local \"VO1 VO2\"] [--central \"VO3 VO4\"] [--verbose]"

TEMP=$(getopt -o hc:l:s:vg --long central:,local:,alias:,site:,verbose,glue2 -- "$@")
eval set -- "$TEMP"


# by default the local and central VO lists are empty
LFC_LOCAL=""
LFC_CENTRAL=""
# by default, the hostname to use is the current host
LFC_HOST=`hostname -f`
# by default quiet operation
VERBOSE='false'
# by default, no site-name
SITE_NAME=""

while true; do
    case "$1" in
        -a|--alias)
            shift
            LFC_HOST="$1"
            shift
            ;;
        -s|--site)
            shift
            SITE_NAME="$1"
            shift
            ;;
        -l|--local)
            shift
            LFC_LOCAL="$LFC_LOCAL $1"
            shift
            ;;
        -c|--central)
            shift
            LFC_CENTRAL="$LFC_CENTRAL $1"
            shift
            ;;
        -v|--verbose)
            VERBOSE='true'
            shift
            ;;
        -h|--help)
            echo $USAGE
            exit
            ;;
        -g|--glue2)
            GLUE2='true'
            shift
            ;;
        --)
            shift
            break
            ;;
        *)
            echo "Unknown option '$1'"
            exit
            ;;
    esac
done

if [ -z $SITE_NAME ] ; then
    echo "ERROR : No site name defined" 1>&2;
    exit 1;
fi


#######################################################################
# system parameters
START_TIME=$(stat -c %z /var/run/lfcdaemon.pid  2>/dev/null)
if [ -n "$START_TIME" ]; then
	ISO8061_START_TIME=$(date -u -d "$START_TIME" "+%Y-%m-%dT%H:%M:%SZ")
fi

# Please look away now
LFC_VERSION=$(rpm -q --qf '%{VERSION}' lcgdm-libs) # lcgdm-libs version is always the same as LFC version

if [ ! $? == 0 ]; then
    LFC_VERSION=""
fi

# GLUE 1.3 
# Now create ldif fragment for central catalog

if [ "$LFC_CENTRAL" ]; then
    
    cat <<EOF
dn: GlueServiceUniqueID=http://${LFC_HOST}:8085/,mds-vo-name=resource,o=grid
objectClass: GlueTop
objectClass: GlueService
GlueServiceUniqueID: http://${LFC_HOST}:8085/
GlueServiceName: ${SITE_NAME}-lfc-dli
GlueServiceType: data-location-interface
GlueServiceEndpoint: http://${LFC_HOST}:8085/
GlueServiceURI: http://${LFC_HOST}:8085/
GlueServiceAccessPointURL: http://${LFC_HOST}:8085/
GlueServiceStatus: OK
GlueServiceStatusInfo: No Problems
GlueServiceWSDL: unset
GlueServiceSemantics: unset
GlueForeignKey: GlueSiteUniqueID=${SITE_NAME}
EOF
    if [ -n "$START_TIME" ]; then
	echo "GlueServiceStartTime: ${START_TIME}"

    fi
    if [ -n "$LFC_VERSION" ]; then
	echo "GlueServiceVersion: ${LFC_VERSION}"
    fi
    for VO in $LFC_CENTRAL; do
	echo "GlueServiceOwner: $VO"
	echo "GlueServiceAccessControlRule: $VO"
	echo "GlueServiceAccessControlBaseRule: VO:$VO"
    done
    
    echo 

    cat <<EOF
dn: GlueServiceUniqueID=${LFC_HOST},mds-vo-name=resource,o=grid
objectClass: GlueTop
objectClass: GlueService
GlueServiceUniqueID: ${LFC_HOST}
GlueServiceName: ${SITE_NAME}-lfc
GlueServiceType: lcg-file-catalog
GlueServiceEndpoint: ${LFC_HOST}
GlueServiceURI: ${LFC_HOST}
GlueServiceAccessPointURL: ${LFC_HOST}
GlueServiceStatus: OK
GlueServiceStatusInfo: No Problems
GlueServiceWSDL: unset
GlueServiceSemantics: unset
GlueForeignKey: GlueSiteUniqueID=${SITE_NAME}
EOF

    if [ -n "$START_TIME" ]; then
	echo "GlueServiceStartTime: ${START_TIME}"

    fi
    if [ -n "$LFC_VERSION" ]; then
	echo "GlueServiceVersion: ${LFC_VERSION}"
    fi
    for VO in $LFC_CENTRAL; do
	echo "GlueServiceOwner: $VO"
	echo "GlueServiceAccessControlRule: $VO"
	echo "GlueServiceAccessControlBaseRule: VO:$VO"
    done
    
    echo
    
    
fi

if [ "$LFC_LOCAL" ]; then
    
    cat <<EOF
dn: GlueServiceUniqueID=local-http://${LFC_HOST}:8085/,mds-vo-name=resource,o=grid
objectClass: GlueTop
objectClass: GlueService
GlueServiceUniqueID: local-http://${LFC_HOST}:8085/
GlueServiceName: ${SITE_NAME}-lfc-dli
GlueServiceType: local-data-location-interface
GlueServiceEndpoint: http://${LFC_HOST}:8085/
GlueServiceURI: http://${LFC_HOST}:8085/
GlueServiceAccessPointURL: http://${LFC_HOST}:8085/
GlueServiceStatus: OK
GlueServiceStatusInfo: No Problems
GlueServiceWSDL: unset
GlueServiceSemantics: unset
GlueForeignKey: GlueSiteUniqueID=${SITE_NAME}
EOF

    if [ -n "$START_TIME" ]; then
	echo "GlueServiceStartTime: ${START_TIME}"

    fi
    if [ -n "$LFC_VERSION" ]; then
	echo "GlueServiceVersion: ${LFC_VERSION}"
    fi
    for VO in $LFC_LOCAL; do
	echo "GlueServiceOwner: $VO"
	echo "GlueServiceAccessControlRule: $VO"
	echo "GlueServiceAccessControlBaseRule: VO:$VO"
    done
    
    echo
    
    cat <<EOF

dn: GlueServiceUniqueID=local-${LFC_HOST},mds-vo-name=resource,o=grid
objectClass: GlueTop
objectClass: GlueService
GlueServiceUniqueID: local-${LFC_HOST}
GlueServiceName: ${SITE_NAME}-lfc
GlueServiceType: lcg-local-file-catalog
GlueServiceEndpoint: ${LFC_HOST}
GlueServiceURI: ${LFC_HOST}
GlueServiceAccessPointURL: ${LFC_HOST}
GlueServiceStatus: OK
GlueServiceStatusInfo: No Problems
GlueServiceWSDL: unset
GlueServiceSemantics: unset
GlueForeignKey: GlueSiteUniqueID=${SITE_NAME}
EOF

    if [ -n "$START_TIME" ]; then
	echo "GlueServiceStartTime: ${START_TIME}"

    fi
    if [ -n "$LFC_VERSION" ]; then
	echo "GlueServiceVersion: ${LFC_VERSION}"
    fi
    for VO in $LFC_LOCAL; do
	echo "GlueServiceOwner: $VO"
	echo "GlueServiceAccessControlRule: $VO"
	echo "GlueServiceAccessControlBaseRule: VO:$VO"
    done
    
fi

# GLUE 2

if [ "$GLUE2" == "true" ]; then

    MIDDLEWARE_VERSION=`cat /etc/emi-version 2>/dev/null`
    if [ $? -ne 0 ]; then
        MIDDLEWARE_VERSION="unset"
    fi

    echo

# This bit is shared by central and local LFCs

    cat <<EOF
dn: GLUE2GroupID=resource,o=glue
objectClass: GLUE2Group
GLUE2GroupID: resource
EOF

    echo

    if [ "$LFC_CENTRAL" ]; then

    cat <<EOF
dn: GLUE2ServiceID=${LFC_HOST}-lfc,GLUE2GroupID=resource,o=glue
objectClass: GLUE2Service
GLUE2ServiceID: ${LFC_HOST}-lfc
GLUE2ServiceCapability: data.naming.resolver
GLUE2ServiceType: lcg-file-catalog
GLUE2ServiceQualityLevel: production
GLUE2ServiceAdminDomainForeignKey: ${SITE_NAME}

dn: GLUE2EndpointID=${LFC_HOST}-lfc-endpoint,GLUE2ServiceID=${LFC_HOST}-lfc,GLUE2GroupID=resource,o=glue
objectClass: GLUE2Endpoint
GLUE2EndpointID: ${LFC_HOST}-lfc-endpoint
GLUE2EndpointURL: lfc://${LFC_HOST}:5010
GLUE2EndpointCapability: data.naming.resolver
GLUE2EndpointQualityLevel: production
GLUE2EndpointServingState: production
GLUE2EndpointInterfaceName: lcg-file-catalog
GLUE2EndpointImplementor: gLite
GLUE2EndpointImplementationName: LFC
GLUE2EndpointHealthState: ok
GLUE2EndpointServiceForeignKey: ${LFC_HOST}-lfc
GLUE2EntityOtherInfo: MiddlewareName=EMI
GLUE2EntityOtherInfo: MiddlewareVersion=${MIDDLEWARE_VERSION}
EOF

	if [ -n "$LFC_VERSION" ]; then
	    echo "GLUE2EndpointImplementationVersion: ${LFC_VERSION}"
	fi

	if [ -n "$ISO8061_START_TIME" ]; then
	    echo "GLUE2EndpointStartTime: $ISO8061_START_TIME"
	fi

	echo

    cat <<EOF
dn: GLUE2PolicyID=${LFC_HOST}-lfc-AccessPolicy,GLUE2EndpointID=${LFC_HOST}-lfc-endpoint,GLUE2ServiceID=${LFC_HOST}-lfc,GLUE2GroupID=resource,o=glue
objectClass: GLUE2Policy
objectClass: GLUE2AccessPolicy
GLUE2PolicyID: ${LFC_HOST}-lfc-AccessPolicy
GLUE2PolicyScheme: org.glite.standard
GLUE2AccessPolicyEndpointForeignKey: ${LFC_HOST}-lfc-endpoint
EOF

	for VO in $LFC_CENTRAL; do
	    echo "GLUE2PolicyRule: VO:$VO"
	    echo "GLUE2PolicyUserDomainForeignKey: $VO"
	done
    
	echo

    fi

    if [ "$LFC_LOCAL" ]; then
    cat <<EOF
dn: GLUE2ServiceID=${LFC_HOST}-local-lfc,GLUE2GroupID=resource,o=glue
objectClass: GLUE2Service
GLUE2ServiceID: ${LFC_HOST}-local-lfc
GLUE2ServiceCapability: data.naming.resolver
GLUE2ServiceType: lcg-local-file-catalog
GLUE2ServiceQualityLevel: production
GLUE2ServiceAdminDomainForeignKey: ${SITE_NAME}

dn: GLUE2EndpointID=${LFC_HOST}-local-lfc-endpoint,GLUE2ServiceID=${LFC_HOST}-local-lfc,GLUE2GroupID=resource,o=glue
objectClass: GLUE2Endpoint
GLUE2EndpointID: ${LFC_HOST}-local-lfc-endpoint
GLUE2EndpointURL: lfc://${LFC_HOST}:5010
GLUE2EndpointCapability: data.naming.resolver
GLUE2EndpointQualityLevel: production
GLUE2EndpointServingState: production
GLUE2EndpointInterfaceName: lcg-local-file-catalog
GLUE2EndpointImplementor: gLite
GLUE2EndpointImplementationName: LFC
GLUE2EndpointHealthState: ok
GLUE2EndpointServiceForeignKey: ${LFC_HOST}-local-lfc
GLUE2EntityOtherInfo: MiddlewareName=EMI
GLUE2EntityOtherInfo: MiddlewareVersion=${MIDDLEWARE_VERSION}
EOF

	if [ -n "$LFC_VERSION" ]; then
	    echo "GLUE2EndpointImplementationVersion: ${LFC_VERSION}"
	fi

	if [ -n "$ISO8061_START_TIME" ]; then
	    echo "GLUE2EndpointStartTime: $ISO8061_START_TIME"
	fi

	echo

    cat <<EOF
dn: GLUE2PolicyID=${LFC_HOST}-local-lfc-AccessPolicy,GLUE2EndpointID=${LFC_HOST}-local-lfc-endpoint,GLUE2ServiceID=${LFC_HOST}-local-lfc,GLUE2GroupID=resource,o=glue
objectClass: GLUE2Policy
objectClass: GLUE2AccessPolicy
GLUE2PolicyID: ${LFC_HOST}-local-lfc-AccessPolicy
GLUE2PolicyScheme: org.glite.standard
GLUE2AccessPolicyEndpointForeignKey: ${LFC_HOST}-local-lfc-endpoint
EOF

	for VO in $LFC_LOCAL; do
	    echo "GLUE2PolicyRule: VO:$VO"
	    echo "GLUE2PolicyUserDomainForeignKey: $VO"
	done
    
	echo

    fi

fi
