#!/bin/bash
#
# Init file for the NorduGrid grid-manager
#
# chkconfig: 2345 55 25
# description: NorduGrid grid-manager
#
# config: /etc/sysconfig/globus
# config: /etc/sysconfig/nordugrid
# config: /opt/nordugrid/etc/nordugrid.conf
# config: /etc/nordugrid.conf

# source function library
. /etc/rc.d/init.d/functions

###############  Standard Globus configuration  ######################

if [ -f /etc/sysconfig/globus ]; then
    . /etc/sysconfig/globus
fi

GLOBUS_LOCATION=${GLOBUS_LOCATION:-/opt/globus}

if [ ! -d "$GLOBUS_LOCATION" ]; then
  echo "GLOBUS_LOCATION not found" 
  exit 1
fi
LD_LIBRARY_PATH=$GLOBUS_LOCATION/lib:$LD_LIBRARY_PATH
export GLOBUS_LOCATION LD_LIBRARY_PATH

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

#  GLOBUS_CONFIG=${GLOBUS_CONFIG:-/etc/globus.conf}
#
#  if [ ! -r "$GLOBUS_CONFIG" ]; then
#    echo "Globus configuration not found (usually /etc/globus.conf)"
#  fi

###############  Standard NorduGrid configuration  ######################

if [ -f /etc/sysconfig/nordugrid ]; then
    . /etc/sysconfig/nordugrid
fi

NORDUGRID_LOCATION=${NORDUGRID_LOCATION:-/opt/nordugrid}

if [ ! -d "$NORDUGRID_LOCATION" ]; then
  echo "NORDUGRID_LOCATION not found" 
  exit 1
fi
export NORDUGRID_LOCATION

if [ -z "$NORDUGRID_CONFIG" ]; then
  if [ -r $NORDUGRID_LOCATION/etc/nordugrid.conf ]; then
    NORDUGRID_CONFIG=$NORDUGRID_LOCATION/etc/nordugrid.conf
  elif [ -r /etc/nordugrid.conf ]; then
    NORDUGRID_CONFIG=/etc/nordugrid.conf
  fi
fi

if [ ! -r "$NORDUGRID_CONFIG" ]; then
   if [ ! -r $NORDUGRID_LOCATION/etc/grid-manager.conf ]; then
     echo "NorduGrid configuration not found (usually /etc/nordugrid.conf)"
     exit 1
   else
     echo "NorduGrid common configuration not found (usually /etc/nordugrid.conf)"
     echo "  using low level configuration instead."
   fi
fi

######################################################################

#
# Get NORDUGRID_ETC_LOCATION
#
# Search order:
#  1. Environment
#  2. nordugrid.conf [common] : nordugrid_etc_location
#  3. NORDUGRID_LOCATION/etc
#
unset nordugrid_etc_location
conf_read common
NORDUGRID_ETC_LOCATION=${NORDUGRID_ETC_LOCATION:-$nordugrid_etc_location}
NORDUGRID_ETC_LOCATION=${NORDUGRID_ETC_LOCATION:-$NORDUGRID_LOCATION/etc}

if test -d "$NORDUGRID_ETC_LOCATION"; then
  :
else
  mkdir -p "$NORDUGRID_ETC_LOCATION"
fi

function std_header()
{
    echo '# This file was automatically generated by nordugrid startup script. Do not modify.' > $1
    echo >> $1
}

