#!/bin/bash
# Set env
PATH=/usr/sbin:/sbin:/usr/bin:/bin:/etc
#
findpager() {
   #
   # Set the page command
   #
   pgcmd='cat - '
   ck4less=`which less 2>&1 | grep -v "no less"`
   if [ -n "${ck4less}" ]
   then
      pgcmd='less'
   else
      ck4more=`which more 2>&1 | grep -v "no more"`
      if [ -n "${ck4more}" ]
      then
         pgcmd='more'
      fi
   fi
}
#
showlicense() {
   findpager

cat <<EOF | ${pgcmd}

EOF

   exit 0
}
##############################################################################
#
#
#
showUsage() {
   echo "Usage: $0 [-h] [-v] [-l]"
   echo "   -h  Show Usage"
   echo "   -v  Show Version"
   echo "   -l  Show License"
   echo "   -q  Don't show disclaimer"
   exit 0
}
##############################################################################
#
# If any parameters were passed evaluate their usage...
#
showDisclaimer='true'
while getopts lvhq OPT
do
   case ${OPT} in
   l|+l) showlicense;;
   v|+v) echo "Version 1.6"; exit 0;;
   h|+h) showUsage;;
   q|+q) showDisclaimer='false';;
   *) showUsage;;
   esac
done
shift `expr ${OPTIND} - 1`
##############################################################################
#
# Run main program
#
os=$(uname -s)
osVersion=$(uname -r)
osInfo="$os $osVersion"
case $os in
    'SunOS') procInfo=$(psrinfo -pv)
             cpus=$(($(echo "$procInfo"|grep "physical processor"| wc -l)+0))
             cores=$(echo "$procInfo"|grep "physical processor"|awk 'BEGIN {t = 0;}{t += $5;} END {print t;}')
             ck4cores=$(echo "$procInfo"|grep 'cores')
             if [ -z "$ck4cores" ]
             then
                threads=$cores
             else
                threads=$(echo "$procInfo"|grep "physical processor"|awk 'BEGIN {t = 0;}{t += $8;} END {print t;}')
             fi
             cpuSpeed=$(psrinfo -pv|grep clock|uniq|head -1|sed -e "s/^.*clock//g" -e "s/)//g" -e "s/^ //g")
             cpuType="$(prtconf|head -5|tail -1) $cpuSpeed"
             ;;
    'Linux') cpus=$(grep "physical id" /proc/cpuinfo|sort|uniq|wc -l)
             if [ $cpus -eq 0 ];then cpus=$(grep "^processor" /proc/cpuinfo|sort|uniq|wc -l);fi
             cores=$(grep 'core id' /proc/cpuinfo|wc -l)
             if [ $cores -eq 0 ];then cores=$cpus;fi
             #threads=$(lscpu|grep '^CPU(s)'|cut -d: -f2|tr -cd "[:digit:]")
             threads=$(grep "^processor" /proc/cpuinfo|wc -l)
             if [ -e /etc/oracle-release ];         then osInfo=$(head -1 /etc/oracle-release);
             elif [ -e /etc/redhat-release ];       then osInfo=$(head -1 /etc/redhat-release);
             elif [ -e /etc/sun-release ];          then osInfo=$(head -1 /etc/sun-release);
             elif [ -e /etc/sles-release ];         then osInfo=$(head -1 /etc/sles-release);
             elif [ -e /etc/SuSE-release ];         then osInfo=$(head -1 /etc/SuSE-release);
             elif [ -e /etc/novell-release ];       then osInfo=$(head -1 /etc/novell-release);
             elif [ -e /etc/mandrake-release ];     then osInfo=$(head -1 /etc/mandrake-release);
             elif [ -e /etc/mandrakelinux-release ];then osInfo=$(head -1 /etc/mandrakelinux-release);
             elif [ -e /etc/mandriva-release ];     then osInfo=$(head -1 /etc/mandriva-release);
             elif [ -e /etc/fedora-release ];       then osInfo=$(head -1 /etc/fedora-release);
             elif [ -e /etc/gentoo-release ];       then osInfo=$(head -1 /etc/gentoo-release);
             elif [ -e /etc/release ];              then osInfo=$(head -1 /etc/release);
             elif [ -e /etc/lsb-release ];          then . /etc/lsb-release;osInfo=$DISTRIB_DESCRIPTION;
             fi
             cpuType=$(grep "model name" /proc/cpuinfo|uniq| cut -d' ' -f4-|sed -e"s/     //g" -e "s/     //g")
             ;;
    'HP-UX') procInfo=$(machinfo)
             cpus=$(echo $procInfo|grep 'Number of CPUs'|awk '{ print $4 }')
             if [ -z "$cpus" ];then cpus=$(echo $procInfo|egrep ' processors | processor '|awk '{ print $1 }');fi
             cores=$(echo $procInfo|grep 'logical processor'|awk '{ print $1 }')
             if [ -z "$cores" ]
             then
                cores=$cpus
             fi
             threads=$cores
             cpuType=$(echo procInfo|grep 'processor model'| cut -d' ' -f4-)
             ;;
      'AIX') # IBM Doesn't expose processor sockets nor distinguish between cores and virtual 
             # processors, which are a sub-division of cores up to 10:1 via micropartitioning.
	     # However, the default micropartitioning is 1:1.  Consequently, initially the 
             # initial number of virtual processors equals the number of cores.
	     # Fortunately, the number of threads is a known quantity thanks to smtctl.
             cpus=$(lsdev -S A -Cc processor|wc -l)
             cores=$cpus
             threads=$(smtctl|grep bound|wc -l)
             cpuType=$(prtconf|grep "Processor Type"|cut -d: -f2-|sed -e "s/^ //g")
             cpuSpeed=$(prtconf|grep "Clock Speed"|cut -d: -f2-|sed -e "s/^ //g")
             cpuType="$cpuType $cpuSpeed"
             ;;
   'Darwin') cpus=$(sysctl -a hw.packages | awk '{ print $2 }')
             cores=$(sysctl -a hw.physicalcpu | awk '{ print $2 }')
             threads=$(sysctl -a hw.ncpu | awk '{ print $2 }')
             cpuType=$(system_profiler SPHardwareDataType |grep "Processor Name"|cut -d: -f2-| sed -e "s/^ //g")
             ;;
esac
lenOS=$(($(echo "$osInfo" | wc -c) + 0))
if [ $lenOS -le 16 ]; then lenOS=17;fi
lenType=$(($(echo "$cpuType" | wc -c) + 0))
printf "%-6s%-6s%-8s%-${lenOS}s%-${lenType}s\n" 'CPUs' 'Cores' 'Threads' 'Operating System' 'cpuType' 
printf "%-6s%-6s%-8s%-${lenOS}s%-${lenType}s\n" "$cpus" "$cores" "$threads" "$osInfo" "$cpuType"

##############################################################################
#
# Add disclaimer
#
if [ $showDisclaimer == 'false' ]
then
   echo "This script is not guaranteed to show all cores and should not be relied"
   echo "soley upon for determining core counts for licensing purposes. Be sure"
   echo "to validate core counts via other means as well."
fi
