#!/bin/bash

# Environment variables:
#
# GRID_FUNCTIONS
# NORDUGRID_CONFIG
# NORDUGRID_LOCATION
#

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$1" = "x"; then
  conf_init
  conf_open $NORDUGRID_CONFIG
else
  conf_tmpdir=$1
fi

server=grid-manager

# Defaults

user=""
debug=""
unset runtimedir
unset mail
unset controldir
unset statusowner
unset joblog
unset jobreport
unset maxjobs
unset maxload
unset defaultttl
unset lrms
unset cachedir
unset privatecache
unset cachesize
unset cachedata
unset helper
unset authplugin
unset localcred
unset linkurl
unset copyurl
unset securetransfer
unset localtransfer
unset cacheregistration
unset maxrerun

# Read the common section
conf_read common

# Server defaults
logfile="/var/log/$server.log"

# Read the main server entry
conf_read $server

if [ `id -u` = 0 ] ; then
  pidfile="/var/run/$server.pid"
else
  pidfile="$HOME/$server.pid"
fi

if [ -z "$mail" ]; then 
  echo "Please specify a mail adress with the mail attribute" 1>&2
  if test "x$1" = "x"; then
    conf_exit 1
  fi
fi

if [ -z "$sessiondir" ]; then 
  echo "Please specify a session directory with the sessiondir attribute" 1>&2
  if test "x$1" = "x"; then
    conf_exit 1
  fi
fi

controldir=${controldir:-"/var/spool/nordugrid/jobstatus"}
if [ ! -d "$controldir" ]; then
  echo "Warning: creating control directory $controldir" 1>&2
  mkdir -p "$controldir"
fi

cat <<-EOF
	# This file was automatically generated by nordugrid startup script. Do not modify.

	EOF

test "x$user"         = "x" || echo "user $user"
test "x$pidfile"      = "x" || echo "pidfile $pidfile"
test "x$logfile"      = "x" || echo "logfile $logfile"
test "x$debug"        = "x" || echo "debug $debug"

statusowner=${statusowner:-yes}
echo "statusowner ${statusowner}"
joblog=${joblog:-/var/log/gm-jobs.log}
echo "joblog ${joblog}"

maxjobs=${maxjobs:-10000 10}
echo "maxjobs ${maxjobs}"
maxload=${maxload:-10 2 5}
echo "maxload ${maxload}"
echo "mail ${mail}"
defaultttl=${defaultttl:-86400}
echo "defaultttl ${defaultttl}"
defaultlrms=${lrms:-pbs}
echo "defaultlrms ${defaultlrms}"
securetransfer=${securetransfer:-no}
echo "securetransfer ${securetransfer}"
localtransfer=${localtransfer:-no}
echo "localtransfer ${localtransfer}"
cacheregistration=${cacheregistration:-no}
echo "cacheregistration ${cacheregistration}"

if test ! "x$cachedir" = "x" ; then
  echo "cache ${cachedir}"
fi

if test ! "x$privatecache" = "x" ; then
  echo "privatecache $privatecache"
fi

if test ! "x$cachesize" = "x" ; then
  echo "cachesize $cachesize"
fi

if test ! "x$cachedata" = "x" ; then
  echo "cachedata $cachedata"
fi

if test ! "x$maxrerun" = "x" ; then
  echo "maxrerun ${maxrerun}"
fi

i=0
tempplugin=$authplugin
while [ ! -z "$tempplugin" ]; do
 echo "authplugin $tempplugin"
 i=$[$i+1]
 tempplugin=`eval echo '$authplugin'_$i`
done

i=0
tempplugin=$localcred
while [ ! -z "$tempplugin" ]; do
 echo "localcred $tempplugin"
 i=$[$i+1]
 tempplugin=`eval echo '$localcred'_$i`
done

echo "session ${sessiondir}"
echo "control ${controldir} ."
i=0
tempvar=$helper
while [ ! -z "$tempvar" ]; do
  echo "helper $tempvar"
  i=$[$i+1]
  tempvar=`eval echo '$helper'_$i`
done
i=0
tempurl=$linkurl
while [ ! -z "$tempurl" ]; do
 echo "linkurl $tempurl"
 i=$[$i+1]
 tempurl=`eval echo '$linkurl'_$i`
done
i=0
tempurl=$copyurl
while [ ! -z "$tempurl" ]; do
 echo "copyurl $tempurl"
 i=$[$i+1]
 tempurl=`eval echo '$copyurl'_$i`
done
i=0
tempurl=$jobreport
while [ ! -z "$tempurl" ]; do
 echo "jobreport $tempurl"
 i=$[$i+1]
 tempurl=`eval echo '$jobreport'_$i`
done

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