#!/usr/bin/ksh93

if [ "$1" == "-?" ]
then
  cat << TEXT

machine_type returns the model of the AIX server

TEXT

exit
fi

# ----------- exe ----------------

# gets info
procstr=`lsdev -Cc processor | head -1 | cut -d' ' -f1`
proctype=`lsattr -El $procstr | awk '/^type/ {print $2}'`
Hz=`lsattr -El ${procstr%% *} -a frequency -F value`

# computes frequency
if [[ ! -z $Hz ]]
then
    # Convert Hz to MHz, and round up or down
    if (( ($Hz%1000000) >= 500000 ))
    then
    (( MHz=($Hz/1000000) + 1 ))
    else
    (( MHz=($Hz/1000000) ))
    fi
else
        echo MHz rating is not available, exiting
        exit 2
fi

# break the MHz in to the first two digits (enough for the lookup) and the rest
dd=${MHz:0:2}
ff=${MHz:2:9}

# This first attempt does not round up the lower digits!
#  ff=${ff//[0-9]/0}
# so below we round down, round up and round to 50

if(( $ff < 10 ))  # if below 10 i.e.  01 and 09 round down
then
        ff=${ff//[0-9]/0}
elif(( $ff > 90 ))  # if bigger than 90 round up
then
        ff=${ff//[0-9]/0}
        let dd=$dd+1
elif(( $ff > 40 ))
then
        if(( $ff < 60 )) # near 50 so make it 50
        then
                ff=50
        fi
fi

roundedMHz=$dd$ff

# displays result
printf  "%s %s hz\n" $proctype $roundedMHz 
