#!/bin/bash
{
	#////////////////////////////////////
	# DietPi CPU Info Script
	#
	#////////////////////////////////////
	# Created by Daniel Knight / daniel.knight@dietpi.com / dietpi.com
	#
	#////////////////////////////////////
	#
	# Info:
	# - Prints CPU information
	#
	# Usage:
	# - /DietPi/dietpi/dietpi-cpuinfo 		Obtain stats and print
	# - /DietPi/dietpi/dietpi-cpuinfo 2 	Generates file of available, ordered scaling freq's $FP_CPU_SCALINGAVAILABLE_FREQ
	#////////////////////////////////////

	#Import DietPi-Globals ---------------------------------------------------------------
	. /DietPi/dietpi/func/dietpi-globals
	G_PROGRAM_NAME='DietPi-CPU_info'
	G_CHECK_ROOT_USER
	G_INIT
	#Import DietPi-Globals ---------------------------------------------------------------

	#DietPi-Globals exit trap addition
	G_EXIT_CUSTOM(){ unset aCPU_CURRENT_FREQ aCPU_MIN_FREQ aCPU_MAX_FREQ aCPU_SCALINGAVAILABLE_FREQ; }

	#Grab Input
	INPUT=0
	disable_error=1 G_CHECK_VALIDINT "$1" && INPUT=$1

	aCPU_CURRENT_FREQ=()
	aCPU_MIN_FREQ=()
	aCPU_MAX_FREQ=()
	Obtain_Cpu_Freq(){

		for ((i=0; i<$(nproc --all); i++))
		do

			if [[ -f /sys/devices/system/cpu/cpu$i/cpufreq/scaling_cur_freq ]]; then

				aCPU_CURRENT_FREQ[$i]=$(</sys/devices/system/cpu/cpu$i/cpufreq/scaling_cur_freq)

			fi

			if [[ -f /sys/devices/system/cpu/cpu$i/cpufreq/scaling_min_freq ]]; then

				aCPU_MIN_FREQ[$i]=$(</sys/devices/system/cpu/cpu$i/cpufreq/scaling_min_freq)

			fi

			if [[ -f /sys/devices/system/cpu/cpu$i/cpufreq/scaling_max_freq ]]; then

				aCPU_MAX_FREQ[$i]=$(</sys/devices/system/cpu/cpu$i/cpufreq/scaling_max_freq)

			fi

		done

	}

	aCPU_SCALINGAVAILABLE_FREQ=()
	CPU_SCALINGSUPPORTED=1
	FP_CPU_SCALINGAVAILABLE_FREQ='/tmp/dietpi-available_cpu_freqs'
	Obtain_Cpu_Scaling_Freq(){

		# - Intel
		if [[ -f /sys/devices/system/cpu/intel_pstate/max_perf_pct ]]; then

			local index=0
			local value=10
			while (( $value <= 100 ))
			do

				aCPU_SCALINGAVAILABLE_FREQ[$index]=$value
				((index++))
				value=$(( $value + 5 ))

			done

		# - Standard
		elif [[ -f /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies ]]; then

			for ((i=0; i<$(nproc --all); i++))
			do

				aCPU_SCALINGAVAILABLE_FREQ[$i]=$(tr ' ' '\n' < /sys/devices/system/cpu/cpu$i/cpufreq/scaling_available_frequencies | sed 's/[^0-9]//g' | sed '/^$/d')

			done

		# - XU4 3.x
		elif [[ -f /sys/devices/system/cpu/cpufreq/mp-cpufreq/cpu_freq_table ]]; then

			aCPU_SCALINGAVAILABLE_FREQ[0]=$(tr ' ' '\n' < /sys/devices/system/cpu/cpufreq/mp-cpufreq/cpu_freq_table | sed 's/[^0-9]//g' | sed '/^$/d')

		else

			CPU_SCALINGSUPPORTED=0

		fi

	}

	CPU_TEMP_CURRENT='N/A'
	CPU_TEMP_PRINT='N/A'
	Obtain_Cpu_Temp(){

		CPU_TEMP_CURRENT=$(G_OBTAIN_CPU_TEMP)

		if disable_error=1 G_CHECK_VALIDINT "$CPU_TEMP_CURRENT"; then

			local cpu_temp_current_f=$(( $CPU_TEMP_CURRENT * 9/5 + 32 ))
			if (( $CPU_TEMP_CURRENT >= 70 )); then

				CPU_TEMP_PRINT="\e[1;31mWarning: $CPU_TEMP_CURRENT'c : $cpu_temp_current_f'f | Reducing the life of your device.\e[0m"

			elif (( $CPU_TEMP_CURRENT >= 60 )); then

				CPU_TEMP_PRINT="\e[38;5;202m$CPU_TEMP_CURRENT'c : $cpu_temp_current_f'f\e[90m | Running hot, not recommended.\e[0m"

			elif (( $CPU_TEMP_CURRENT >= 50 )); then

				CPU_TEMP_PRINT="\e[1;33m$CPU_TEMP_CURRENT'c : $cpu_temp_current_f'f\e[0m \e[90m | Running warm, but safe.\e[0m"

			elif (( $CPU_TEMP_CURRENT >= 40 )); then

				CPU_TEMP_PRINT="\e[1;32m$CPU_TEMP_CURRENT'c : $cpu_temp_current_f'f\e[0m\e[90m | Optimal temperature.\e[0m"

			elif (( $CPU_TEMP_CURRENT >= 30 )); then

				CPU_TEMP_PRINT="\e[1;36m$CPU_TEMP_CURRENT'c : $cpu_temp_current_f'f\e[0m\e[90m | Cool runnings.\e[0m"

			else

				CPU_TEMP_PRINT="\e[1;36m$CPU_TEMP_CURRENT'c : $cpu_temp_current_f'f \e[0m \e[90m| Who put me in the freezer!\e[0m"

			fi

		else

			CPU_TEMP_PRINT=$CPU_TEMP_CURRENT

		fi

	}

	CPU_GOV_CURRENT='N/A'
	Obtain_Cpu_Gov(){

		if [[ -f /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor ]]; then

			CPU_GOV_CURRENT=$(</sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)

		fi

	}

	Print_Stats(){

		echo -e "\n \e[38;5;154m─────────────────────────────────────────────────────\e[0m\n \e[1mDietPi CPU Info\n\e[90m Use dietpi-config to change CPU / performance options\e[0m\n \e[38;5;154m─────────────────────────────────────────────────────\e[0m"

		#VM does not support most info
		(( $G_HW_MODEL == 20 )) && echo -e '\n\e[90m[\e[93mWARNING\e[90m] \e[97mMost CPU information is not available on VM.\e[0m\n'

		#Processor Arch
		echo -e " Architecture \e[90m|\e[0m     $(uname -m)"

		#Current Temp
		echo -e " Temperature  \e[90m|\e[0m     $CPU_TEMP_PRINT"

		#Current Gov
		echo -e " Governor     \e[90m|\e[0m     $CPU_GOV_CURRENT"

		if [[ $CPU_GOV_CURRENT == 'ondemand' || $CPU_GOV_CURRENT == 'conservative' ]]; then

			echo -e " Throttle up  \e[90m|\e[0m     $(grep -m1 '^[[:blank:]]*CONFIG_CPU_USAGE_THROTTLE_UP=' /DietPi/dietpi.txt | sed 's/^[^=]*=//')% CPU usage"

		fi

		#Current Frequency
		(( ${#aCPU_CURRENT_FREQ[@]} )) && echo -e '\n \e[90m                Current Freq    Min Freq   Max Freq\e[0m'
		for ((i=0; i<${#aCPU_CURRENT_FREQ[@]}; i++))
		do

			echo -e " \e[90mCPU$i         |\e[0m      $(( ${aCPU_CURRENT_FREQ[$i]:-0} / 1000 )) MHz      \e[90m$(( ${aCPU_MIN_FREQ[$i]:-0} / 1000 )) MHz    $(( ${aCPU_MAX_FREQ[$i]:-0} / 1000 )) MHz\e[0m"

		done

		echo ''

		#CPU scaling may bump up with script
		G_DIETPI-NOTIFY 2 'CPU current frequency, may be affected by this script, due to the processing required to run it.\n'

	}

	#/////////////////////////////////////////////////////////////////////////////////////
	# Main Loop
	#/////////////////////////////////////////////////////////////////////////////////////
	#-----------------------------------------------------------------------------------

	#Return mode
	if (( $INPUT == 2 )); then

		Obtain_Cpu_Scaling_Freq

		#	Export to file
		> $FP_CPU_SCALINGAVAILABLE_FREQ

		if (( $CPU_SCALINGSUPPORTED )); then

			for i in ${aCPU_SCALINGAVAILABLE_FREQ[@]}
			do

				echo -e "$i" >> $FP_CPU_SCALINGAVAILABLE_FREQ

			done

			#	Order, remove dupes
			sort -u -n $FP_CPU_SCALINGAVAILABLE_FREQ -o $FP_CPU_SCALINGAVAILABLE_FREQ

		fi

	#Print all stats
	else

		Obtain_Cpu_Freq
		Obtain_Cpu_Temp
		Obtain_Cpu_Gov

		Print_Stats

	fi

	#-----------------------------------------------------------------------------------
	exit
	#-----------------------------------------------------------------------------------
}
