##############################################################################
# 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_backend
#
# DESCRIPTION : This function start storm-backend service on a StoRM SE backend.
#
# AUTHORS :     grid-release@infn.it
#
# NOTES :       - Change ownership to storm-backend directory
#               - Activate and restart service
#
# YAIM MODULE:  yaim-storm
#
##############################################################################

function config_storm_backend_check () {

  yaimlog INFO "No required variables to set."

}

function config_storm_backend_setenv () {

#  yaimgridenv_set LCMAPS_LOG_FILE "${LCMAPS_LOG_FILE}"
#  yaimgridenv_set LCAS_LOG_FILE "${LCAS_LOG_FILE}"
  yaimlog DEBUG "This function currently doesn't set any environment variables."

}

function config_storm_backend () {

  FUNCTION="config_storm_backend"

  if [ -z ${JAVA_HOME} ] ; then
	yaimlog ERROR "${FUNCTION}: JAVA_HOME variable not set."
    yestr ${YEX_MISSINGVAR}
    yaimlog ERROR "${YERRORSTR}"
    return ${YEX_MISSINGVAR}
  fi

  config_dir_prefix="/etc/storm"
  config_dir="${config_dir_prefix}/backend-server"
  
  # Change ownership to storm-backend-server configuration directory
  if [ -d ${config_dir_prefix} ]; then
   #NOTE: this is not recursive
    chown ${STORM_USER}:${STORM_USER} ${config_dir_prefix}
  else
    yaimlog ERROR "${FUNCTION}: StoRM backend configuration directory prefix ${config_dir_prefix} doesn't exist"
    yestr ${YEX_NOSUCHFILE}
    yaimlog ERROR "${YERRORSTR}"
    return ${YEX_NOSUCHFILE}
  fi
  
  if [ -d ${config_dir} ]; then
    yaimlog INFO "${FUNCTION}: Recursively set '${STORM_USER}:${STORM_USER}' ownership for configuration directory ${config_dir}"
    chown -R ${STORM_USER}:${STORM_USER} ${config_dir}
  else
    yaimlog ERROR "${FUNCTION}: StoRM backend server configuration directory ${config_dir} doesn't exist."
    yestr ${YEX_NOSUCHFILE}
    yaimlog ERROR "${YERRORSTR}"
    return ${YEX_NOSUCHFILE}
  fi

  # temporary fix for version  1.5.1-2 -> 1.5.1-3
  BACKEND_VERSION=`rpm -q --queryformat '%{VERSION}-%{RELEASE}' storm-backend-server`

  # end temporary fix
  #Note:the following instruction is necessary to enable /sbin/service to see the JAVA_LOCATION variable. It already called by setted
  #     config_storm_dir. This note can be usefull in case of the edit/remotion of that function 
  #yaimgridenv_set JAVA_HOME ${JAVA_LOCATION}

  # Activate and restart service
  /sbin/chkconfig --add storm-backend-server
  /sbin/chkconfig storm-backend-server on
  yaimlog INFO "${FUNCTION}: Restart service."
  /sbin/service storm-backend-server stop
  /sbin/service storm-backend-server start

  # Exit with success
  return 0

}
