##############################################################################
# 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_namespace
#
# DESCRIPTION : This function configures storage area namespaces on a StoRM SE.
#
# AUTHORS :     grid-release@infn.it
#		M. David (david@lip.pt)
#
# NOTES :
#
# YAIM MODULE:  yaim-storm
#
##############################################################################

function config_storm_namespace_check () {

  requires $1 STORM_BACKEND_HOST STORM_DEFAULT_ROOT VOS STORM_STORAGEAREA_LIST
  return $?

}

function config_storm_namespace_setenv () {

  yaimlog INFO "No enviroment variables to set."

}

function config_storm_namespace () {

  FUNCTION="config_storm_namespace"

  yaimlog INFO "${FUNCTION}: Configuration file"
  #----------------------------------------------------------
  # /etc/storm/backend-server/namespace.xml - BEGIN
  #----------------------------------------------------------
  FILE="/etc/storm/backend-server/namespace.xml"        # 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
  STORM_NAMESPACE_OVERWRITE=`echo ${STORM_NAMESPACE_OVERWRITE} | tr '[:upper:]' '[:lower:]'`   # convert in lowercase
  # If not exists, create it empty
  if [ ! -f ${FILE} ]; then
    yaimlog DEBUG "${FUNCTION}: First configuration, create ${FILE} "
    touch ${FILE}
  # Else if overwrite is enabled, create backup
  elif [ "${STORM_NAMESPACE_OVERWRITE}" = "true" ]; then
    yaimlog DEBUG "${FUNCTION}: Overwrite old configuration file ${FILE}"
    yaimlog DEBUG "${FUNCTION}: Backup old configuration file in ${FILE_B}."
    cp ${FILE} ${FILE_B}
  # Else if overwrite is disabled, create yaimnew
  else
    yaimlog DEBUG "${FUNCTION}: Not overwrite old configuration file ${FILE}"
    yaimlog DEBUG "${FUNCTION}: Create new configuration file in ${FILE_N}"
    FILE="${FILE_N}"
    touch ${FILE}
  fi

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

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

  # Set permissions
  chmod 640 ${FILE}

  #==========================================================
  # Configuration file creation
  yaimlog DEBUG "${FUNCTION}: Create header"
  cat > ${FILE} << EOF
<namespace xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="namespace-1.5.0.xsd" version="1.5.0">
  <filesystems>
EOF

  # Convert in lowercase
  STORM_INFO_FILE_SUPPORT=`echo $STORM_INFO_FILE_SUPPORT | tr '[:upper:]' '[:lower:]'`
  STORM_INFO_RFIO_SUPPORT=`echo $STORM_INFO_RFIO_SUPPORT | tr '[:upper:]' '[:lower:]'`
  STORM_INFO_GRIDFTP_SUPPORT=`echo $STORM_INFO_GRIDFTP_SUPPORT | tr '[:upper:]' '[:lower:]'`
  STORM_INFO_ROOT_SUPPORT=`echo $STORM_INFO_ROOT_SUPPORT | tr '[:upper:]' '[:lower:]'`
  STORM_INFO_HTTP_SUPPORT=`echo $STORM_INFO_HTTP_SUPPORT | tr '[:upper:]' '[:lower:]'`
  STORM_INFO_HTTPS_SUPPORT=`echo $STORM_INFO_HTTPS_SUPPORT | tr '[:upper:]' '[:lower:]'`
  STORM_ANONYMOUS_HTTP_READ=`echo $STORM_ANONYMOUS_HTTP_READ | tr '[:upper:]' '[:lower:]'`

  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}_ROOT;                    SA_ROOT=${!x:-${STORM_DEFAULT_ROOT}/${SA_LOW}}
    x=STORM_${SA_UPP}_FSTYPE;                  SA_FSTYPE=${!x:-${STORM_FSTYPE}}
    x=STORM_${SA_UPP}_USE_GPFS_PREALLOCATION;  SA_USE_GPFS_PREALLOCATION=${!x:-${STORM_INFO_USE_GPFS_PREALLOCATION}}
    y=`echo ${SA_FSTYPE} | tr '[:upper:]' '[:lower:]'`
    case "$y" in
      posixfs)
        SA_FSTYPE=ext3
        SA_FSDRIVER=posixfs
        SA_FSSPACESYSTEM=MockSpaceSystem
        ;;
      gpfs)
        SA_FSTYPE=gpfs
        SA_FSDRIVER=gpfs
        if [ "${SA_USE_GPFS_PREALLOCATION}xxx" == "falsexxx" ] || [ "${SA_USE_GPFS_PREALLOCATION}xxx" == "xxx" ]; then
           SA_FSSPACESYSTEM=MockSpaceSystem
        else
           SA_FSSPACESYSTEM=GPFSSpaceSystem
        fi 
        ;;
      *)
        yaimlog ERROR "${FUNCTION}: Invalid filesystem type '${SA_FSTYPE}'"
        yaimlog ERROR "${FUNCTION}: Please check $x variable in your site-info.def"
        return 1
    esac
    x=STORM_${SA_UPP}_ACLMODE;                 SA_ACLMODE=${!x:-${STORM_ACLMODE}}
    y=`echo ${SA_ACLMODE} | tr '[:upper:]' '[:lower:]'`
    case "$y" in
      aot)
        SA_ACLMODE=AoT
        ;;
      jit)
        SA_ACLMODE=JiT
        ;;
      *)
        yaimlog ERROR "${FUNCTION}: Invalid ACL mode ${SA_ACLMODE}"
        yaimlog ERROR "${FUNCTION}: Please check $x variable in your site-info.def"
        return 1
    esac

    # NOTE: Temporary re-enable default token on namespace
    x=STORM_${SA_UPP}_TOKEN;                   SA_TOKEN=${!x:-${SA_UPP}_TOKEN}
    x=STORM_${SA_UPP}_STORAGECLASS;            SA_STORAGECLASS=${!x:-${STORM_STORAGECLASS}}
    x=STORM_${SA_UPP}_QUOTA;                   SA_QUOTA=${!x:-false}
    x=STORM_${SA_UPP}_QUOTA_DEVICE;            SA_QUOTA_DEVICE=${!x}
    x=STORM_${SA_UPP}_QUOTA_USER;              SA_QUOTA_USER=${!x}
    x=STORM_${SA_UPP}_QUOTA_GROUP;             SA_QUOTA_GROUP=${!x}
    x=STORM_${SA_UPP}_QUOTA_FILESET;           SA_QUOTA_FILESET=${!x}
    x=STORM_${SA_UPP}_GRIDFTP_POOL_STRATEGY;   SA_GRIDFTP_POOL_STRATEGY=${!x:-${STORM_GRIDFTP_POOL_STRATEGY}}
    x=STORM_${SA_UPP}_GRIDFTP_POOL_LIST;       SA_GRIDFTP_POOL_LIST=${!x:-${STORM_GRIDFTP_POOL_LIST}}
    x=STORM_${SA_UPP}_RFIO_HOST;               SA_RFIO_HOST=${!x:-${STORM_RFIO_HOST}}
    x=STORM_${SA_UPP}_ROOT_HOST;               SA_ROOT_HOST=${!x:-${STORM_ROOT_HOST}}
    x=STORM_${SA_UPP}_GRIDHTTP_POOL_STRATEGY;  SA_GRIDHTTP_POOL_STRATEGY=${!x:-${STORM_GRIDHTTP_POOL_STRATEGY}}
    x=STORM_${SA_UPP}_GRIDHTTP_POOL_LIST;      SA_GRIDHTTP_POOL_LIST=${!x:-${STORM_GRIDHTTP_POOL_LIST}};
    x=STORM_${SA_UPP}_HTTP_PORT;               SA_HTTP_PORT=${!x:-${STORM_GRIDHTTPS_HTTP_PORT}}
    x=STORM_${SA_UPP}_GRIDHTTPS_POOL_STRATEGY; SA_GRIDHTTPS_POOL_STRATEGY=${!x:-${STORM_GRIDHTTPS_POOL_STRATEGY}}
    x=STORM_${SA_UPP}_GRIDHTTPS_POOL_LIST;     SA_GRIDHTTPS_POOL_LIST=${!x:-${STORM_GRIDHTTPS_POOL_LIST}};
    x=STORM_${SA_UPP}_HTTPS_PORT;              SA_HTTPS_PORT=${!x:-${STORM_GRIDHTTPS_HTTPS_PORT}}
    x=STORM_${SA_UPP}_FILE_SUPPORT;            SA_FILE_SUPPORT=${!x:-${STORM_INFO_FILE_SUPPORT}}
    x=STORM_${SA_UPP}_GRIDFTP_SUPPORT;         SA_GRIDFTP_SUPPORT=${!x:-${STORM_INFO_GRIDFTP_SUPPORT}}
    x=STORM_${SA_UPP}_RFIO_SUPPORT;            SA_RFIO_SUPPORT=${!x:-${STORM_INFO_RFIO_SUPPORT}}
    x=STORM_${SA_UPP}_ROOT_SUPPORT;            SA_ROOT_SUPPORT=${!x:-${STORM_INFO_ROOT_SUPPORT}}
    x=STORM_${SA_UPP}_HTTP_SUPPORT;            SA_HTTP_SUPPORT=${!x:-${STORM_INFO_HTTP_SUPPORT}}
    x=STORM_${SA_UPP}_HTTPS_SUPPORT;           SA_HTTPS_SUPPORT=${!x:-${STORM_INFO_HTTPS_SUPPORT}}
    x=STORM_${SA_UPP}_AUTH;                    SA_AUTH=${!x:-${STORM_AUTH}}
    x=STORM_${SA_UPP}_DEFAULT_ACL_LIST;        SA_DEFAULT_ACL_LIST=${!x}
    x=STORM_${SA_UPP}_SIZE_LIMIT;              SA_SIZE_LIMIT=${!x:-${STORM_SIZE_LIMIT}}
    # Convert in lowercase
    SA_SIZE_LIMIT=`echo ${SA_SIZE_LIMIT} | tr '[:upper:]' '[:lower:]'`
    # Convert in uppercase
    SA_STORAGECLASS=`echo ${SA_STORAGECLASS} | tr '[:lower:]' '[:upper:]'`
     
    x=STORM_${SA_UPP}_ONLINE_SIZE;           SA_ONLINE_SIZE=${!x}
    x=STORM_${SA_UPP}_NEARLINE_SIZE;         SA_NEARLINE_SIZE=${!x}

    yaimlog DEBUG "${FUNCTION}: Create SA ${SA_UPP} filesystem"
    cat >> ${FILE} << EOF
    <filesystem name="${SA_UPP}-FS" fs_type="${SA_FSTYPE}">
