#! /bin/sh

# Note that we have a hard-coded shell here, rather than choosing it at
# install time, because this provider doesn't work if run in bash.

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

# load GRIS CPU common code and initialization
. ${libexecdir}/grid-hostinfo-mem-common

# so we know where lscfg and lsattr is
. ${libexecdir}/globus-sh-tools.sh

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

probe_resource_mem ()
{
    totram=0
    ${GLOBUS_SH_LSCFG} | grep mem | \
    {
	while read v1 mem v2
	do
	    memval=`${GLOBUS_SH_LSATTR} -E -l $mem -a size -F value`
	    totram=`$EXPR ${totram} + ${memval}`
	done
    }

    /usr/sbin/lsps -s 2>/dev/null | \
    {
	read header
	IFS=" %MB"
	read swaptot pctused

        pctfree=`$EXPR 100 - ${pctused}`
        swapfree=`$EXPR ${swaptot} \* ${pctfree} \/ 100`

	IFS="$IFS_save"


    }

    _vm_size_mbytes=${swaptot}
    _vm_free_mbytes=${swapfree}
    _mem_size_mbytes=${totram}

     # get free pages of real memory then convert to MB                     
         # vmstat command is in optional acct pkg, so may not be available      
     if [ -n "${GLOBUS_SH_VMSTAT}" ]                                        
         then                                                                   
         _mem_free_mbytes=`${GLOBUS_SH_VMSTAT} | \
                           ${GLOBUS_SH_AWK} ' NR == 4 {print $4}'`          
         _mem_free_mbytes=`${GLOBUS_SH_EXPR} \
                           ${_mem_free_mbytes} \* 4096 / 1000000 `          
     fi                                                                 



    # run this in initialized subshell environment
    emit_resource_mem_descriptions
}

#############
# do the work
probe_resource_mem
