#! /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 [ -r $LINUX_CPUINFO ]
    then
	{
	    if [ ! "X${_probe_cache}" = "X" ]
	    then
		${GLOBUS_SH_CAT-cat} 2>/dev/null > "${_probe_cache_tmp}" <<EOF
EOF
	    fi

	    _cpu_count=0
	    _cpu=0
	    IFS=":."
	    while read attr value rest
	    do
		IFS="$IFS_save"

		case "$attr" in
		    processor*)
			_cpu_count=`$EXPR ${_cpu_count} + 1`
			;;
		    vendor_id*)
			vendor="$value"
			;;
		    cpu\ family*)
			familyno=`white_strip $value`
			;;
		    model\ name*)
			model="$value"
			;;
		    model*)
			modelno=`white_strip $value`
			;;
		    stepping*)
			stepno=`white_strip $value`
			;;
		    cpu\ MHz*)
			speed=`white_strip $value`
			;;
		    cache\ size*)
			cache=`white_strip_first $value`
			;;
		    flags*)
			features=`white_strip $value`
			;;
		    bogomips*)
			# trigger action at end of cpu def'n block
			eval "_cpu_${_cpu}_vendor=\"${vendor}\""
			eval "_cpu_${_cpu}_model=\"${model}\""
			eval "_cpu_${_cpu}_version=\"${familyno}.${modelno}.${stepno}\""
			eval "_cpu_${_cpu}_features=\"${features}\""
			eval "_cpu_${_cpu}_speed=\"${speed}\""
			eval "_cpu_${_cpu}_l2cache_size=\"${cache}\""

			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


			_cpu=`$EXPR ${_cpu} + 1`
			;;
		    *) : ;;
		esac
		IFS=":."
	    done

	    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
	} < $LINUX_CPUINFO
	return 0
    else
	ldap_shell_log_error "Linux proc date \"$LINUX_CPUINFO\" not readable"
	return 1
    fi
}

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