EOF

    if ! [ -z "${SA_TOKEN}" ]; then
      yaimlog DEBUG "${FUNCTION}: Create SA ${SA_UPP} space-token-description (${SA_TOKEN})"
      cat >> ${FILE} << EOF	
      <space-token-description>${SA_TOKEN}</space-token-description>
EOF
    fi

    if [ "${SA_STORAGECLASS}" == "T1D0" ]; then
      if [ -z "${SA_NEARLINE_SIZE}" ] ; then
         yaimlog ERROR "${FUNCTION}: Missing nearline size!"
         yaimlog ERROR "Please set: STORM_${SA_UPP}_NEARLINE_SIZE in GB"
         yestr ${YEX_CONFIG}
         yaimlog ERROR "${YERRORSTR}"
         return ${YEX_CONFIG}
      fi
      if [ "${SA_NEARLINE_SIZE}" == "0" ] || [ "${SA_NEARLINE_SIZE}" == "0.0" ]; then
         yaimlog ERROR "${FUNCTION}: Wrong nearline size!"
         yaimlog ERROR "Please set: STORM_${SA_UPP}_NEARLINE_SIZE > 0 GB"
         yestr ${YEX_CONFIG}
         yaimlog ERROR "${YERRORSTR}"
         return ${YEX_CONFIG}
      fi
      SA_NEARLINE_SIZE=$((${SA_NEARLINE_SIZE}*(1000*1000*1000)))
    else
      if [ ! -z "${SA_NEARLINE_SIZE}" ] && [ "${SA_NEARLINE_SIZE}" -ne "0" ]; then
         yaimlog DEBUG "${FUNCTION}: Wrong Nearline Size setting!"
         yaimlog DEBUG "Set: STORM_${SA_UPP}_NEARLINE_SIZE to 0 GB"
      fi
      SA_NEARLINE_SIZE=0
    fi

    if [ -z "${SA_ONLINE_SIZE}" ]; then
       yaimlog ERROR "${FUNCTION}: Missing online size!"
       yaimlog ERROR "Please set: STORM_${SA_UPP}_ONLINE_SIZE in GB"
       yestr ${YEX_CONFIG}
       yaimlog ERROR "${YERRORSTR}"
       return ${YEX_CONFIG}
    fi
    if [ "${SA_ONLINE_SIZE}" == "0" ] || [ "${SA_ONLINE_SIZE}" == "0.0" ]; then
       yaimlog ERROR "${FUNCTION}: Wrong online size!"
       yaimlog ERROR "Please set: STORM_${SA_UPP}_ONLINE_SIZE > 0 GB"
       yestr ${YEX_CONFIG}
       yaimlog ERROR "${YERRORSTR}"
       return ${YEX_CONFIG}
    fi
    SA_ONLINE_SIZE=$((${SA_ONLINE_SIZE}*(1000*1000*1000)))

    # Check the STORAGECLASS
    SA_RETENTION_POLICY="replica"   # default
    SA_ACCESS_LATENCY="online"      # default
    if ! [ -z "${SA_STORAGECLASS}" ]; then
      case ${SA_STORAGECLASS} in
        "T1D0")
          SA_RETENTION_POLICY="custodial"
          SA_ACCESS_LATENCY="nearline"
          ;;
        "T1D1")
          SA_RETENTION_POLICY="custodial"
          SA_ACCESS_LATENCY="online"
          ;;
        "T0D1")
          SA_RETENTION_POLICY="replica"
          SA_ACCESS_LATENCY="online"
          ;;
        *)
          SA_RETENTION_POLICY="replica"
          SA_ACCESS_LATENCY="online"
          ;;
      esac

      yaimlog DEBUG "${FUNCTION}: Create SA ${SA_UPP} storage-class (${SA_STORAGECLASS}:${SA_RETENTION_POLICY}:${SA_ACCESS_LATENCY})"
      cat >> ${FILE} << EOF
      <storage-class>${SA_STORAGECLASS}</storage-class>
