#! /bin/bash


# default libexecdir used to bootstrap scripts
libexecdir=${GLOBUS_LOCATION}/libexec

# load GRIS common code and initialization
. ${libexecdir}/grid-info-common

######################################################################

#_fs_count=0
_fs_total_size_mbytes=0
_fs_total_free_mbytes=0
# _fs_N_mount="mount location"
# _fs_N_size_mbytes="number"
# _fs_N_free_mbytes="number"

if [ ${_fs_count} -eq 0 ]
then
    _fs_type=default
else
    _fs_type=scratch
fi

emit_resource_fs_summary_attrs ()
{
${GLOBUS_SH_CAT-cat} <<EOF
${_line_class_av}Mds-Fs-Total-count: ${_fs_count}
${_line_class_av}Mds-Fs-Total-sizeMB: ${_fs_total_size_mbytes}
${_line_class_av}Mds-Fs-Total-freeMB: ${_fs_total_free_mbytes}
EOF
    i=0
    while [ $i -lt ${_fs_count} ]
    do
	eval "size=\${_fs_${i}_size_mbytes}"
	eval "free=\${_fs_${i}_free_mbytes}"
	${GLOBUS_SH_CAT-cat} <<EOF
${_line_class_av}Mds-Fs-sizeMB: $size
${_line_class_av}Mds-Fs-freeMB: $free
EOF
	i=`${EXPR} $i + 1`
    done
}

emit_resource_fs_summary ()
{
    # $1 is dn suffix
    # $2 .. $N are additional object entries
${GLOBUS_SH_CAT-cat} <<EOF
${_line_class_dn}dn: $1
${_line_class_oc}objectclass: MdsFsTotal
EOF
    shift
    while [ $# -gt 0 ]
    do
	${GLOBUS_SH_CAT-cat} <<EOF
$1
EOF
	shift
    done
    emit_resource_fs_summary_attrs | sort | uniq
    echo ""
}

emit_resource_fs_devices ()
{
    i=0
    while [ $i -lt ${_fs_count} ]
    do
	eval "mount=\${_fs_${i}_mount}"
	eval "size=\${_fs_${i}_size_mbytes}"
	eval "free=\${_fs_${i}_free_mbytes}"
	${GLOBUS_SH_CAT-cat} <<EOF
${_line_class_dn}dn: Mds-Device-name=${mount}, $1
${_line_class_oc}objectclass: MdsDevice
${_line_class_oc}objectclass: MdsFs
${_line_class_av}Mds-Device-name: ${mount}
${_line_class_av}Mds-Fs-sizeMB: ${size}
${_line_class_av}Mds-Fs-freeMB: ${free}
${_line_class_av}Mds-Fs-mount: ${mount}
`emit_mds_object_timestamps`

EOF
	i=`$EXPR $i + 1`
    done
}

emit_resource_fs_descriptions ()
{
    probe_mds_object_timestamps

    timestamps=`emit_mds_object_timestamps`

    if [ "X${_dump_host_object}" = "Xtrue" ]
    then
	emit_resource_fs_summary "${_suffix}"
    fi

    if [ "X${_dump_devclass_object}" = "Xtrue" ]
    then
	emit_resource_fs_summary "Mds-Device-Group-name=filesystems, ${_suffix}" "${_line_class_oc}objectclass: MdsDeviceGroup" "${_line_class_av}Mds-Device-Group-name: filesystems" "$timestamps"
    fi

    if [ "X${_dump_dev_objects}" = "Xtrue" ]
    then
	emit_resource_fs_devices "Mds-Device-Group-name=filesystems, ${_suffix}" 
    fi
}

