##############################################################################
# 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_bdii_only
#
# DESCRIPTION : This function configures the 3.1 (resource) BDII
#
# AUTHORS :     Laurence.Field@cern.ch
#
# NOTES :      
#
# YAIM MODULE:  glite-yaim-core
#                 
##############################################################################

config_bdii_only_check () {

  requires $1 BDII_USER BDII_GROUP BDII_BREATHE_TIME BDII_READ_TIMEOUT BDII_ARCHIVE_SIZE BDII_PASSWD
  return $?

}

config_bdii_only(){

if [ ! -d ${INSTALL_ROOT}/bdii/etc ]; then
  mkdir -p ${INSTALL_ROOT}/bdii/etc
fi

if [ ! -d ${INSTALL_ROOT}/bdii/var ]; then
   mkdir -p ${INSTALL_ROOT}/bdii/var
fi 

chown -R ${BDII_USER}:${BDII_GROUP} $INSTALL_ROOT/bdii/etc
chown -R ${BDII_USER}:${BDII_GROUP} $INSTALL_ROOT/bdii/var

if [ ! -f ${INSTALL_ROOT}/bdii/etc/bdii.conf ]; then
  yaimlog ERROR "No bdii.conf file found"
  return 1
fi

# BDII_PASSWD BDII_READ_TIMEOUT BDII_ARCHIVE_SIZE are in bdii 5 and not in 4.
sed -i 's/BDII_IS_CACHE=no/BDII_IS_CACHE=yes/' ${INSTALL_ROOT}/bdii/etc/bdii.conf
mv ${INSTALL_ROOT}/bdii/etc/bdii.conf ${INSTALL_ROOT}/bdii/etc/bdii.conf.old
cat ${INSTALL_ROOT}/bdii/etc/bdii.conf.old | sed -e "s#BDII_USER=.*#BDII_USER=$BDII_USER#" \
  | sed -e "s#BDII_BREATHE_TIME=.*#BDII_BREATHE_TIME=$BDII_BREATHE_TIME#" \
  | sed -e "s#BDII_READ_TIMEOUT=.*#BDII_READ_TIMEOUT=${BDII_READ_TIMEOUT}#" \
  | sed -e "s#BDII_ARCHIVE_SIZE=.*#BDII_ARCHIVE_SIZE=${BDII_ARCHIVE_SIZE}#" > ${INSTALL_ROOT}/bdii/etc/bdii.conf

if [ -f ${INSTALL_ROOT}/bdii/etc/bdii-slapd.conf ]; then
  sed -i  "s/.*rootpw.*/rootpw   ${BDII_PASSWD}/" ${INSTALL_ROOT}/bdii/etc/bdii-slapd.conf
fi

/sbin/chkconfig --add bdii
if [ $? -ne 0 ]; then
  return $?
fi

/sbin/service bdii stop
/sbin/service bdii start

return $?
}
