##############################################################################
# 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_workload_manager_client
#
# DESCRIPTION : This file configures the workload management client.
#
# AUTHORS :     yaim-contact@cern.ch
#
# NOTES :
#
# YAIM MODULE:  glite-yaim-clients
#
##############################################################################


function config_workload_manager_client_check () {

  if [ "x$RB_HOST" = "x" ]; then
    return 0
  fi

  requires $1 PX_HOST VOS
  return $?
}


function config_workload_manager_client_setenv () {
  if [ "x$RB_HOST" != "x" ]; then
    yaimgridenv_set EDG_WL_LOCATION ${INSTALL_ROOT}/edg
    yaimgridpath_append LD_LIBRARY_PATH ${GLITE_EXTERNAL_ROOT:-}/opt/c-ares/lib
  fi
}

function config_workload_manager_client() {

  if [ "x$RB_HOST" = "x" ]; then
    return 0
  fi

INSTALL_ROOT=${INSTALL_ROOT:-/opt}

if [ $UID -eq 0 ]; then
    OUTPUT_STORAGE=${OUTPUT_STORAGE:-/tmp/jobOutput}
else
    OUTPUT_STORAGE=${OUTPUT_STORAGE:-${HOME}/jobOutput}
fi

for i in $VOS ; do 
	VO=`echo $i | tr '[[:lower:]]' '[[:upper:]]'`
	RBS=`get_vo_param ${VO} RBS`   # try the VO specific RBs
	test -z "$RBS" && RBS=$RB_HOST # fallback to site RB

	# generate the RB list in the correct format
	NS=""
	LB=""
	for RB in $RBS; do
		NS="$NS,\"$RB:7772\""
		LB="$LB,{\"$RB:9000\"}"
	done
	NS=${NS#,}
	LB=${LB#,}

	if [ ! -d $INSTALL_ROOT/edg/etc/$i ]; then
		mkdir -p $INSTALL_ROOT/edg/etc/$i
	fi
	if [ -f $INSTALL_ROOT/edg/etc/$i/edg_wl_ui.conf ]; then
		rm -f $INSTALL_ROOT/edg/etc/$i/edg_wl_ui.conf 
	fi
	cat <<EOF >$INSTALL_ROOT/edg/etc/$i/edg_wl_ui.conf
[
VirtualOrganisation = "$i";
NSAddresses = {$NS};
LBAddresses = {$LB};
## HLR location is optional. Uncomment and fill correctly for
## enabling accounting
#HLRLocation = "fake HLR Location"
## MyProxyServer is optional. Uncomment and fill correctly for
## enabling proxy renewal. This field should be set equal to
## MYPROXY_SERVER environment variable
MyProxyServer = "$PX_HOST"
]
EOF
done

if [ -f $INSTALL_ROOT/edg/etc/edg_wl_ui_cmd_var.conf ]; then
	rm -f $INSTALL_ROOT/edg/etc/edg_wl_ui_cmd_var.conf
fi
cat <<EOF >$INSTALL_ROOT/edg/etc/edg_wl_ui_cmd_var.conf
[
rank = - other.GlueCEStateEstimatedResponseTime;
requirements = other.GlueCEStateStatus == "Production";
RetryCount = 3;
ErrorStorage = "/tmp";
OutputStorage = "${OUTPUT_STORAGE}";
ListenerPort = 44000;
ListenerStorage = "/tmp";
LoggingTimeout = 30;
LoggingSyncTimeout = 30;
# LoggingDestination = "$RB_HOST:9002";
# Default NS logger level is set to 0 (null)
# max value is 6 (very ugly)
NSLoggerLevel = 0;
DefaultLogInfoLevel = 0;
DefaultStatusLevel = 0;
DefaultVo = "unspecified";
]
EOF

if [ -f $INSTALL_ROOT/edg/etc/edg_wl_ui_gui_var.conf ]; then 
	rm -f $INSTALL_ROOT/edg/etc/edg_wl_ui_gui_var.conf
fi
cat <<EOF >$INSTALL_ROOT/edg/etc/edg_wl_ui_gui_var.conf
[
JDLEDefaultSchema = "Glue";
Glue = [
rank = - other.GlueCEStateEstimatedResponseTime;
rankMPI = other.GlueCEStateFreeCPUs;
requirements = other.GlueCEStateStatus == "Production"
];
EDG = [
rank = - other.EstimatedTraversalTime;
rankMPI = other.FreeCPUs;
requirements = true
];
RetryCount = 3;
ErrorStorage = "/tmp";
OutputStorage = "$OUTPUT_STORAGE";
ListenerPort = 44000;
ListenerStorage = "/tmp";
LoggingTimeout = 30;
LoggingSyncTimeout = 30;
# LoggingDestination = "$RB_HOST:9002";
# Default NS logger level is set to 0 (null)
# max value is 6 (very ugly)
NSLoggerLevel = 0;
DefaultLogInfoLevel = 0;
DefaultStatusLevel = 0;
DefaultVo = "unspecified";
]
EOF

if [ ! -d $OUTPUT_STORAGE ]; then
	mkdir $OUTPUT_STORAGE
fi

if [ $UID -eq 0 ]; then
	chmod 1777 $OUTPUT_STORAGE
fi

if [ ! -d ${INSTALL_ROOT}/edg/var/etc/profile.d ]; then
	mkdir -p ${INSTALL_ROOT}/edg/var/etc/profile.d
fi

for i in edg-wl-ui-env.csh edg-wl-ui-env.sh edg-wl-ui-gui-env.csh edg-wl-ui-gui-env.sh; do
	if [ -f ${INSTALL_ROOT}/edg/etc/profile.d/${i} ]; then
		cp -f ${INSTALL_ROOT}/edg/etc/profile.d/${i} ${INSTALL_ROOT}/edg/var/etc/profile.d
	fi
done

return 0

}
