function config_glite_lb_check(){
  requires $1 MYSQL_PASSWORD INSTALL_ROOT GLITE_LOCATION
}

# check directory existence first
# (most of the dependencies should be in default locations now)
function config_glite_lb_libs_append() {
  prefix="$1"

  if [ x`uname -m` = xx86_64 ]; then
    if [ -d "$prefix/lib64" ]; then
      libarch=lib64
    fi
  fi
  libarch=${libarch:-'lib'}

  if [ -d "$prefix/$libarch" -a "$prefix" != "/usr" ]; then
    yaimgridpath_append LD_LIBRARY_PATH "$prefix/$libarch"
  fi
}

function config_glite_lb_setenv(){

  # Redefine GLITE_USER to value used in packages
  yaimgridenv_set GLITE_USER glite
  # 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"
    exit ${YEX_NOSUCHFILE}
  fi
  yaimgridenv_set GLITE_WMS_QUERY_TIMEOUT "${GLITE_WMS_QUERY_TIMEOUT:-300}"
  yaimgridenv_set GLITE_HOST_CERT ${GLITE_HOME_DIR:-/home/glite}/.certs/hostcert.pem
  yaimgridenv_set GLITE_HOST_KEY ${GLITE_HOME_DIR:-/home/glite}/.certs/hostkey.pem
  yaimgridenv_set X509_CERT_DIR "${X509_CERT_DIR:-/etc/grid-security/certificates}"
  yaimgridenv_set X509_VOMS_DIR "${X509_VOMS_DIR:-/etc/grid-security/vomsdir}"

  yaimgridenv_set GLITE_LB_EXPORT_ENABLED ${GLITE_LB_EXPORT_ENABLED:-false}
  yaimgridenv_set GLITE_LB_EXPORT_PURGE_ARGS "${GLITE_LB_EXPORT_PURGE_ARGS:---cleared 2d --aborted 15d --cancelled 15d --other 60d}"
  yaimgridenv_set GLITE_LB_EXPORT_JPPS ${GLITE_LB_EXPORT_JPPS:-}

  yaimgridenv_set GLITE_LOCATION "${GLITE_LOCATION:-/usr}"
  yaimgridenv_set GLITE_LOCATION_ETC "${GLITE_LOCATION_ETC:-/etc}"
  yaimgridenv_set GLITE_JP_LOCATION "${GLITE_JP_LOCATION:-}"

  yaimgridenv_set GLITE_LB_NOTIF_SOCK "${GLITE_LB_NOTIF_SOCK:-}"
  yaimgridenv_set GLITE_LB_PROXY_SOCK "${GLITE_LB_PROXY_SOCK:-}"
  yaimgridenv_set GLITE_LB_IL_SOCK "${GLITE_LB_IL_SOCK:-}"

  yaimgridenv_set GLITE_LB_SERVER_OTHER_OPTIONS "${GLITE_LB_SERVER_OTHER_OPTIONS}"
  yaimgridenv_set GLITE_LB_NOTIF_IL_OTHER_OPTIONS "${GLITE_LB_NOTIF_IL_OTHER_OPTIONS}"
  yaimgridenv_set GLITE_LB_PROXY_IL_OTHER_OPTIONS "${GLITE_LB_PROXY_IL_OTHER_OPTIONS}"

  #
  # default L&B mode (proxy/server/both) according to the node list,
  # admin can specify GLITE_LB_TYPE, the value is overrided as needed
  #
  # behaviour:
  #   LB:       default 'server', no overriding
  #   WMS:      default 'proxy', override to 'both' if needed
  #   WMS & LB: default 'both', override to 'both' if needed
  #
  use_proxy=0
  use_server=0
  case "$NODE_TYPE_LIST" in
    *WMS*) use_proxy=1 ;;
  esac
  case "$NODE_TYPE_LIST" in
    *LB*) use_server=1 ;;
  esac
  if [ "x${GLITE_LB_TYPE}" = "x" ]; then
    # default
    if [ $use_server -eq 1 ]; then
      if [ $use_proxy -eq 1 ]; then
        GLITE_LB_TYPE=both
      else
        GLITE_LB_TYPE=server
      fi
    else
      if [ $use_proxy -eq 1 ]; then
        GLITE_LB_TYPE=proxy
      fi
    fi
  else
    # override if needed
    if [ $use_proxy -eq 1 ]; then
      case "$GLITE_LB_TYPE" in
        *proxy*|*both*)
          ;;
        *)
          yaimlog WARNING "Overriding L&B mode from '$GLITE_LB_TYPE' to 'both'"
          GLITE_LB_TYPE=both
      esac
    fi
  fi
  yaimgridenv_set GLITE_LB_TYPE ${GLITE_LB_TYPE:-server}

  config_glite_lb_libs_append ${INSTALL_ROOT:-'/opt'}/c-ares
  config_glite_lb_libs_append ${INSTALL_ROOT:-'/opt'}/classads
  config_glite_lb_libs_append ${INSTALL_ROOT:-'/opt'}/log4c
  config_glite_lb_libs_append ${INSTALL_ROOT:-'/opt'}/apr-dev
  config_glite_lb_libs_append ${INSTALL_ROOT:-'/opt'}/aprutil-dev
  config_glite_lb_libs_append ${INSTALL_ROOT:-'/opt'}/activemq-cpp-library
}

