#! /bin/bash

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

# load GRIS CPU common code and initialization
. ${libexecdir}/grid-info-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* | vendor_id*)
			vendor="$value"
			;;
                    arch\ *)
		    	arch=`white_strip $value`
			;;
		    cpu\ family*)
			familyno=`white_strip $value`
			;;
		    model\ name* | family*)
			model="$value"
			;;
		    model*)
			modelno=`white_strip $value`
			;;
		    stepping*)
			stepno=`white_strip $value`
			;;
		    revision*)
		    	revisionno=`white_stip $value`
			;;
		    archrev*)
		    	archrevno=`white_strip $value`
			;;
		    cpu\ MHz*)
			speed=`white_strip $value`
			;;
		    cache\ size*)
			cache=`white_strip_first $value`
			;;
		    flags* | features*)
			features=`white_strip $value`
			;;
		    BogoMIPS* | bogomips*)
			# trigger action at end of cpu def'n block
			eval "_cpu_${_cpu}_vendor=\"${vendor}\""
			eval "_cpu_${_cpu}_model=\"${model}\""
			# ia64 is using a different versioning scheme
			# also, cache information is not in /proc/cpuinfo
			if [ "x${arch}" = "xIA-64" ]; then
				eval "_cpu_${_cpu}_version=\"${modelno}.${revisionno}.${archrevno}\""
			
				# L2 cache size
				cache=`cat /proc/pal/cpu${_cpu}/cache_info | \
				  grep -A1 "Data/Instruction Cache level 2" | \
				  tail -n 1 | sed 's/.*: //' | sed 's/ .*//'`
				cache=`echo "${cache}/1024" | bc`
				cache=`white_strip $cache`
			else
				eval "_cpu_${_cpu}_version=\"${familyno}.${modelno}.${stepno}\""
			fi
			
			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
