#!/bin/sh
# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2022-24 JELOS (https://github.com/JustEnoughLinuxOS)
# Copyright (C) 2024-present ArchR (https://github.com/archr-linux/Arch-R)

# Minimal OS variable loading for performance
. /etc/profile.d/001-functions
. /etc/profile.d/099-freqfunctions

tocon "Setting performance mode..."

### Enable the desired number of threads.
tocon "Restoring cpu threads..."
NUMTHREADS=$(get_setting "system.threads")
if [ -n "${NUMTHREADS}" ]; then
  onlinethreads ${NUMTHREADS} 0
else
  onlinethreads all 1
fi

### If we don't have a default governor set, set it
### to ondemand if the device supports it, otherwise
### set to performance, but don't enable it.
if [ -z "$(get_setting system.cpugovernor)" ]; then
  GOVTEST="$(awk '/ondemand/ {print $1}' /sys/devices/system/cpu/cpufreq/policy0/scaling_available_governors)"
  if [ -n "${GOVTEST}" ]; then
    set_setting system.cpugovernor ondemand
  else
    set_setting system.cpugovernor performance
  fi
fi

### Set the default GPU performance mode
GPUPERF=$(get_setting system.gpuperf)
if [ -n "${GPUPERF}" ]; then
  gpu_performance_level ${GPUPERF}
fi
