#!/bin/bash
##############################################################################
# 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_DPM_disk
#
# DESCRIPTION : This function configures the DPM disk node
#
# AUTHORS :     David.Smith@cern.ch, Gergely.Debreczeni@cern.ch
#
# YAIM MODULE:  glite-yaim-dpm
#
##############################################################################

config_DPM_disk_check () {
 requires $1 DPM_HOST
 retcode=$?
 return ${retcode}
}

function config_DPM_disk () {

export DPNS_HOST=${DPM_HOST}
export DPM_HOST=${DPM_HOST}

thisnode=`hostname -f`

####@ Hack to be removed when VOMS or YAIM CORE provides this !
if [ `uname -m` = 'x86_64' ]; then
        if [ `grep -c ^${GLITE_LOCATION}/lib64 /etc/ld.so.conf` = 0 ]; then
                echo "${GLITE_LOCATION}/lib64" >> /etc/ld.so.conf
                /sbin/ldconfig
        fi
else
        if [ `grep -c ^${GLITE_LOCATION}/lib /etc/ld.so.conf` = 0 ]; then
                echo "${GLITE_LOCATION}/lib" >> /etc/ld.so.conf
                /sbin/ldconfig
        fi
fi


# Iterate through the configured pool:host:filesystems
found_fs=0
for fs_entry in $DPM_FILESYSTEMS; do
    pool=`echo $fs_entry | awk -F: '{if ($3) print $1}'`
    host=`echo $fs_entry | awk -F: '{if ($3) print $2; else print $1}'`
    path=`echo $fs_entry | awk -F: '{if ($3) print $3; else print $2}'`

    if [ -z "${pool}" ]; then
        pool=${DPMPOOL}
    fi

    yaimlog DEBUG "Found ${host}:${path} for pool ${pool}"

    # If we are on the right host only
    if [ "${thisnode}" == "${host}" ]; then
        found_fs=1
        # Check wether the filesystem is already part of the pool
        ${LCG_LOCATION}/bin/dpm-qryconf | grep "${thisnode}" | grep -q "${path}"
        if [ $? -eq 0 ]; then
            yaimlog WARNING "$path is already part of a pool! Skipping. "
        else
            mkdir -p "${path}"
            chown ${DPMMGR_USER}:${DPMMGR_GROUP} "${path}"
            yaimlog DEBUG "Executing: ${LCG_LOCATION}/bin/dpm-addfs --poolname $pool --server ${host} --fs $path"
            ${LCG_LOCATION}/bin/dpm-addfs --poolname "${pool}" --server "${host}" --fs "${path}"
            if [ $? -ne 0 ]; then
                yaimlog WARNING "Failed to add the filesytem $path to the pool!"
            fi
        fi
    fi
done
${LCG_LOCATION}/bin/dpm-qryconf

if [ $found_fs -eq 0 ]; then
    yaimlog WARNING "No filesystems configured for this host!"
fi

return 0

}


