##############################################################################
# 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_storm_properties
#
# DESCRIPTION : This function configures storm.properties file on a StoRM SE.
#
# AUTHORS :     grid-release@infn.it
#
# NOTES :
#
# YAIM MODULE:  yaim-storm
#
##############################################################################

function config_storm_properties_check () {

  requires $1 STORM_DB_HOST STORM_DB_PWD STORM_DB_USER \
              STORM_FRONTEND_HOST_LIST STORM_PROXY_HOME STORM_BACKEND_REST_SERVICES_PORT STORM_GRIDHTTPS_ENABLED STORM_CKSUM_ALGORITHM
  return $?

}

function config_storm_properties_setenv () {

  yaimlog INFO "No enviroment variables to set."

}

function config_storm_properties () {

  FUNCTION="config_storm_properties"

JARDIR="/usr/share/java/storm-backend-server"
if [ -f ${JARDIR}/mysql-connector-java-5.1.13-bin.jar ]; then
  # Download needed jar file
  rm -f ${JARDIR}/mysql-connector-java-5.1.13-bin.jar
fi

  yaimlog INFO "${FUNCTION}: Configuration file"
  #----------------------------------------------------------
  # /etc/storm/backend-server/storm.properties - BEGIN
  #----------------------------------------------------------
  FILE="/etc/storm/backend-server/storm.properties"     # Configuration filename
  FILE_T="${FILE}.template"                       # Template configuration filename
  FILE_B="${FILE}.bkp_`date +%Y%m%d_%H%M%S`"      # Backup configuration filename (if needed)
  FILE_N="${FILE}.yaimnew_`date +%Y%m%d_%H%M%S`"  # New configuration filename (if needed)
  
  # Configuration file management
  # If not exists, create it from template
  if [ ! -f ${FILE} ]; then
    yaimlog DEBUG "${FUNCTION}: First configuration, create ${FILE} from template ${FILE_T}"
    cp ${FILE_T} ${FILE}
  # Check storm.properties.version
  else
    yaimlog DEBUG "${FUNCTION}: Check template version"
    get_value "storm.properties.version" ${FILE};     file_ver=${value}
    get_value "storm.properties.version" ${FILE_T};   file_t_ver=${value}
    # If equal, backup and write on old file
    if [ "${file_ver}" == "${file_t_ver}" ]; then
      yaimlog DEBUG "${FUNCTION}: Same version, overwrite old configuration file ${FILE}"
      yaimlog DEBUG "${FUNCTION}: Backup old configuration file in ${FILE_B}"
      cp ${FILE} ${FILE_B}
    # If not equal (different or NULL), backup old file and create new from template
    else
      yaimlog DEBUG "${FUNCTION}: Different or null version, create ${FILE} from template ${FILE_T}"
      yaimlog DEBUG "${FUNCTION}: Backup old configuration file in ${FILE_B}"
      mv ${FILE} ${FILE_B}
      cp ${FILE_T} ${FILE}
    fi
  fi

  yaimlog INFO "${FUNCTION}: Creating ${FILE} ..."

  # Remove oldest backup file
  yaimlog DEBUG "${FUNCTION}: Removing old backup files."
  find `dirname "${FILE}"` -ctime +2 -name 'storm.properties.bkp*' -exec rm {} \;

  # Set permissions
  chmod 640 ${FILE}

  #==========================================================
  # Set default (not in defaults/ because use variables set in services/ file)
  STORM_SURL_ENDPOINT_LIST=${!STORM_SURL_ENDPOINT_LIST:-srm://${STORM_FRONTEND_PUBLIC_HOST}:${STORM_FRONTEND_PORT}${STORM_FRONTEND_PATH}}
  # Convert spaces to commas
  STORM_SURL_ENDPOINT_LIST=`echo ${STORM_SURL_ENDPOINT_LIST} | sed -e 's/ /,/g'`

  set_value "storm.service.FE-list.hostnames"   "${STORM_FRONTEND_HOST_LIST}"            ${FILE}
  set_value "storm.service.FE-public.hostname"  "${STORM_FRONTEND_PUBLIC_HOST}"          ${FILE}
  set_value "storm.service.SURL.endpoint"       "${STORM_SURL_ENDPOINT_LIST}"            ${FILE}
  set_value "storm.service.request-db.host"     "${STORM_DB_HOST}"                       ${FILE}
  set_value "storm.service.request-db.username" "${STORM_DB_USER}"                       ${FILE}
  set_value "storm.service.request-db.passwd"   "${STORM_DB_PWD}"                        ${FILE}
  set_value "proxy.home"                        "${STORM_PROXY_HOME}"                    ${FILE}
  set_value "storm.service.SURL.default-ports"  "${STORM_SERVICE_SURL_DEF_PORTS:-8444}"  ${FILE}
  set_value "storm.rest.services.port"		    "${STORM_BACKEND_REST_SERVICES_PORT}"    ${FILE}
  set_value "checksum.algorithm"                "${STORM_CKSUM_ALGORITHM}"               ${FILE}
  
  
  # Gridhttps stuff
  
  # Convert in lowercase
  STORM_GRIDHTTPS_ENABLED=`echo ${STORM_GRIDHTTPS_ENABLED} | tr '[:upper:]' '[:lower:]'`
  # Convert spaces to commas

  if [ "x${STORM_GRIDHTTPS_ENABLED}" == "xtrue" ]; then
  	set_value "gridhttps.enabled" "true"  ${FILE}
    set_value "gridhttps.plugin.classname" "${STORM_GRIDHTTPS_PLUGIN_CLASSNAME}"  ${FILE}
  else
    set_value "gridhttps.enabled" "false"  ${FILE}
  fi
  #==========================================================

  yaimlog INFO "${FUNCTION}: Successfully created ${FILE} !"
  #----------------------------------------------------------
  # /etc/storm/backend-server/storm.properties - END
  #----------------------------------------------------------

  # Exit with success
  return 0

}