EOF
    fi

    cat >> ${FILE} << EOF
      <root>${SA_ROOT}</root>
      <filesystem-driver>it.grid.storm.filesystem.swig.${SA_FSDRIVER}</filesystem-driver>
      <spacesystem-driver>it.grid.storm.filesystem.${SA_FSSPACESYSTEM}</spacesystem-driver>
      <storage-area-authz>
EOF
    if ( echo " ${SA_AUTH} " | egrep -q "permit-all|deny-all" ); then
      cat >> ${FILE} << EOF
         <fixed>${SA_AUTH}</fixed>
EOF
    else
      cat >> ${FILE} << EOF
         <authz-db>${SA_AUTH}</authz-db>
EOF
    fi
    yaimlog DEBUG "${FUNCTION}: Set Online Size $SA_ONLINE_SIZE for ${SA_UPP}"
    yaimlog DEBUG "${FUNCTION}: Set Nearline Size $SA_NEARLINE_SIZE for ${SA_UPP}"
    cat >> ${FILE} << EOF
      </storage-area-authz>
      <properties>
          <RetentionPolicy>${SA_RETENTION_POLICY}</RetentionPolicy>
          <AccessLatency>${SA_ACCESS_LATENCY}</AccessLatency>
          <ExpirationMode>neverExpire</ExpirationMode>
          <TotalOnlineSize unit="Byte" limited-size="${SA_SIZE_LIMIT}">$SA_ONLINE_SIZE</TotalOnlineSize>
          <TotalNearlineSize unit="Byte">$SA_NEARLINE_SIZE</TotalNearlineSize>
      </properties>
      <capabilities>
        <aclMode>${SA_ACLMODE}</aclMode>
