#!/bin/bash

SLEEP_INTERVAL=0.01
# set temps (in degrees C * 1000) and corresponding pwm values in ascending order and with the same amount of values
# hwmon paths, hardcoded for one amdgpu card at hwmon0, adjust as needed
FILE_SCLK=/sys/class/drm/card0/device/hwmon/hwmon0/freq1_input
FILE_MCLK=/sys/class/drm/card0/device/hwmon/hwmon0/freq2_input
FILE_TEMP=/sys/class/drm/card0/device/hwmon/hwmon0/temp1_input
FILE_FAN=/sys/class/drm/card0/device/hwmon/hwmon0/fan1_input
FILE_GFXVDD=/sys/class/drm/card0/device/hwmon/hwmon0/in0_input
FILE_POW=/sys/class/drm/card0/device/hwmon/hwmon0/power1_average
FILE_BUS=/sys/class/drm/card0/device/gpu_busy_percent
FILE_PWM=/sys/class/drm/card0/device/hwmon/hwmon0/pwm1

# checking for privileges
if [ $UID -ne 0 ]
then
  echo "Writing to sysfs requires privileges, relaunch as root"
  exit 1
fi

function read_output {

  SCLK=$(expr $(cat $FILE_SCLK) / 1000000)
  MCLK=$(expr $(cat $FILE_MCLK) / 1000000)
  GFXVDD=$(cat $FILE_GFXVDD)
#  TEMP=$(expr $(cat $FILE_TEMP) / 1000)
#  FAN=$(cat $FILE_FAN)
#  POW=$(expr $(cat $FILE_POW) / 1000000)
#  BUS=$(cat $FILE_BUS)
#  PWM=$(cat $FILE_PWM)

#  echo "sclk: $SCLK mclk: $MCLK vdd: $GFXVDD temp: $TEMP fan: $FAN pow: $POW bus: $BUS"
  echo "sclk: $SCLK mclk: $MCLK vdd: $GFXVDD"

}

function run_daemon {
  while :; do
    read_output
    sleep $SLEEP_INTERVAL
  done
}


# finally start the loop
run_daemon