function create_gm_config () {

# Read configuration

if [ -r $NORDUGRID_CONFIG ]; then

unset gridmap
unset x509_user_cert
unset x509_user_key
unset runtimedir
unset mail
unset controldir
unset statusowner
unset joblog
unset jobreport
unset maxjobs
unset maxload
unset defaultttl
unset defaultlrms
unset cachedir
unset privatecache
unset cachesize
unset cachedata
unset helper
unset linkurl
unset copyurl
unset securetransfer
unset localtransfer

conf_read common

conf_read grid-manager

# PBS bin path
PBS_BIN_PATH_DEFAULT=/usr/bin
[ ! -d $PBS_BIN_PATH_DEFAULT ] && PBS_BIN_PATH_DEFAULT=""
PBS_BIN_PATH=${pbs_bin_path:-"$PBS_BIN_PATH_DEFAULT"}

if [ ! -d "$PBS_BIN_PATH" ]; then
  echo "Can't find PBS binary path: <$PBS_BIN_PATH>"
  conf_exit 1
fi

# PBS log path
PBS_LOG_PATH_DEFAULT=/var/spool/pbs/server_logs
[ ! -d $PBS_LOG_PATH_DEFAULT ] && PBS_LOG_PATH_DEFAULT=""
PBS_LOG_PATH=${pbs_log_path:-"$PBS_LOG_PATH_DEFAULT"}

if [ ! -d "$PBS_LOG_PATH" ]; then
  echo "Can't find PBS log file path: <$PBS_LOG_PATH>"
  conf_exit 1
fi

TMP_DIR=${tmpdir:-/tmp}

RUNTIME_CONFIG_DIR=${runtimedir}

# GNU Time
GNU_TIME_DEFAULT=/usr/bin/time
[ ! -f $GNU_TIME_PATH_DEFAULT ] && GNU_TIME_PATH_DEFAULT=
GNU_TIME=${gnu_time:-"$GNU_TIME_DEFAULT"}

export PBS_BIN_PATH PBS_LOG_PATH TMP_DIR RUNTIME_CONFIG_DIR GNU_TIME

#
# nodename
#

NODENAME_PATH_DEFAULT="/bin/hostname"
[ ! -x "$NODENAME_PATH_DEFAULT" ] && NODENAME_PATH_DEFAULT=
[ ! -z "$NODENAME_PATH_DEFAULT" ] && NODENAME_PATH_DEFAULT="$NODENAME_PATH_DEFAULT -f"
NODENAME=${nodename:-"$NODENAME_PATH_DEFAULT"}
export NODENAME

[ -n "$gridmap" ]        && GRIDMAP=$gridmap
[ -n "$x509_user_cert" ] && X509_USER_CERT=$x509_user_cert
[ -n "$x509_user_key"  ] && X509_USER_KEY=$x509_user_key

if [ -z "$mail" ]; then 
  echo "Please specify a mail adress with the mail attribute"
  conf_exit 1
fi

if [ -z "$sessiondir" ]; then 
  echo "Please specify a session directory with the sessiondir attribute"
  conf_exit 1
fi

controldir=${controldir:-"/var/spool/nordugrid/jobstatus"}
if [ ! -d "$controldir" ]; then
  mkdir -p "$controldir"
fi

std_header $NORDUGRID_ETC_LOCATION/grid-manager.conf
(
    echo "statusowner ${statusowner:-yes}"
    echo "joblog ${joblog:-/var/log/gm-jobs.log}"

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

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

    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

    echo "session ${sessiondir}"
    echo "control ${controldir} ."
    echo "helper ${helper:-. %W/bin/parse-%L-log %c}"
    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
    ) >> $NORDUGRID_ETC_LOCATION/grid-manager.conf

fi

}

lockfile=/var/lock/subsys/grid-manager

start()
{
    if [ -r "$NORDUGRID_CONFIG" ]; then
	conf_init
	conf_open $NORDUGRID_CONFIG
    fi

    if conf_match grid-manager; then
      create_gm_config
      $NORDUGRID_LOCATION/sbin/grid-manager $1
      RETVAL=$?
      if [ $RETVAL -ne 0 ]; then
        failure "grid-manager startup"
        conf_close
        return $RETVAL
      fi
      touch $lockfile
    fi

    conf_close
}

stop()
{
    if [ -x $NORDUGRID_LOCATION/sbin/grid-manager ]; then 
      $NORDUGRID_LOCATION/sbin/grid-manager stop
      RETVAL=$?
    fi
    rm -f $lockfile

    return $RETVAL
}

restart()
{
    stop
    start start
    RETVAL=$?
    return $RETVAL
}

# See how we were called.
case "$1" in
  start|lightcleanstart|cleanstart|distcleanstart)
        start $1
        ;;
  stop)
        stop
        ;;
  status)
        status grid-manager
        ;;
  restart|reload)
        restart
        ;;
  condrestart)
        [ -f /var/lock/subsys/grid-manager ] && restart
        ;;
  *)
        echo "Usage: $0 {start|stop|status|restart|reload|condrestart|lightcleanstart|cleanstart|distcleanstart}"
        exit 1
esac

exit $?
