#!/bin/bash

SLEEP_INTERVAL=0.05
# 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=$(cat $FILE_SCLK)
  MCLK=$(cat $FILE_MCLK)
  GFXVDD=$(cat $FILE_GFXVDD)
#  TEMP=$(cat $FILE_TEMP)
#  FAN=$(cat $FILE_FAN)
#  POW=$(cat $FILE_POW)
#  BUS=$(cat $FILE_BUS)

#  echo "sclk: `expr $SCLK / 1000000` mclk: `expr $MCLK / 1000000` vdd: $GFXVDD temp: `expr $TEMP / 1000` fan: $FAN pow: `expr $POW / 1000000` bus: $BUS"
  echo "sclk: `expr $SCLK / 1000000` mclk: `expr $MCLK / 1000000` vdd: $GFXVDD"
}

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


# finally start the loop
run_daemon
