##############################################################################
# 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_sched_plugin_pbs
#
# DESCRIPTION : This functions configures the generic information providor
#		for the pbs/torque batch system.
#
# AUTHORS :     Nuno.Orestes.Vaz.da.Silva@cern.ch
#
# NOTES :       Only for EMI.
#
# YAIM MODULE:  glite-yaim-torque-utils
#
##############################################################################

function config_gip_sched_plugin_pbs_check () {

if ( echo "${NODE_TYPE_LIST}" | grep -q CE ); then
  requires $1 CE_BATCH_SYS BATCH_SERVER
fi

}
                                                                                                                             
function config_gip_sched_plugin_pbs_setenv () {
  yaimlog DEBUG "This function currently doesn't set any environment variables."
}
 
function config_gip_sched_plugin_pbs () {

  if [ -z "${TORQUE_VAR_DIR}" ]; then
    yaimlog DEBUG "TORQUE_VAR_DIR not set"
    if [ -d "/var/lib/torque" ]; then
      yaimlog DEBUG "Using /var/lib/torque"
      TORQUE_VAR_DIR="/var/lib/torque"
    else
      if [ -d "/var/torque" ]; then
        yaimlog DEBUG "Using /var/torque"
        TORQUE_VAR_DIR="/var/torque"
      else
         yaimlog ERROR "And not found /var/torque nor /var/lib/torque"
         exit 1
      fi
    fi
  else
    yaimlog DEBUG "TORQUE_VAR_DIR is set"
    TORQUE_VAR_DIR=${TORQUE_VAR_DIR}
  fi

   if ( echo "${NODE_TYPE_LIST}" | grep -q CE ); then

        ### Auxiliary script parameters
	BATCH_SYS=`echo $CE_BATCH_SYS | tr '[:upper:]' '[:lower:]'`
 
        yaimlog DEBUG "Checks if the batch system is correct for this function"
        ####@ Checks if the batch system is correct for this function
        if [ ${BATCH_SYS} != "pbs" ] && [ ${BATCH_SYS} != "torque" ] ; then
            yaimlog ERROR "Running config_gip_sched_plugin_pbs but current CE_BATCH_SYS is ${BATCH_SYS}"
            return 1
        fi

        yaimlog DEBUG "Define the 'max jobs' cmd for the VOs"
	####@ Define the 'max jobs' cmd for the VOs
      	vo_max_jobs_cmd=""
        if ( diagnose -g --host=${BATCH_SERVER} > /dev/null 2>&1 ); then 
      	    vo_max_jobs_cmd="/usr/libexec/vomaxjobs-maui -h ${BATCH_SERVER}"
        fi
        
        yaimlog DEBUG "Add BATCH_SERVER to the $TORQUE_VAR_DIR/server_name file to enable torque clients"
	####@ Add BATCH_SERVER to the $TORQUE_VAR_DIR/server_name file to enable torque clients.
    	echo $BATCH_SERVER > $TORQUE_VAR_DIR/server_name
     	mylrmsinfo=/usr/libexec/lrmsinfo-pbs
         
        yaimlog DEBUG "Create ERT configuration" 
        ####@ Create ERT configuration
 
        cat <<EOF >> /etc/lrms/scheduler.conf
module_search_path : ../lrms:../ett
[LRMS]
lrms_backend_cmd: $mylrmsinfo
[Scheduler]
cycle_time : 0
EOF
 
        if [ "$vo_max_jobs_cmd" ]; then
            echo "vo_max_jobs_cmd: $vo_max_jobs_cmd" >> /etc/lrms/scheduler.conf
        fi
 
  fi
        return 0
}
