##############################################################################
#
#  Copyright (c) Istituto Nazionale di Fisica Nucleare (INFN). 2006-2011.
#
#  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_used_space
#
# DESCRIPTION : This function provide information about storage area used space on a StoRM SE.
#
# AUTHORS :     storm-support@cnaf.infn.it
#		Michele Dibenedetto (michele.dibenedetto@cnaf.infn.it)
#
# NOTES :
#
# YAIM MODULE:  yaim-storm
#
##############################################################################

function config_storm_used_space_check () {

  requires $1 STORM_STORAGEAREA_LIST
  return $?

}

function config_storm_used_space_setenv () {

  yaimlog INFO "No enviroment variables to set."

}

function config_storm_used_space () {

  FUNCTION="config_storm_used_space"

  yaimlog INFO "${FUNCTION}: Configuration file"
  #----------------------------------------------------------
  # /etc/storm/backend-server/used-space.ini - BEGIN
  #----------------------------------------------------------
  FILE_NAME="used-space.ini"
  FILE="/etc/storm/backend-server/${FILE_NAME}"        # 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)
  
  # If not exists, create it from template (can it be possible?)
  if [ ! -f ${FILE} ]; then
    yaimlog DEBUG "${FUNCTION}: First configuration, create ${FILE} from template ${FILE_T}"
    cp ${FILE_T} ${FILE}
  # Else if overwrite is enabled, create backup
  else
    yaimlog DEBUG "${FUNCTION}: Not delete old configuration file ${FILE}. Creating backup in ${FILE_B} "
    cp ${FILE} ${FILE_B} 
    cp ${FILE_T} ${FILE}
  fi

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

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

  # Set permissions
  chmod 640 ${FILE}

  #==========================================================
  # Configuration file creation
  USED_ONLINE_SIZE_KEY="usedsize"

  for SA in $STORM_STORAGEAREA_LIST; do

    # Convert case
    SA_UPP=`echo ${SA} | tr '[:lower:]' '[:upper:]'`
    SA_LOW=`echo ${SA} | tr '[:upper:]' '[:lower:]'`

    # Remove DNS-like characters from SA_UPP names
    SA_UPP=`echo ${SA_UPP} | sed -e 's/-//g' -e 's/\.//g'`

    x=STORM_${SA_UPP}_TOKEN;                   SA_TOKEN=${!x:-${SA_UPP}_TOKEN}
    x=STORM_${SA_UPP}_USED_ONLINE_SIZE;        SA_USED_ONLINE_SIZE=${!x}
    if [ -n "${SA_USED_ONLINE_SIZE}" ]; then
    	#SA_USED_ONLINE_SIZE is expressed in Bytes
    	yaimlog DEBUG "${FUNCTION}: Create SA ${SA_UPP} Section"
    	cat >> ${FILE} << EOF

[${SA_TOKEN}]
${USED_ONLINE_SIZE_KEY} = ${SA_USED_ONLINE_SIZE}
EOF
	fi
  done

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

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

  # Exit with success
  return 0

}
