#!/bin/sh

# Set default values
APPDIR=${APPDIR:-..}
VERBOSE=${VERBOSE:-0}

. ${APPDIR}/common.subr

sect "CPU information"

arch=$(sysctl -n hw.machine 2>/dev/null)
cores=$(sysctl -n hw.ncpu 2>/dev/null)

cat <<EOF
Machine class:	$arch
CPU Model:	$(sysctl -n hw.model 2>/dev/null)
No. of Cores:	$(sysctl -n hw.ncpu 2>/dev/null)
EOF

if check_privs /var/run/dmesg.boot; then

	echo -e "Cores per CPU:\t$(grep "Cores per" /var/run/dmesg.boot | grep -o "[[:digit:]]")"

	# Check whether our CPU supports amd64 and if so, tell user about
	# it when it is not yet used
	if [ "$arch" != "amd64" ] &&
		( grep -qE 'AMD Features.*LM.*' /var/run/dmesg.boot ); then
			echo
			warn "Your CPU supports amd64 architecture, however you are running $arch."
			warn "You may want to switch to amd64 to get a better performance."
	fi

	if is_verbose 1; then
		subsect "\nCPU Features:"
		grep Feature /var/run/dmesg.boot | tr -d " "

		subsect "\nInterrupt statistics:"
		vmstat -i
	fi
fi

subsect "\nCPU usage statistics:"
# It is required to run top at least twice to get CPU usage statistics
top -d 2 | grep -w CPU

if is_verbose 1 && check_privs /dev/mem; then
	echo
	if which -s dmidecode; then
		echo "Number of processor slots on main board: $(dmidecode -t processor | grep -c Designation)"

		dmidecode -t processor | sed -En 's/^[[:space:]]+//; /Voltage|External|Current|Max|Upgrade/p'
		echo
		info "Run \`dmidecode -t processor\` to see further information."
	else
		# Advice user to install it
		warn "You will need to install the sysutils/dmidecode port in order to obtain further information."
	fi
fi

exit 0