EOF

    if ! [ -z "${SA_DEFAULT_ACL_LIST}" ]; then
      yaimlog DEBUG "${FUNCTION}: Create SA ${SA_UPP} default-acl"
      cat >> ${FILE} << EOF
        <default-acl>
EOF

      for group_permission in ${SA_DEFAULT_ACL_LIST}; do
        # Get GROUPNAME and PERMISSION
        GROUP=`echo "$group_permission" | awk -F":" '{print $1}'`
        PERMISSION=`echo "$group_permission" | awk -F":" '{print $2}'`
        if [ "$?" -eq 0 ]; then
          cat >> ${FILE} << EOF
               <acl-entry><groupName>$GROUP</groupName><permissions>$PERMISSION</permissions></acl-entry>
EOF
        fi
      done

      cat >> ${FILE} << EOF
        </default-acl>
EOF
    fi

    if [ "${SA_FSTYPE}" == "gpfs" ] && [ "${SA_QUOTA}" == "true" ]; then
      yaimlog DEBUG "${FUNCTION}: Create SA ${SA_UPP} quota"
      # Check SA_QUOTA_DEVICE
      if [ -z "${SA_QUOTA_DEVICE}" ]; then
        yaimlog ERROR "${FUNCTION}: Missing quota device definition!"
        yaimlog ERROR "Please set \$STORM_${SA_UPP}_QUOTA_DEVICE variable"
        yestr ${YEX_CONFIG}
        yaimlog ERROR "${YERRORSTR}"
        return ${YEX_CONFIG}
      fi
      # Between SA_QUOTA_USER, SA_QUOTA_GROUP, SA_QUOTA_FILESET consider only the first one set in this order
      if [ ! -z "${SA_QUOTA_USER}" ]; then
        SA_QUOTA_ELEMENT="<userName>${SA_QUOTA_USER}</userName>"
      elif [ ! -z "${SA_QUOTA_GROUP}" ]; then
        SA_QUOTA_ELEMENT="<groupName>${SA_QUOTA_GROUP}</groupName>"
      elif [ ! -z "${SA_QUOTA_FILESET}" ]; then
        SA_QUOTA_ELEMENT="<filesetName>${SA_QUOTA_FILESET}</filesetName>"
      else
        yaimlog ERROR "${FUNCTION}: Missing quota definition!"
        yaimlog ERROR "Please set one between: \$STORM_${SA_UPP}_QUOTA_USER, \$STORM_${SA_UPP}_QUOTA_GROUP, \$STORM_${SA_UPP}_QUOTA_FILESET"
        yestr ${YEX_CONFIG}
        yaimlog ERROR "${YERRORSTR}"
        return ${YEX_CONFIG}
      fi
      cat >> ${FILE} << EOF
        <quota enabled="true">
          <device>${SA_QUOTA_DEVICE}</device>
            <quotaElement>
              ${SA_QUOTA_ELEMENT}
            </quotaElement>
        </quota> 
