#! /bin/bash

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

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

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

_probe_cache_tmp=${localstatedir}/cpu-cache-$$.tmp

# set mode for cache file
umask 077

cleanup ()
{
    rm -f "$_probe_cache_tmp"
}

trap cleanup 0

cleanup

report_resource_cpu ()
{
	{
	    if [ ! "X${_probe_cache}" = "X" ]
	    then
		${GLOBUS_SH_CAT-cat} 2>/dev/null > "${_probe_cache_tmp}" <<EOF
EOF
	    fi

	    _cpu_count=1
	    _cpu=0
	    IFS=":."

	    # trigger action at end of cpu def'n block
	    eval "_cpu_${_cpu}_vendor=\"unknown vendor\""
	    eval "_cpu_${_cpu}_model=\"unknown model\""
	    eval "_cpu_${_cpu}_version=\"unknown version\""
	    eval "_cpu_${_cpu}_features="
	    eval "_cpu_${_cpu}_speed=0"
	    eval "_cpu_${_cpu}_l2cache_size=0"

	    if [ ! "X${_probe_cache}" = "X" ]
	    then
		${GLOBUS_SH_CAT-cat} 2>/dev/null >> "${_probe_cache_tmp}" <<EOF
_cpu_${_cpu}_vendor="${vendor}"
_cpu_${_cpu}_model="${model}"
_cpu_${_cpu}_version="${familyno}.${modelno}.${stepno}"
_cpu_${_cpu}_features="${features}"
_cpu_${_cpu}_speed="${speed}"
_cpu_${_cpu}_l2cache_size="${cache}"
EOF
	    fi


	    if [ ! "X${_probe_cache}" = "X" ]
	    then
		${GLOBUS_SH_CAT-cat} 2>/dev/null >> "${_probe_cache_tmp}" <<EOF
_cpu_count="${_cpu_count}"
EOF
		mv "${_probe_cache_tmp}" "${_probe_cache}" 2> /dev/null
	    fi

	    probe_resource_cpufree_uptime

	    # run this in initialized subshell environment
	    emit_resource_cpu_descriptions
	}
}

#############
# do the work
report_resource_cpu
