##############################################################################
# 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_glite_locallogger
#
# DESCRIPTION : This function configures the Local Logger.
#
# AUTHORS :     Di.Qing@cern.ch
#
# NOTES :      
#
# YAIM MODULE:  glite-yaim-core
#                 
##############################################################################

function config_glite_locallogger_check () {

  requires $1 GLITE_USER X509_CERT_DIR GLITE_LOCATION GRID_ENV_LOCATION
  ret=$?

  if (echo "${NODE_TYPE_LIST}" | grep WMS > /dev/null); then
    requires $1 GLITE_LB_TYPE
    let "ret |= $?"
  fi

  return $ret
}

function config_glite_locallogger_setenv(){

  yaimgridenv_set GLITE_USER ${GLITE_USER}
  # Redefine GLITE_HOME_DIR to make sure we retrieve the correct HOME directory of user glite
  GLITE_HOME_DIR=`getent passwd ${GLITE_USER} | cut -d: -f6`
  if [ "x${GLITE_HOME_DIR}" = "x" ]; then
    yaimlog ERROR "The home directory of ${GLITE_HOME_DIR} doesn't exist. Check whether the user ${GLITE_USER} was properly created"
    yestr ${YEX_NOSUCHFILE}
    yaimlog ERROR "${YERRORSTR}"
    exit ${YEX_NOSUCHFILE}
  fi
  yaimgridenv_set GLITE_HOST_CERT ${GLITE_HOME_DIR}/.certs/hostcert.pem
  yaimgridenv_set GLITE_HOST_KEY ${GLITE_HOME_DIR}/.certs/hostkey.pem
  yaimgridenv_set X509_CERT_DIR ${X509_CERT_DIR}

}

function config_glite_locallogger() {

  if ( echo "${NODE_TYPE_LIST}" | grep WMS > /dev/null ) && [ "x${GLITE_LB_TYPE}" == "xproxy" ]; then
    yaimlog INFO "GLITE_LB_TYPE=proxy: nothing to do."
    return  0
fi 

  # Redefine GLITE_HOME_DIR to make sure we retrieve the correct HOME directory of user glite
  GLITE_HOME_DIR=`getent passwd ${GLITE_USER} | cut -d: -f6`
  if [ "x${GLITE_HOME_DIR}" = "x" ]; then
    yaimlog ERROR "The home directory of ${GLITE_USER} doesn't exist. Check whether the user ${GLITE_USER} was properly created"
    yestr ${YEX_NOSUCHFILE}
    yaimlog ERROR "${YERRORSTR}"
    exit ${YEX_NOSUCHFILE}
  fi


  #############################################
  #   locallogger configuration               #
  #############################################
	
  mkdir -p $GLITE_HOME_DIR/.certs
  chown $GLITE_USER:$GLITE_USER $GLITE_HOME_DIR/.certs
  chmod 0755 $GLITE_HOME_DIR/.certs
  cp -f /etc/grid-security/hostcert.pem /etc/grid-security/hostkey.pem $GLITE_HOME_DIR/.certs/
  if [ ! $? = 0 ] ; then
    yaimlog WARNING "Please copy host certificate and key into /etc/grid-security and"
    yaimlog WARNING "  $GLITE_HOME_DIR/.certs/, change the owner of the ones in"
    yaimlog WARNING "  $GLITE_HOME_DIR/.certs/ to $GLITE_USER"
  fi
  chown $GLITE_USER:$GLITE_USER $GLITE_HOME_DIR/.certs/hostcert.pem $GLITE_HOME_DIR/.certs/hostkey.pem
  chmod 0644 $GLITE_HOME_DIR/.certs/hostcert.pem
  chmod 0400 $GLITE_HOME_DIR/.certs/hostkey.pem

  # The workaround for bug 22389
  if [ -f /etc/cron.d/locallogger.cron ]; then 
    yaimlog INFO "The workaround for bug 22389 already exists"
  else
    yaimlog INFO "Applying the workaround for bug 22389..."

cat << EOF > /etc/cron.d/locallogger.cron
HOME=/ 
MAILTO=$SITE_EMAIL

0 */6 * * * root touch $GLITE_HOME_DIR/.certs/hostcert.pem
EOF

  fi

  # Start services
  if [ ! -f /etc/gLiteservices ] ; then
    touch /etc/gLiteservices
  fi

  grep glite-lb-locallogger /etc/gLiteservices > /dev/null
  if [ ! $? = 0 ] ; then
    echo "/etc/init.d/glite-lb-locallogger" >> /etc/gLiteservices
  fi

  . /opt/glite/yaim/etc/grid-env-funcs.sh
  . ${GRID_ENV_LOCATION}/grid-env.sh
  /etc/init.d/glite-lb-locallogger restart
}