EOF
    fi

    cat >> ${FILE} << EOF
        <trans-prot>
EOF

    if [ "${SA_FILE_SUPPORT}" == "true" ]; then
      yaimlog DEBUG "${FUNCTION}: Create SA ${SA_UPP} prot (file)"
      cat >> ${FILE} << EOF
          <prot name="file">
            <schema>file</schema>
          </prot>
EOF
    fi

    PROTOCOL_ID=0
    if [ "${SA_GRIDFTP_SUPPORT}" == "true" ]; then
      yaimlog DEBUG "${FUNCTION}: Create SA ${SA_UPP} prot (gsiftp)"
      # gsiftp entry could be multiple  
      COUNTER=1
	  COUNT=$(echo ${SA_GRIDFTP_POOL_LIST} | awk -F ',' '{print NF}')
	  while [ $COUNTER -le $COUNT ]; do
      	SA_GRIDFTP_POOL=$(echo ${SA_GRIDFTP_POOL_LIST} | awk -v current_index=$COUNTER -F ',' '{print $current_index}')
        if [ ! -z "${SA_GRIDFTP_POOL}" ]; then
          SA_GRIDFTP_POOL_HOST=`echo ${SA_GRIDFTP_POOL} | awk '{print $1}'`
          cat >> ${FILE} << EOF
          <prot name="gsiftp">
            <id>$PROTOCOL_ID</id>
            <schema>gsiftp</schema>
            <host>${SA_GRIDFTP_POOL_HOST}</host>
            <port>2811</port>
          </prot>
EOF
          ((PROTOCOL_ID++))
          let COUNTER=COUNTER+1
        fi
      done
    fi

    if [ "${SA_RFIO_SUPPORT}" == "true" ]; then
      yaimlog DEBUG "${FUNCTION}: Create SA ${SA_UPP} prot (rfio)"
      cat >> ${FILE} << EOF
          <prot name="rfio">
            <schema>rfio</schema>
            <host>${SA_RFIO_HOST}</host>
            <port>5001</port>
          </prot>
EOF
    fi

    if [ "${SA_ROOT_SUPPORT}" == "true" ]; then
      yaimlog DEBUG "${FUNCTION}: Create SA ${SA_UPP} prot (root)"
      cat >> ${FILE} << EOF
          <prot name="root">
            <schema>root</schema>
            <host>${SA_ROOT_HOST}</host>
            <!--port>1094</port-->
          </prot>
EOF
    fi
     
    if [ "${SA_HTTP_SUPPORT}" == "true" ]; then
      yaimlog DEBUG "${FUNCTION}: Create SA ${SA_UPP} prot (http)"
      # gsiftp entry could be multiple
      COUNTER=1
	  COUNT=$(echo ${SA_GRIDHTTP_POOL_LIST} | awk -F ',' '{print NF}')
	  while [ $COUNTER -le $COUNT ]; do
      	SA_GRIDHTTP_POOL=$(echo ${SA_GRIDHTTP_POOL_LIST} | awk -v current_index=$COUNTER -F ',' '{print $current_index}')
        if [ ! -z "${SA_GRIDHTTP_POOL}" ]; then
          SA_GRIDHTTP_POOL_HOST=`echo ${SA_GRIDHTTP_POOL} | awk '{print $1}'`
          cat >> ${FILE} << EOF
          <prot name="http">
            <id>$PROTOCOL_ID</id>
            <schema>http</schema>
            <host>${SA_GRIDHTTP_POOL_HOST}</host>
            <port>${SA_HTTP_PORT}</port>
          </prot>
