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

function config_storm_gridhttps_plugin_properties_check () {

  requires $1 STORM_GRIDHTTPS_ENABLED
  if [ "x${STORM_GRIDHTTPS_ENABLED}" == "xtrue" ]; then
  	requires $1 STORM_GRIDHTTPS_SERVER_USER_UID STORM_GRIDHTTPS_SERVER_GROUP_UID
  	if [ $? -ne 0 ]; then
  	  yestr ${YEX_MISSINGVAR}
	  yaimlog WARNING "GridHTTPs service user uid or gid not specified,unable to configure StoRM GridHTTPs-plugin. Probably you need to update your YAIM input file with missing information"
	  yaimlog WARNING "${YERRORSTR}"
	  return ${YEX_OK}
  	fi
  fi
  return $?

}

function config_storm_gridhttps_plugin_properties_setenv () {

  yaimlog INFO "No enviroment variables to set."

}

function config_storm_gridhttps_plugin_properties () {

  FUNCTION="config_storm_gridhttps_plugin_properties"
  yaimlog INFO "${FUNCTION}: Configuration file"
  #----------------------------------------------------------
  # /etc/storm/gridhttps-plugin/storm.gridhttps.plugin.properties - BEGIN
  #----------------------------------------------------------
  
  config_dir_prefix="/etc/storm"
  config_dir="${config_dir_prefix}/gridhttps-plugin"
  
  # Change ownership to storm-gridhttps-plugin configuration directory
  if [ -d ${config_dir} ]; then
    chown -R ${STORM_USER}:${STORM_USER} ${config_dir}
  else
    yaimlog ERROR "${FUNCTION}: StoRM gridhttps plugin configuration directory ${config_dir} doesn't exist"
    yestr ${YEX_NOSUCHFILE}
    yaimlog ERROR "${YERRORSTR}"
    return ${YEX_NOSUCHFILE}
  fi
  
  FILE="/etc/storm/gridhttps-plugin/storm.gridhttps.plugin.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)
  
  STORM_GRIDHTTPS_ENABLED=`echo ${STORM_GRIDHTTPS_ENABLED} | tr '[:upper:]' '[:lower:]'`

  
  if [ "x${STORM_GRIDHTTPS_ENABLED}" = "xtrue" ]; then
  #==========================================================
  # Configuration file management
  # If not exists, create it from template
    yaimlog INFO "${FUNCTION}: Creating ${FILE} ..."
	if [ ! -f ${FILE} ]; then
      yaimlog DEBUG "${FUNCTION}: First configuration, create ${FILE} from template ${FILE_T}"
      cp ${FILE_T} ${FILE}
  	else
	  yaimlog DEBUG "${FUNCTION}: 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
    # Remove oldest backup file
    yaimlog DEBUG "${FUNCTION}: Removing old backup files."
    find `dirname "${FILE}"` -ctime +2 -name 'storm.gridhttps.plugin.properties.bkp*' -exec rm {} \;

    # Set permissions
    chmod 640 ${FILE}
    chown ${STORM_USER}:${STORM_USER} ${FILE}
    
    if [ ! "x${STORM_GRIDHTTPS_SERVER_USER_UID}" == "x" ]; then
      set_value "gridhttps.server.user.uid" "${STORM_GRIDHTTPS_SERVER_USER_UID}"    ${FILE}
    else
      yestr ${YEX_MISSINGVAR}
	  yaimlog WARNING "GridHTTPs service user uid not specified,unable to configure StoRM GridHTTPs-plugin. Probably you need to update your YAIM input file with missing information"
	  yaimlog WARNING "${YERRORSTR}"
	  return ${YEX_OK}
    fi
    if [ ! "x${STORM_GRIDHTTPS_SERVER_GROUP_UID}" == "x" ]; then
      set_value "gridhttps.server.user.gid" "${STORM_GRIDHTTPS_SERVER_GROUP_UID}"   ${FILE}
    else
      yestr ${YEX_MISSINGVAR}
	  yaimlog WARNING "GridHTTPs service user gid not specified,unable to configure StoRM GridHTTPs-plugin. Probably you need to update your YAIM input file with missing information"
	  yaimlog WARNING "${YERRORSTR}"
	  return ${YEX_OK}
    fi
    if ! [ -z "$STORM_GRIDHTTPS_MAPPING_SERVICE_PORT" ] || [ "${STORM_GRIDHTTPS_MAPPING_SERVICE_PORT+xxx}" = "xxx" ]; then
       set_value "gridhttps.mapping-service.port" "${STORM_GRIDHTTPS_MAPPING_SERVICE_PORT}"   ${FILE}
    fi
    yaimlog INFO "${FUNCTION}: Successfully created ${FILE} !"
  else
    yaimlog DEBUG "${FUNCTION}: Gridhttps not enabled. No configuration file to produce"
  fi

  #==========================================================

  
  #----------------------------------------------------------
  # /etc/storm/gridhttps-plugin/storm.gridhttps.plugin.properties - END
  #----------------------------------------------------------

  # Exit with success
  return 0

}
