##############################################################################
# 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_lsf
#
# DESCRIPTION : This function configures APEL accounting for a LSF batch
#		system.
#
# AUTHORS :     Di.Qing@cern.ch massimo.sgaravatto@pd.infn.it
#
# NOTES :       Initially based on config_apel_pbs
#
# YAIM MODULE:  glite-yaim-lsf-utils
#
##############################################################################

function config_apel_lsf_check () {

	requires $1 MON_HOST SITE_NAME CE_HOST \
		APEL_DB_PASSWORD 
}                                                                                                                            

function config_apel_lsf_setenv () {                                                                                         

GLITE_LSFUTILS_LOCATION_ETC=/etc
GLITE_LSFUTILS_LOCATION_BIN=/usr/bin

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

  	### Auxiliary script parameters
	R_CONFIG_YAIM="${GLITE_LSFUTILS_LOCATION_ETC}/glite-apel-lsf/parser-config-yaim.xml"
	PARSER_CONFIG="${GLITE_LSFUTILS_LOCATION_ETC}/glite-apel-lsf/parser-config.xml"

	####@ Check if the parser config template (${GLITE_LSFUTILS_LOCATION_ETC}/glite-apel-lsf/parser-config.xml) exists, exits if not.
	if [ ! -f ${PARSER_CONFIG} ]; then
	    yaimlog ERROR "Apel's parser-config template not found in ${PARSER_CONFIG}"
	    return 1
	fi


       # Find directory containing accounting log files
       # Use BATCH_ACCT_DIR if set. Otherwise use BATCH_LOG_DIR
       if [ -z "$BATCH_ACCT_DIR" ]; then
          if [ -z "$BATCH_LOG_DIR" ]; then 
            # both BATCH_LOG_DIR and BATCH_ACCT_DIR not set
            yaimlog ERROR "Both BATCH_LOG_DIR and BATCH_ACCT_DIR are not set. Don't know where accounting log files are"
            return 1
          else 
            yaimlog DEBUG "BATCH_ACCT_DIR is not set but BATCH_LOG_DIR is set"
            acclogdir=${BATCH_LOG_DIR}
          fi 
       else 
            yaimlog DEBUG "BATCH_ACCT_DIR is set"
            acclogdir=${BATCH_ACCT_DIR}
       fi



	####@ Create a parser config xml file (/etc/glite-apel-lsf/parser-config-yaim.xml)
	yaimlog DEBUG "Creating APEL parser configuration in ${PARSER_CONFIG}"
	cat ${PARSER_CONFIG} | 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/<GIIS>.*/<GIIS>${CE_HOST}<\/GIIS>/" \
    	    -e "s|/var/spool/pbs/server_priv/accounting|${acclogdir}|" \
    	    -e "/<DBDeleteProcessor/d" \
    	    -e "/<ExtraFile/d" \
	> ${R_CONFIG_YAIM}

	####@ Change the ownership and permissions of the created config file.
	chown root:root ${R_CONFIG_YAIM}
	chmod 600 ${R_CONFIG_YAIM}


	####@ Add a cron job (edg-apel-lsf-parser) for periodic configuration.
cron_job glite-apel-lsf-parser root "35 01 * * * env APEL_HOME=/ ${GLITE_LSFUTILS_LOCATION_BIN}/apel-lsf-log-parser -f ${GLITE_LSFUTILS_LOCATION_ETC}/glite-apel-lsf/parser-config-yaim.xml >> /var/log/apel.log 2>&1"

        ### Exit with success
        return 0
}