EOF
          ((PROTOCOL_ID++))
          let COUNTER=COUNTER+1
        fi
      done
    fi


    if [ "${SA_HTTPS_SUPPORT}" == "true" ]; then
      yaimlog DEBUG "${FUNCTION}: Create SA ${SA_UPP} prot (https)"
      # gsiftp entry could be multiple
      COUNTER=1
	  COUNT=$(echo ${SA_GRIDHTTPS_POOL_LIST} | awk -F ',' '{print NF}')
	  while [ $COUNTER -le $COUNT ]; do
      	SA_GRIDHTTPS_POOL=$(echo ${SA_GRIDHTTPS_POOL_LIST} | awk -v current_index=$COUNTER -F ',' '{print $current_index}')
        if [ ! -z "${SA_GRIDHTTPS_POOL}" ]; then
          SA_GRIDHTTPS_POOL_HOST=`echo ${SA_GRIDHTTPS_POOL} | awk '{print $1}'`
          cat >> ${FILE} << EOF
          <prot name="https">
            <id>$PROTOCOL_ID</id>
            <schema>https</schema>
            <host>${SA_GRIDHTTPS_POOL_HOST}</host>
            <port>${SA_HTTPS_PORT}</port>
          </prot>
EOF
          ((PROTOCOL_ID++))
          let COUNTER=COUNTER+1
        fi
      done
    fi

    cat >> ${FILE} << EOF
        </trans-prot>
EOF
    PROTOCOL_ID=0
    # gsiftp entry could be use a load balancing strategy
    SA_GRIDFTP_POOL_NUMBER=`echo ${SA_GRIDFTP_POOL_LIST} | awk 'BEGIN{FS=","}{print NF}'`
    if [[ ${SA_GRIDFTP_POOL_NUMBER} > 1 ]]; then
      yaimlog DEBUG "${FUNCTION}: Create SA ${SA_UPP} gsiftp pool"
      cat >> ${FILE} << EOF
        <pool>
          <balance-strategy>${SA_GRIDFTP_POOL_STRATEGY}</balance-strategy>
          <members>
EOF
      # 'weight' strategy case
      if [ ${SA_GRIDFTP_POOL_STRATEGY} = 'weight' ]; then
        COUNTER=1
	    COUNT=$(echo ${SA_GRIDFTP_POOL_LIST} | awk -F ',' '{print NF}')
	    while [ $COUNTER -le $COUNT ]; do
		  SA_GRIDFTP_POOL=$(echo ${SA_GRIDFTP_POOL_LIST} | awk -v current_index=$COUNTER -F ',' '{print $current_index}')
          if [ ! -z "${SA_GRIDFTP_POOL}" ]; then
            SA_GRIDFTP_POOL_WEIGHT=`echo ${SA_GRIDFTP_POOL} | awk '{print $2}'`
            # set to 1 if weight is null
            if [ -z "${SA_GRIDFTP_POOL_WEIGHT}" ]; then
              SA_GRIDFTP_POOL_WEIGHT=100
            fi
            cat >> ${FILE} << EOF
            <member member-id="$PROTOCOL_ID"><weight>${SA_GRIDFTP_POOL_WEIGHT}</weight></member>
EOF
            ((PROTOCOL_ID++))
            let COUNTER=COUNTER+1
          fi
        done
      # other strategy cases
      else
        i=0
        for SA_GRIDFTP_POOL in $(echo ${SA_GRIDFTP_POOL_LIST} | awk 'BEGIN{FS=","}{for (i=1; i<=NF; i++) print $i}'); do
          if [ ! -z "${SA_GRIDFTP_POOL}" ]; then
            cat >> ${FILE} << EOF
            <member member-id="$PROTOCOL_ID"></member>
EOF
            ((PROTOCOL_ID++))
          fi
        done
      fi
      cat >> ${FILE} << EOF
          </members>
       </pool>
