#!/bin/bash

#NOTE: The values for the gpu TDP in this script are for 3070 MAX-Q
#      You can add cpu/apu options per profile by changing the command in the CPU_CONTROL_COMMAD variable and adding the RyzenADJ or Undervolt command (read the README)
#      Developer: To test the script just run it from the /extra/service directory using the command "su -c ./fancurve-set"

POWER_PROFILE=$(cat /sys/firmware/acpi/platform_profile)
AC_ADAPTER=$(cat /sys/class/power_supply/ADP0/online)
#verfiy Nvidia card is not using vfio
VFIO_LOADED=$(lsmod | grep -w "vfio_pci") #verify vfio
NVIDIA_LOADED=$(lsmod | grep -w "nvidia") #verify nvidia dGPU is loaded

#add run as root
if [ "$EUID" -ne 0 ]
  then echo "Please run as root"
  exit
fi

FOLDER=/etc/legion_linux/
#Please edit this file to enable GPU TDP
source $FOLDER/.env

if  [ $AC_ADAPTER == 1 ]; then
    if [ $POWER_PROFILE == quiet ]; then
        echo "Applying Quiet Mode Profile ﴛ  -> charger..."
        GPU_TDP=$GPU_TDP_AC_Q
        FANCURVE_FILE=$FOLDER/quiet-ac #set the fancurve file
        CPU_CONTROL_COMMAD=$CPU_CC_AC_Q

    elif [ $POWER_PROFILE == balanced ]; then
        echo "Applying Balance Mode Profile   -> charger..."
        GPU_TDP=$GPU_TDP_AC_B
        FANCURVE_FILE=$FOLDER/balanced-ac #set the fancurve file
        CPU_CONTROL_COMMAD=$CPU_CC_AC_B

    elif [ $POWER_PROFILE == performance ]; then
        echo "Applying Performance Mode Profile 龍  -> charger..."
        GPU_TDP=$GPU_TDP_AC_P
        FANCURVE_FILE=$FOLDER/performance-ac #set the fancurve file
        CPU_CONTROL_COMMAD=$CPU_CC_AC_P

    elif [ $POWER_PROFILE == balanced-performance ]; then
        echo "Applying Balance Mode Profile   -> battery..."     
        GPU_TDP=$GPU_TDP_AC_BP
        FANCURVE_FILE=$FOLDER/balanced-performance-ac #set the fancurve file
        CPU_CONTROL_COMMAD=$CPU_CC_AC_BP
    fi
else
    if [ $POWER_PROFILE == quiet ]; then
        echo "Applying Quiet Mode Profile ﴛ  -> battery..."
        GPU_TDP=$GPU_TDP_BAT_Q
        FANCURVE_FILE=$FOLDER/quiet-battery #set the fancurve file
        CPU_CONTROL_COMMAD=$CPU_CC_BAT_Q

    elif [ $POWER_PROFILE == balanced ]; then
        echo "Applying Balance Mode Profile 󰈐  -> battery..."     
        GPU_TDP=$GPU_TDP_BAT_B
        FANCURVE_FILE=$FOLDER/balanced-battery #set the fancurve file
        CPU_CONTROL_COMMAD=$CPU_CC_BAT_B
    
    elif [ $POWER_PROFILE == performance ]; then
        echo "Applying Performance Mode Profile 龍  -> charger..."
        GPU_TDP=$GPU_TDP_BAT_P
        FANCURVE_FILE=$FOLDER/performance-battery #set the fancurve file
        CPU_CONTROL_COMMAD=$CPU_CC_BAT_P

    elif [ $POWER_PROFILE == balanced-performance ]; then
        echo "Applying Balance Mode Profile   -> battery..."     
        GPU_TDP=$GPU_TDP_BAT_BP
        FANCURVE_FILE=$FOLDER/balanced-performance-battery #set the fancurve file
        CPU_CONTROL_COMMAD=$CPU_CC_BAT_BP
    fi
fi

if [[ $TEAM_GREEN -eq 1 && $NVIDIA_LOADED ]]; then
    /opt/bin/nvidia-smi -pl $GPU_TDP
elif [[ $TEAM_RED -eq 1 && $VFIO_LOADED -eq false ]]; then
    /opt/bin/rocm-smi --setpoweroverdrive $GPU_TDP
fi

if [[ $CPU_Control -eq 1 ]]; then
    $CPU_CONTROL_COMMAD
fi

legion_cli fancurve-write-preset-to-hw $FANCURVE_FILE
