#!/bin/bash

# Script to invoke the correct DPM info provider

# Defaults
DPM_INFO_PROVIDER="dpm-listspaces"
DPM_INFO_CERT="/var/lib/ldap/hostcert.pem"
DPM_INFO_KEY="/var/lib/ldap/hostkey.pem"
DPM_INFO_SITENAME=""

if [ -f /etc/sysconfig/dpminfo ]; then
  . /etc/sysconfig/dpminfo
else
  echo "No /etc/sysconfig/dpminfo, not running Dome info provider"
  exit 1
fi

if [ "$DPM_INFO_PROVIDER" == "none" -o -z "$DPM_INFO_PROVIDER" ]; then
  echo "Info provider configured not to run"
  exit 0
fi

# Don't run without a site name
if [ -z "$DPM_INFO_SITENAME" ]; then
  echo "DPM info provider can't run without a site name"
  exit 1
fi

# Don't run if the old info provider is being called
if [ -x /var/lib/bdii/gip/provider/se-dpm ]; then
  echo "Please remove /var/lib/bdii/gip/provider/se-dpm in order to use the Dome info provider"
  exit 1
fi
 
# Don't run dpm-listspaces if it's not there
if [ ! -x /usr/bin/dpm-listspaces -a -x /usr/bin/dome-info-provider.py ]; then
  DPM_INFO_PROVIDER="dome" 
fi

# Invoke the right info provider
if [ "$DPM_INFO_PROVIDER" == "dome" ]; then
    /usr/bin/dome-info-provider.py --sitename $DPM_INFO_SITENAME --cert $DPM_INFO_CERT --key $DPM_INFO_KEY
else
  export X509_USER_CERT=$DPM_INFO_CERT
  export X509_USER_KEY=$DPM_INFO_KEY
  dpm-listspaces --gip --protocols --basedir home --site $DPM_INFO_SITENAME --glue2
fi

