#!/bin/bash
# Phone-Pi Power Profile Switcher for RK3566
# Optimizes CPU frequency and governor for battery life vs performance

set -e

PROFILE="${1:-balanced}"
LED_PATH="/sys/class/leds/green:heartbeat"

# Function to set all CPUs
set_all_cpus() {
    local governor="$1"
    local min_freq="$2"
    local max_freq="$3"

    for cpu in 0 1 2 3; do
        echo "$governor" > /sys/devices/system/cpu/cpu$cpu/cpufreq/scaling_governor
        echo "$min_freq" > /sys/devices/system/cpu/cpu$cpu/cpufreq/scaling_min_freq
        echo "$max_freq" > /sys/devices/system/cpu/cpu$cpu/cpufreq/scaling_max_freq
    done

    echo "Power profile: $PROFILE"
    echo "Governor: $governor"
    echo "Frequency: ${min_freq}kHz - ${max_freq}kHz"
}

# Function to control LED
set_led() {
    local mode="$1"
    if [ -d "$LED_PATH" ]; then
        if [ "$mode" = "off" ]; then
            echo none > "$LED_PATH/trigger"
            echo 0 > "$LED_PATH/brightness"
        else
            echo heartbeat > "$LED_PATH/trigger"
        fi
    fi
}

case "$PROFILE" in
    powersave|battery|idle)
        # Ultra battery saver - minimal CPU, LED off
        set_all_cpus "powersave" 408000 816000
        set_led off
        echo "Mode: POWERSAVE - Maximum battery life (idle/display only)"
        ;;

    balanced|normal|auto)
        # Balanced mode - scales as needed, LED heartbeat
        set_all_cpus "ondemand" 408000 1416000
        set_led on
        echo "Mode: BALANCED - Normal use (scales with demand)"
        ;;

    performance|gaming|emulator)
        # Performance mode - full speed, LED heartbeat
        set_all_cpus "performance" 408000 1800000
        set_led on
        echo "Mode: PERFORMANCE - Gaming/emulation (full speed)"
        ;;

    status)
        # Show current status
        echo "Current Power Profile Status:"
        echo "----------------------------"
        for cpu in 0 1 2 3; do
            gov=$(cat /sys/devices/system/cpu/cpu$cpu/cpufreq/scaling_governor)
            cur=$(cat /sys/devices/system/cpu/cpu$cpu/cpufreq/scaling_cur_freq)
            min=$(cat /sys/devices/system/cpu/cpu$cpu/cpufreq/scaling_min_freq)
            max=$(cat /sys/devices/system/cpu/cpu$cpu/cpufreq/scaling_max_freq)
            echo "CPU$cpu: ${cur}kHz (min: ${min}kHz, max: ${max}kHz, governor: $gov)"
        done

        # Get temperature
        temp=$(cat /sys/class/thermal/thermal_zone0/temp)
        temp_c=$((temp / 1000))
        echo "Temperature: ${temp_c}°C"
        
        # LED status
        if [ -f "$LED_PATH/trigger" ]; then
            led_trigger=$(cat "$LED_PATH/trigger" | grep -o '\[[^]]*\]' | tr -d '[]')
            echo "LED: $led_trigger"
        fi
        exit 0
        ;;

    *)
        echo "Usage: $0 {powersave|balanced|performance|status}"
        echo ""
        echo "Profiles:"
        echo "  powersave    - 408-816MHz, powersave governor, LED off"
        echo "  balanced     - 408-1416MHz, ondemand governor, LED heartbeat"
        echo "  performance  - 408-1800MHz, performance governor, LED heartbeat"
        echo "  status       - Show current settings and temperature"
        exit 1
        ;;
esac

# Show new status
sleep 0.5
cpu0_freq=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq)
temp=$(cat /sys/class/thermal/thermal_zone0/temp)
temp_c=$((temp / 1000))
echo "Current: ${cpu0_freq}kHz @ ${temp_c}°C"
