#!/bin/bash
#
# Usage: globus_environment [service] [conf_dir]
#
# Environment variables:
#
# Input:
# GRID_FUNCTIONS
# NORDUGRID_CONFIG
# NORDUGRID_LOCATION
#
# Output:
# X509_USER_CERT
# X509_USER_KEY
# X509_CERT_DIR
# GRIDMAP
# GLOBUS_TCP_PORT_RANGE
# GLOBUS_UDP_PORT_RANGE
#

GRID_FUNCTIONS=${GRID_FUNCTIONS:-/etc/init.d/grid-functions}
if test ! -r "$GRID_FUNCTIONS"; then
  echo "grid functions ($GRID_FUNCTIONS) not available"
  exit 1
fi
. $GRID_FUNCTIONS

NORDUGRID_CONFIG=${NORDUGRID_CONFIG:-/etc/nordugrid.conf}

if ! test -r "$NORDUGRID_CONFIG"; then
  echo "Can not read $NORDUGRID_CONFIG"
  exit 1
fi

if test "x$2" = "x"; then
  conf_init
  conf_open $NORDUGRID_CONFIG
else
  conf_tmpdir="$2"
fi

# Defaults are different for root and non-root
# For non-root use the old Globus defaults.
if test "`id -u`" = "0"; then
  sec_dir="/etc/grid-security"
  x509_cert_dir="$sec_dir/certificates"
else
  sec_dir="$GLOBUS_LOCATION/etc"
  x509_cert_dir="$GLOBUS_LOCATION/share/certificates"
fi
x509_user_cert="$sec_dir/hostcert.pem"
x509_user_key="$sec_dir/hostkey.pem"
gridmap="$sec_dir/grid-mapfile"
globus_tcp_port_range=""
globus_udp_port_range=""

if test "x$1" = "xgridftpd"; then
  port="2811"
  connections=""
fi

# Read the common section
conf_read common

# Read the main server entry
test "x$1" = "x" || conf_read $1

# Prefer upper case from environment over lower case from configuration file
# use above defaults if all else fail
X509_USER_CERT="${X509_USER_CERT:-$x509_user_cert}"
X509_USER_KEY="${X509_USER_KEY:-$x509_user_key}"
X509_CERT_DIR="${X509_CERT_DIR:-$x509_cert_dir}"
GRIDMAP="${GRIDMAP:-$gridmap}"
GLOBUS_TCP_PORT_RANGE="${GLOBUS_TCP_PORT_RANGE:-$globus_tcp_port_range}"
GLOBUS_UDP_PORT_RANGE="${GLOBUS_UDP_PORT_RANGE:-$globus_udp_port_range}"

cat <<EOF
X509_USER_CERT="$X509_USER_CERT"
X509_USER_KEY="$X509_USER_KEY"
X509_CERT_DIR="$X509_CERT_DIR"
GRIDMAP="$GRIDMAP"
EOF
test "x$GLOBUS_TCP_PORT_RANGE" = "x" || echo GLOBUS_TCP_PORT_RANGE="${GLOBUS_TCP_PORT_RANGE}"
test "x$GLOBUS_UDP_PORT_RANGE" = "x" || echo GLOBUS_UDP_PORT_RANGE="${GLOBUS_UDP_PORT_RANGE}"

# The current grid-manager implementation relies heavily on the environment. This should be
# removed asap.

if test "x$1" = "xgrid-manager"; then
  # PBS_BIN_PATH
  PBS_BIN_PATH_DEFAULT=/usr/bin
  test -d "$PBS_BIN_PATH_DEFAULT" || PBS_BIN_PATH_DEFAULT=""
  PBS_BIN_PATH=${pbs_bin_path:-"$PBS_BIN_PATH_DEFAULT"}
  test "x$PBS_BIN_PATH" = "x" || echo PBS_BIN_PATH="\"$PBS_BIN_PATH\""

  # PBS_LOG_PATH
  PBS_LOG_PATH_DEFAULT=/usr/bin
  test -d "$PBS_LOG_PATH_DEFAULT" || PBS_LOG_PATH_DEFAULT=""
  PBS_LOG_PATH=${pbs_log_path:-"$PBS_LOG_PATH_DEFAULT"}
  test "x$PBS_LOG_PATH" = "x" || echo PBS_LOG_PATH="\"$PBS_LOG_PATH\""

  # NODENAME
  NODENAME_PATH_DEFAULT="/bin/hostname"
  test -x "$NODENAME_PATH_DEFAULT" || NODENAME_PATH_DEFAULT=
  test "x$NODENAME_PATH_DEFAULT" = "x" || NODENAME_PATH_DEFAULT="$NODENAME_PATH_DEFAULT -f"
  NODENAME=${nodename:-"$NODENAME_PATH_DEFAULT"}
  test "x$NODENAME" = "x" || echo NODENAME="\"$NODENAME\""

  # GNU_TIME
  GNU_TIME_DEFAULT=/usr/bin/time
  test -r "$GNU_TIME_PATH_DEFAULT" || GNU_TIME_PATH_DEFAULT=""
  GNU_TIME=${gnu_time:-"$GNU_TIME_DEFAULT"}
  test "x$GNU_TIME" = "x" || echo GNU_TIME=\"$GNU_TIME\"

  # RUNTIME_CONFIG_DIR
  RUNTIME_CONFIG_DIR=${runtimedir}
  test "x$runtimedir" = "x" || echo RUNTIME_CONFIG_DIR=\"$runtimedir\"

  # NFS setup by default
  if test "x$shared_filesystem" = "x" ; then
    RUNTIME_NODE_SEES_FRONTEND="yes"
  elif test "x$shared_filesystem" = "xno"  || test "x$shared_filesystem" = "xNO" ; then
    RUNTIME_NODE_SEES_FRONTEND="no"
  else
    RUNTIME_NODE_SEES_FRONTEND="$shared_filesystem"
  fi
  test "x$RUNTIME_NODE_SEES_FRONTEND" = "x" || echo RUNTIME_NODE_SEES_FRONTEND=\""$RUNTIME_NODE_SEES_FRONTEND"\"

#  nordugrid.conf unsupported environment:
#
#  RUNTIME_LOCAL_SCRATCH_DIR
#  RUNTIME_FRONTEND_SEES_NODE

  test "x$RUNTIME_LOCAL_SCRATCH_DIR"  = "x" || echo RUNTIME_LOCAL_SCRATCH_DIR=\""$RUNTIME_LOCAL_SCRATCH_DIR"\"
  test "x$RUNTIME_FRONTEND_SEES_NODE" = "x" || echo RUNTIME_FRONTEND_SEES_NODE=\""$RUNTIME_FRONTEND_SEES_NODE"\"

fi

# Only close if we opened the config file
if test "x$2" = "x"; then
  conf_close
fi
