##############################################################################
# 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_apel_sge
#
# DESCRIPTION : This function configures apel to parse SGE logs
#
# AUTHORS     : goncalo@lip.pt,ge-support@listas.cesga.es
#
# NOTES       : 
#
# YAIM MODULE : glite-yaim-sge-utils
#
##############################################################################


function config_apel_sge_check(){
    requires $1 MON_HOST SITE_NAME CE_HOST APEL_DB_PASSWORD BATCH_SERVER BATCH_LOG_DIR
    return $?
}


function config_apel_sge_setenv(){
    return 0
}


function config_apel_sge(){

echo ${BATCH_LOG_DIR} | sed 's/\//\\\//g' > /tmp/batchacclogs
BATCH_LOGS_DIR=`cat /tmp/batchacclogs`
rm -f /tmp/batchacclogs

PARSER_CONFIG_YAIM_FILE=/etc/glite-apel-sge/parser-config-yaim.xml
yaimlog DEBUG "Installing ${PARSER_CONFIG_YAIM_FILE} configuration file for SGE"

cat /etc/glite-apel-sge/parser-config.xml | sed \
    -e "s/localhost/${MON_HOST}/" \
    -e "s/<DBUsername>.*/<DBUsername>accounting<\/DBUsername>/" \
    -e "s/<DBPassword>.*/<DBPassword>${APEL_DB_PASSWORD}<\/DBPassword>/" \
    -e "s/<SubmitHost>.*/<SubmitHost>${CE_HOST}<\/SubmitHost>/" \
    -e "s/<SiteName>.*/<SiteName>${SITE_NAME}<\/SiteName>/" \
    -e "s/<Dir>\/var\/spool\/pbs\/server_priv\/accounting<\/Dir>/<Dir>${BATCH_LOGS_DIR}<\/Dir>/" \
    -e "s/<GIIS>.*/<GIIS>${CE_HOST}<\/GIIS>/" \
    -e "/<DBDeleteProcessor/d" \
    -e "/<ExtraFile/d" \
> ${PARSER_CONFIG_YAIM_FILE}


#*****************************************************************************************
#***************************************** LCG-CE ****************************************
#*****************************************************************************************

if ( echo " ${NODE_TYPE_LIST} " | egrep -q "CE" ) && ( ! echo " ${NODE_TYPE_LIST} " | egrep -q 'creamCE' ); then
cat ${PARSER_CONFIG_YAIM_FILE} | sed \
                -e "s/<BlahdLogPrefix>.*/<BlahdLogPrefix>grid-jobmap_<\/BlahdLogPrefix>/" \
> ${PARSER_CONFIG_YAIM_FILE}.new
fi

#*****************************************************************************************
#***************************************** CreamCE ***************************************
#*****************************************************************************************
if ( echo " ${NODE_TYPE_LIST} " | egrep -q "CE" ) && ( echo " ${NODE_TYPE_LIST} " | egrep -q 'creamCE' ); then
	cat ${PARSER_CONFIG_YAIM_FILE} | sed \
    		-e "s/<BlahdLogPrefix>.*/<BlahdLogPrefix>blahp.log-<\/BlahdLogPrefix>/" \
		-e "s/<Dir>\/opt\/edg\/var\/gatekeeper.*/<Dir>\/var\/log\/cream\/accounting<\/Dir>/" \
> ${PARSER_CONFIG_YAIM_FILE}.new
fi

if [ -e ${PARSER_CONFIG_YAIM_FILE}.new ]; then
	mv -f ${PARSER_CONFIG_YAIM_FILE}.new ${PARSER_CONFIG_YAIM_FILE}
fi

yaimlog DEBUG "Set ownership and permissions to ${PARSER_CONFIG_YAIM_FILE}"
chown root:root ${PARSER_CONFIG_YAIM_FILE}
chmod 600  ${PARSER_CONFIG_YAIM_FILE}

# Remove confusion with two different jobs being called edg-rgma-apel
if [ -f ${CRON_DIR}/edg-rgma-apel ]; then
    rm -f ${CRON_DIR}/edg-rgma-apel
fi

yaimlog DEBUG "Define edg-apel-sge-parser cron job"
cron_job edg-apel-sge-parser root "35 01 * * * env APEL_HOME=/ /usr/bin/apel-sge-log-parser -f ${PARSER_CONFIG_YAIM_FILE} >> /var/log/apel.log 2>&1"

return 0

}