EOF
    elif [ "${SA_GRIDFTP_SUPPORT}" == "true" ] && [ ${SA_GRIDFTP_POOL_NUMBER} == 1 ]; then
      ((PROTOCOL_ID++))
    fi
    # http entry could be use a load balancing strategy
    SA_GRIDHTTP_POOL_NUMBER=`echo ${SA_GRIDHTTP_POOL_LIST} | awk 'BEGIN{FS=","}{print NF}'`
    if [[ ${SA_GRIDHTTP_POOL_NUMBER} > 1 ]]; then
      yaimlog DEBUG "${FUNCTION}: Create SA ${SA_UPP} http pool"
      cat >> ${FILE} << EOF
        <pool>
          <balance-strategy>${SA_GRIDHTTP_POOL_STRATEGY}</balance-strategy>
          <members>
EOF
      # 'weight' strategy case
      if [ ${SA_GRIDHTTP_POOL_STRATEGY} = 'weight' ]; then
        COUNTER=1
	    COUNT=$(echo ${SA_GRIDHTTP_POOL_LIST} | awk -F ',' '{print NF}')
	    while [ $COUNTER -le $COUNT ]; do
		  SA_GRIDHTTP_POOL=$(echo ${SA_GRIDHTTP_POOL_LIST} | awk -v current_index=$COUNTER -F ',' '{print $current_index}')
          if [ ! -z "${SA_GRIDHTTP_POOL}" ]; then
            SA_GRIDHTTP_POOL_WEIGHT=`echo ${SA_GRIDHTTP_POOL} | awk '{print $2}'`
            # set to 1 if weight is null
            if [ -z "${SA_GRIDHTTP_POOL_WEIGHT}" ]; then
              SA_GRIDHTTP_POOL_WEIGHT=100
            fi
            cat >> ${FILE} << EOF
            <member member-id="$PROTOCOL_ID"><weight>${SA_GRIDHTTP_POOL_WEIGHT}</weight></member>
EOF
            ((PROTOCOL_ID++))
            let COUNTER=COUNTER+1
          fi
        done
      # other strategy cases
      else
        i=0
        for SA_GRIDHTTP_POOL in $(echo ${SA_GRIDHTTP_POOL_LIST} | awk 'BEGIN{FS=","}{for (i=1; i<=NF; i++) print $i}'); do
          if [ ! -z "${SA_GRIDHTTP_POOL}" ]; then
            cat >> ${FILE} << EOF
            <member member-id="$PROTOCOL_ID"></member>
EOF
            ((PROTOCOL_ID++))
          fi
        done
      fi
      cat >> ${FILE} << EOF
          </members>
       </pool>
EOF
    elif [ "${SA_HTTP_SUPPORT}" == "true" ] && [ ${SA_GRIDHTTP_POOL_NUMBER} == 1 ]; then
      ((PROTOCOL_ID++))
    fi
    # https entry could be use a load balancing strategy
    SA_GRIDHTTPS_POOL_NUMBER=`echo ${SA_GRIDHTTPS_POOL_LIST} | awk 'BEGIN{FS=","}{print NF}'`
    if [[ ${SA_GRIDHTTPS_POOL_NUMBER} > 1 ]]; then
      yaimlog DEBUG "${FUNCTION}: Create SA ${SA_UPP} https pool"
      cat >> ${FILE} << EOF
        <pool>
          <balance-strategy>${SA_GRIDHTTPS_POOL_STRATEGY}</balance-strategy>
          <members>
EOF
      # 'weight' strategy case
      if [ ${SA_GRIDHTTPS_POOL_STRATEGY} = 'weight' ]; then
        COUNTER=1
	    COUNT=$(echo ${SA_GRIDHTTPS_POOL_LIST} | awk -F ',' '{print NF}')
	    while [ $COUNTER -le $COUNT ]; do
		  SA_GRIDHTTPS_POOL=$(echo ${SA_GRIDHTTPS_POOL_LIST} | awk -v current_index=$COUNTER -F ',' '{print $current_index}')
          if [ ! -z "${SA_GRIDHTTPS_POOL}" ]; then
            SA_GRIDHTTPS_POOL_WEIGHT=`echo ${SA_GRIDHTTPS_POOL} | awk '{print $2}'`
            # set to 1 if weight is null
            if [ -z "${SA_GRIDHTTPS_POOL_WEIGHT}" ]; then
              SA_GRIDHTTPS_POOL_WEIGHT=100
            fi
            cat >> ${FILE} << EOF
            <member member-id="$PROTOCOL_ID"><weight>${SA_GRIDHTTPS_POOL_WEIGHT}</weight></member>
EOF
            ((PROTOCOL_ID++))
            let COUNTER=COUNTER+1
          fi
        done
      # other strategy cases
      else
        i=0
        for SA_GRIDHTTPS_POOL in $(echo ${SA_GRIDHTTPS_POOL_LIST} | awk 'BEGIN{FS=","}{for (i=1; i<=NF; i++) print $i}'); do
          if [ ! -z "${SA_GRIDHTTPS_POOL}" ]; then
            cat >> ${FILE} << EOF
            <member member-id="$PROTOCOL_ID"></member>
