##############################################################################
# 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_maui_cfg
#
# DESCRIPTION : This function configures maui.
#
# AUTHORS :     Dimitrios Apostolou jimis@email.edunet.gr
#
# NOTES :       -- 
#
# YAIM MODULE:  glite-yaim-torque-utils
#                 
##############################################################################


config_maui_cfg_check () {

requires $1 BATCH_SERVER CE_HOST CONFIG_MAUI
return $?

}

config_maui_cfg_setenv () {

yaimlog DEBUG "Currently this function doesn't set any environment variables."


}

config_maui_cfg () {

if [ "x${CONFIG_MAUI}" = "xyes" ]; then

  yaimlog INFO "configuring maui ..." 
  ####@ configuring Maui ...

  if [ "x${BATCH_SERVER}" != "x${CE_HOST}" ]; then
    AUTH_ADMINHOSTS="${BATCH_SERVER} ${CE_HOST}"
  else
    AUTH_ADMINHOSTS=${CE_HOST}
  fi

  if ! grep -q maui /etc/services; then
    echo "maui		15004/tcp" >> /etc/services
  fi
       
  chmod 0644 /var/spool/maui/maui.cfg

  if [ -f /var/spool/maui/maui.cfg ]; then
    yaimlog DEBUG "Saving the existing maui configuration"
    mv /var/spool/maui/maui.cfg /var/spool/maui/maui.cfg.old
  fi
  cat << EOF > /var/spool/maui/maui.cfg
# MAUI configuration example

SERVERHOST              ${BATCH_SERVER}
ADMIN1                  root
ADMIN3                  edginfo rgma edguser ldap
ADMINHOSTS              ${AUTH_ADMINHOSTS} 
RMCFG[base]             TYPE=PBS
SERVERPORT              40559
SERVERMODE              NORMAL

# Set PBS server polling interval. If you have short # queues or/and jobs it is worth to set a short interval. (10 seconds)

RMPOLLINTERVAL        00:00:10

# a max. 10 MByte log file in a logical location

LOGFILE               /var/log/maui.log
LOGFILEMAXSIZE        10000000
LOGLEVEL              1

# Set the delay to 1 minute before Maui tries to run a job again, # in case it failed to run the first time.
# The default value is 1 hour.

DEFERTIME       00:01:00

# Necessary for MPI grid jobs
ENABLEMULTIREQJOBS TRUE

EOF


  if ( echo "${NODE_TYPE_LIST}" | grep TORQUE_server ) then 
    yaimlog DEBUG "Starting Maui ..." 
    ####@ Add maui as service and restart it 
    /sbin/chkconfig maui on
    /etc/rc.d/init.d/maui stop
    sleep 1
    /etc/rc.d/init.d/maui start
  fi

else
  yaimlog INFO "maui is not going to be configured since CONFIG_MAUI=no"
fi

 return 0

}
