#!/bin/bash
##############################################################################
# Copyright (c) Members of the EGEE Collaboration. 2011.
# 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_DPM_dmlite
#
# DESCRIPTION : This function configures the DMLite product
#
# AUTHORS :     Alejandro.Alvarez.Ayllon@cern.ch
#
# YAIM MODULE:  glite-yaim-dpm
#
##############################################################################

config_DPM_dmlite_check () {
  requires $1 DMLITE
}


dmlite_conf_set() {
  sed -i "s/^Host .*/DpmHost ${DPM_HOST}/g"                      "${1}"
  sed -i "s/^DpmHost .*/DpmHost ${DPM_HOST}/g"                   "${1}"
  sed -i "s/^NsHost .*/NsHost ${DPM_HOST}/g"                     "${1}"
  sed -i "s/MySqlHost .*/MySqlHost ${DPM_DB_HOST}/g"             "${1}"
  sed -i "s/MySqlUsername .*/MySqlUsername ${DPM_DB_USER}/g"     "${1}"
  sed -i "s/MySqlPassword .*/MySqlPassword ${DPM_DB_PASSWORD}/g" "${1}"
  sed -i "s/NsDatabase .*/NsDatabase ${DPNS_DB}/g"               "${1}"
  sed -i "s/DpmDatabase .*/DpmDatabase ${DPM_DB}/g"              "${1}"
    
  sed -i "s/TokenPassword .*/TokenPassword ${DMLITE_TOKEN_PASSWORD}/g" "${1}"
  sed -i "s/TokenId .*/TokenId ${DMLITE_TOKEN_ID}/g"                   "${1}"
  sed -i "s/TokenLife .*/TokenLife ${DMLITE_TOKEN_LIFE}/g"             "${1}"
}

config_DPM_dmlite () {
  if [ "${DMLITE}" == "no" ]; then
    yaimlog WARNING "DMLITE set to no. DMLite will not be configured"
    return 0
  fi
 
  if [ `uname -m` == "x86_64" ]; then
    LIB="lib64"
  else
    LIB="lib"
  fi
  
  # Validate values
  if [ "${DMLITE_TOKEN_PASSWORD}" == "" ]; then
    yaimlog ERROR "DMLITE set to yes, but DMLITE_TOKEN_PASSWORD not set"
    return 1
  fi

  thishost=`hostname -f`

  # If head, configure head file
  if [ "${DPM_HOST}" == $thishost ]; then
    cat > "${DMLITE_HEAD_CONFIG}" << EOF
LoadPlugin plugin_adapter_dpm /usr/${LIB}/dmlite/plugin_adapter.so

DpmHost ${DPM_HOST}

TokenPassword ${DMLITE_TOKEN_PASSWORD}
TokenId       ${DMLITE_TOKEN_ID}
TokenLife     ${DMLITE_TOKEN_LIFE}
EOF
  fi

  # If disk, configure disk file
  echo "${DPM_FILESYSTEMS}" | grep -q "${thishost}:"
  if [ $? -eq 0 ]; then
   cat > "${DMLITE_DISK_CONFIG}" << EOF
LoadPlugin plugin_fs_io       /usr/${LIB}/dmlite/plugin_adapter.so

DpmHost ${DPM_HOST}

TokenPassword ${DMLITE_TOKEN_PASSWORD}
TokenId       ${DMLITE_TOKEN_ID}
EOF
  fi

  return 0  
}