EOF
            ((PROTOCOL_ID++))
          fi
        done
      fi
      cat >> ${FILE} << EOF
          </members>
       </pool>
EOF
    elif [ "${SA_HTTPS_SUPPORT}" == "true" ] && [ ${SA_GRIDHTTPS_POOL_NUMBER} == 1 ]; then
      ((PROTOCOL_ID++))
    fi
    cat >> ${FILE} << EOF
      </capabilities>
    </filesystem>

EOF

  done

  cat >> ${FILE} << EOF
  </filesystems>
  <mapping-rules>
EOF

  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}_ROOT;                    SA_ROOT=${!x:-${STORM_DEFAULT_ROOT}/${SA_LOW}}
    x=STORM_${SA_UPP}_ACCESSPOINT;             SA_ACCESSPOINT=${!x:-/${SA_LOW}}

    yaimlog DEBUG "${FUNCTION}: Create SA ${SA_UPP} map-rule (${SA_LOW})"
    cat >> ${FILE} << EOF
    <map-rule name="${SA_LOW}-maprule">
      <stfn-root>${SA_ACCESSPOINT}</stfn-root>
      <mapped-fs>${SA_UPP}-FS</mapped-fs>
    </map-rule>
EOF

  done

  cat >> ${FILE} << EOF
  </mapping-rules>
  <approachable-rules>
EOF

  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}_VONAME;                  SA_VONAME=${!x:-${SA_LOW}}
    x=STORM_${SA_UPP}_ANONYMOUS_HTTP_READ;     SA_ANONYMOUS_HTTP_READ=${!x:-${STORM_ANONYMOUS_HTTP_READ}}
    SA_DN_REGEX=""
    DN_REGEX_FIELD=STORM_${SA_UPP}_DN_C_REGEX
    if [ ! "${!DN_REGEX_FIELD}x" == "x" ]; then
       SA_DN_REGEX="${SA_DN_REGEX}/C=${!DN_REGEX_FIELD}"
    fi
    DN_REGEX_FIELD=STORM_${SA_UPP}_DN_O_REGEX
    if [ ! "${!DN_REGEX_FIELD}x" == "x" ]; then
       SA_DN_REGEX="${SA_DN_REGEX}/O=${!DN_REGEX_FIELD}"
    fi
    DN_REGEX_FIELD=STORM_${SA_UPP}_DN_OU_REGEX
    if [ ! "${!DN_REGEX_FIELD}x" == "x" ]; then
       SA_DN_REGEX="${SA_DN_REGEX}/OU=${!DN_REGEX_FIELD}"
    fi
    DN_REGEX_FIELD=STORM_${SA_UPP}_DN_L_REGEX
    if [ ! "${!DN_REGEX_FIELD}x" == "x" ]; then
       SA_DN_REGEX="${SA_DN_REGEX}/L=${!DN_REGEX_FIELD}"
    fi
    DN_REGEX_FIELD=STORM_${SA_UPP}_DN_CN_REGEX
    if [ ! "${!DN_REGEX_FIELD}x" == "x" ]; then
       SA_DN_REGEX="${SA_DN_REGEX}/CN=${!DN_REGEX_FIELD}"
    fi
    DN_REGEX_FIELD=STORM_${SA_UPP}_DN_DC_REGEX
    if [ ! "${!DN_REGEX_FIELD}x" == "x" ]; then
       SA_DN_REGEX="${SA_DN_REGEX}/DC=${!DN_REGEX_FIELD}"
    fi
    if [ -z "${SA_DN_REGEX}" ]; then
       SA_DN_REGEX="*"
    fi
    
    yaimlog DEBUG "${FUNCTION}: Create SA ${SA_UPP} app-rule (${SA_LOW})"
    
    cat >> ${FILE} << EOF
    <app-rule name="${SA_LOW}-rule">
      <subjects>
        <dn>${SA_DN_REGEX}</dn>
	    <vo-name>${SA_VONAME}</vo-name>
      </subjects>
      <approachable-fs>${SA_UPP}-FS</approachable-fs>
      <anonymous-http-read>${SA_ANONYMOUS_HTTP_READ}</anonymous-http-read>    
    </app-rule>
EOF
  done

  cat >> ${FILE} << EOF
  </approachable-rules>
</namespace>
EOF

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

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

  # Exit with success
  return 0

}