function config_glite_lb() {

  #############################################
  #   Logging and Bookkeeping configuration   #
  #############################################
	
  HOSTNAME=`hostname -f`

  yaimlog INFO "Configuring nodes [$NODE_TYPE_LIST], using L&B mode: $GLITE_LB_TYPE"

  # 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"
    exit ${YEX_NOSUCHFILE}
  fi

  # Let Job Provenance Primary Storage as localhost:8901 in LB startup script.
  # More strict check of GLITE_LB_EXPORT_JPPS variable only here.
  if [ x"$GLITE_LB_EXPORT_ENABLED" = x"true" -a -z "$GLITE_LB_EXPORT_JPPS" ]; then
    yaimlog ABORT "Job Provenance Primary Storage for export not set! (GLITE_LB_EXPORT_JPPS)"
    return 1
  fi

  chmod og+rx /var/lib/mysql/ 
  chown mysql:mysql /var/run/mysqld/

  # LCAS plugin
  # config file
  lcas_libarch=lib
  if [ x`uname -m` = xx86_64 ]; then
    if [ -f "$GLITE_LOCATION/lib64/modules/lcas_lb.mod" ]; then
      lcas_libarch=lib64
    fi
  fi
  lcas_plugin="$GLITE_LOCATION/$lcas_libarch/modules/lcas_lb.mod"
  echo "pluginname=\"$lcas_plugin\"" > ${GLITE_LOCATION_ETC}/glite-lb/lcas.db

  # remove config file with defaults for yaim-less setup
  rm -f /etc/sysconfig/glite-lb
  # main L&B setup
  . ${GLITE_LOCATION}/sbin/glite-lb-setup yaim
  if [ $? != 0 ]; then
    yaimlog ABORT "glite-lb-setup failed!"
    return 1
  fi

  # with YAIM let's use only the /etc/init.d/gLite startup
  if egrep -i "Debian|Ubuntu" /etc/issue >/dev/null; then
    update-rc.d glite-lb-bkserverd disable
    update-rc.d glite-lb-locallogger disable
    update-rc.d glite-lb-harvester disable
  else
    /sbin/chkconfig glite-lb-bkserverd off
    /sbin/chkconfig glite-lb-locallogger off
    /sbin/chkconfig glite-lb-harvester off
  fi
  grep -v 'glite-lb-' /etc/gLiteservices 2>/dev/null > /tmp/gLiteservices
  mv /tmp/gLiteservices /etc/gLiteservices
  echo "${GLITE_LOCATION_ETC}/init.d/glite-lb-bkserverd" >> /etc/gLiteservices
  echo "${GLITE_LOCATION_ETC}/init.d/glite-lb-locallogger" >> /etc/gLiteservices
  if [ x"$GLITE_LB_RTM_ENABLED" = x"true" ]; then
    echo "${GLITE_LOCATION_ETC}/init.d/glite-lb-harvester" >> /etc/gLiteservices
  fi

  # set mysql password
  set_mysql_passwd || return 1 # the function uses $MYSQL_PASSWORD

  return 0
}
