#! /bin/bash

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

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

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

_cpu_count=0

### variables used to store probed data
# _cpu_N_vendor=name
# _cpu_N_model=name
# _cpu_N_version=name
# _cpu_N_features=name
# _cpu_N_speed_mhertz=number
# _cpu_N_l2cache_kbytes=number
# _cpu_free1=number
# _cpu_free5=number
# _cpu_free15=number

emit_resource_cpu_summary_attrs ()
{
#<ProcessorLoad xsi:type="LoadType" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
cat <<EOF
<ce:ProcessorLoad 
  ce:Last1Min="${_cpu_free1}"
  ce:Last5Min="${_cpu_free5}"
  ce:Last15Min="${_cpu_free15}"
  xmlns:ce="http://glue.base.ogsa.globus.org/ce/1.1"
/>
EOF
}

emit_resource_cpu_devices ()
{
    i=0
    while [ $i -lt ${_cpu_count} ]
    do
	eval "vendor=\"\${_cpu_${i}_vendor}\""
 	eval "model=\"\${_cpu_${i}_model}\""
 	eval "version=\"\${_cpu_${i}_version}\""
 	eval "features=\"\${_cpu_${i}_features}\""
 	eval "speed=\"\${_cpu_${i}_speed}\""
 	eval "l2cache_size=\"\${_cpu_${i}_l2cache_size}\""
	cat <<EOF
<ce:Processor
  xmlns:ce="http://glue.base.ogsa.globus.org/ce/1.1"
  ce:Vendor="${vendor}"
  ce:Model="${model}"
  ce:Version="${version}"
  ce:OtherProcessorDescription="${features}"
  ce:ClockSpeed="${speed}"
  ce:CacheL2="${l2cache_size}"
/>

EOF

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

emit_resource_cpu_descriptions ()
{
    probe_mds_object_timestamps

    timestamps=`emit_mds_object_timestamps`

    emit_resource_cpu_devices
}

white_strip ()
{
    echo "$@"
}

white_strip_first ()
{
    echo "$1"
}

white_strip_second ()
{
    echo "$2"
}

calculate_freecpus ()
{
    # $1 is cpu count
    # $2 is integer runnable process load
    # $3 is decimal fraction runnable process load

    if [ $3 -gt 0 ]
    then

	case "$3" in
	    08) frac=2 ;;
	    09) frac=9 ;;
	    *) frac=$3 ;;
	esac

	# subtract and carry
	whole=`$EXPR $1 - $2 - 1`
	frac=`$EXPR 100 - $frac`
	if [ $whole -lt 0 ]
	then
	    whole=0
	    frac=0
	elif [ $frac -lt 10 ]
	then
	    frac="0${frac}"
	fi
    else
	whole=`$EXPR $1 - $2`
	frac=00
    fi
    # print fixed precision result as ${whole}.${frac} * 100
    echo "${whole}${frac}"
}

extract_uptime ()
{
    # $1 is "1" "5" or "15" minute average selector
    # $2 ... $N are uptime info tokens

    select=$1
    shift

    while [ $# -gt 3 ]
    do
	shift
    done

    case "$select" in
	1) : ;;
	5) shift ;;
	15) shift 2 ;;
    esac

    echo "$1"
}

probe_resource_cpufree_uptime ()
{
    uptimeinfo=`${GLOBUS_SH_UPTIME-uptime}`

    IFS="${IFS_save},"

    load1=`extract_uptime 1 $uptimeinfo`
    load5=`extract_uptime 5 $uptimeinfo`
    load15=`extract_uptime 15 $uptimeinfo`

    IFS="${IFS_save}"

    IFS="${IFS_save}."
    _cpu_free1=`calculate_freecpus ${_cpu_count} $load1`
    _cpu_free5=`calculate_freecpus ${_cpu_count} $load5`
    _cpu_free15=`calculate_freecpus ${_cpu_count} $load15`
    IFS="${IFS_save}"
}

