#!/bin/bash
##############################################################################
# Copyright (c) Members of the EGEE Collaboration. 2004.
# See http://www.eu-egee.org/partners/ for details on the copyright
# holders.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS
# OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################################
#
# NAME :        config_gip_dpm
#
# DESCRIPTION : This function configures the GIP for DPM
#
# AUTHORS :     David.Smith@cern.ch, Gergely.Debreczeni@cern.ch
#
# YAIM MODULE:  glite-yaim-dpm
#
##############################################################################

function config_gip2_dpm_check() {
  requires $1 INSTALL_ROOT DPM_HOST VOS SITE_NAME
}

function l_write_vo_info() {
  tofile=$1
  echo -n "get_owner =" >> $tofile
  nvo=0
  for VO in $VOS; do
    echo -n " echo $VO;" >> $tofile
    nvo=1
  done
  if [ $nvo -eq 0 ]; then
    echo " echo" >> $tofile
  else
    echo >> $tofile
  fi
  echo -n "get_acbr =" >> $tofile
  nvo=0
  for VO in $VOS; do
    echo -n " echo VO:$VO;" >> $tofile
    nvo=1
  done
  if [ $nvo -eq 0 ]; then
    echo " echo" >> $tofile
  else
    echo >> $tofile
  fi
}

function config_gip2_dpm() {

  GLITE_LOCATION_ETC=${GLITE_LOCATION_ETC:-$GLITE_LOCATION/etc}
  BDII_GIP_LOCATION="/var/lib/bdii/gip"

# Set dpns basedirectory to default if not already set
  DPNS_BASEDIR=${DPNS_BASEDIR:-home}

# Removing DPM info provision related files
# Legacy
  rm -f ${BDII_GIP_LOCATION}/providers

# Previous 'glite-info-dynamic-dpm' info provider
  rm -f ${BDII_GIP_LOCATION}/ldif/static-file-dSE.ldif
  rm -f ${BDII_GIP_LOCATION}/ldif/static-file-SE.ldif
  rm -f ${BDII_GIP_LOCATION}/plugin/glite-info-dynamic-se

# Current version (dpm-listspaces and glite-info-service based)
  rm -f ${BDII_GIP_LOCATION}/provider/se-dpm
  rm -f ${BDII_GIP_LOCATION}/provider/service-srm1.1
  rm -f ${BDII_GIP_LOCATION}/provider/service-srm2.2
  rm -f ${BDII_GIP_LOCATION}/service-srm1.1.conf
  rm -f ${BDII_GIP_LOCATION}/service-srm2.2.conf

# Double check these exist as we need to write into them
  mkdir -p ${BDII_GIP_LOCATION}/plugin
  mkdir -p ${BDII_GIP_LOCATION}/provider

# Install script pointing to dpm-listspaces
  cat << EOF  > ${BDII_GIP_LOCATION}/provider/se-dpm
#!/bin/sh
. ${GLITE_LOCATION}/libexec/grid-env.sh
${LCG_LOCATION}/bin/dpm-listspaces --gip --protocols --basedir ${DPNS_BASEDIR} --site ${SITE_NAME} --glue2
EOF

# Set execute permission
  chmod +x ${BDII_GIP_LOCATION}/provider/se-dpm

# For SRM v2.2
  cat << EOF  > ${BDII_GIP_LOCATION}/provider/service-srm2.2
#!/bin/sh
. ${GLITE_LOCATION}/libexec/grid-env.sh
${GLITE_LOCATION}/bin/glite-info-service ${BDII_GIP_LOCATION}/glite-info-service-srm2.2.conf ${SITE_NAME} httpg://${DPM_HOST}:8446/srm/managerv2
EOF
# Set execute permission
  chmod +x ${BDII_GIP_LOCATION}/provider/service-srm2.2
# Configuration file for SRM v2.2 service reporter
  cat << EOF > ${BDII_GIP_LOCATION}/glite-info-service-srm2.2.conf
init = ${GLITE_LOCATION}/bin/glite-info-service-dpm init v2
service_type = SRM
get_version = echo \$GLITE_INFO_SERVICE_VERSION
get_endpoint = echo \$GLITE_INFO_SERVICE_ENDPOINT
get_status = ${GLITE_LOCATION}/bin/glite-info-service-test SRM_V2 && ${GLITE_LOCATION}/bin/glite-info-service-dpm status v2
WSDL_URL = http://sdm.lbl.gov/srm-wg/srm.v2.2.wsdl
semantics_URL = http://sdm.lbl.gov/srm-wg/doc/SRM.v2.2.html
get_starttime = perl -e '@st=stat("/var/run/dpm.pid");print "@st[10]\n";'
get_data = echo
get_services = echo
EOF
  l_write_vo_info ${BDII_GIP_LOCATION}/glite-info-service-srm2.2.conf

return 0
}
