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

BOOST_FILE="/sys/devices/system/cpu/cpufreq/boost"
POLICY="/sys/devices/system/cpu/cpufreq/policy0"

# Get the actual max frequency supported by hardware
MAX_FREQ=$(cat "${POLICY}/cpuinfo_max_freq" 2>/dev/null)

case $1 in
  disable)
    echo "0" > "${BOOST_FILE}" 2>/dev/null
    echo "performance" > "${POLICY}/scaling_governor" 2>/dev/null
    set_setting enable.turbo-mode 0
  ;;
  enable)
    echo "1" > "${BOOST_FILE}" 2>/dev/null
    # Set governor to performance and max freq to hardware maximum
    echo "performance" > "${POLICY}/scaling_governor" 2>/dev/null
    if [ -n "${MAX_FREQ}" ]; then
      echo "${MAX_FREQ}" > "${POLICY}/scaling_max_freq" 2>/dev/null
    fi
    set_setting enable.turbo-mode 1
  ;;
esac
