#!/bin/bash


# if not have bat file use power-saver as default
if [ ! -e "/var/lib/power-profiles-daemon/state_bat.conf" ]; then
    echo power-saver > /var/lib/power-profiles-daemon/state_bat.conf
fi

# if not have ac file use power-saver as default
if [ ! -e "/var/lib/power-profiles-daemon/state_ac.conf" ]; then
    echo balanced > /var/lib/power-profiles-daemon/state_bat.conf
fi

# Read from power profiles daemon file governor to use
powerProfile="$(grep 'Profile=' /var/lib/power-profiles-daemon/state.ini)"

# Read available governors
numGovernors="$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors | wc -w)"

# Verify if have only 2 governors available and name is powersave and performance
if [ "$numGovernors" = "2" -a "$(grep -o -e powersave -e performance /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors | wc -l)" = "2" ]; then
    performance="performance"
    balanced="powersave"
    powersave="powersave"
fi

# Verify if have more than 2 governors and have conservative, performance and schedutil
if [ "$numGovernors" -gt "2" -a "$(grep -o -e conservative -e performance -e schedutil /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors | wc -l)" = "3" ]; then
    performance="performance"
    balanced="schedutil"
    powersave="conservative"
fi


# Convert result to governor style
if [ "${powerProfile##*=}" = "performance" ]; then
    governorApply=$performance

elif [ "${powerProfile##*=}" = "balanced" ]; then
    governorApply=$balanced

elif [ "${powerProfile##*=}" = "power-saver" ]; then
    governorApply=$powersave
fi


if [ "$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)" != "$governorApply" ]; then
    # Apply governor change
    if [[ $balanced = schedutil ]]; then
        cpupower frequency-set -g $governorApply
    fi
fi

# Fix to recurrent problem with daemon block
# systemctl restart power-profiles-daemon-biglinux-start
#change configs on TLP
System_CPU_ENERGY_PERF_POLICY=$(< /sys/devices/system/cpu/cpu0/cpufreq/energy_performance_preference)


if [ "$(acpi -a | awk '{print $3}' | head -n1)" = "off-line" ];then
    if [[ -e /sys/devices/system/cpu/cpu0/cpufreq/energy_performance_preference ]]; then
        sed -Ei "s/#?CPU_ENERGY_PERF_POLICY_ON_BAT=.*/CPU_ENERGY_PERF_POLICY_ON_BAT=$System_CPU_ENERGY_PERF_POLICY/g" /etc/tlp.conf
        sed -Ei "s/#?CPU_SCALING_GOVERNOR_ON_BAT=.*/#CPU_SCALING_GOVERNOR_ON_BAT=$governorApply/" /etc/tlp.conf
    else
        if [[ $balanced = schedutil ]]; then
            sed -Ei "s/#?CPU_SCALING_GOVERNOR_ON_BAT=.*/CPU_SCALING_GOVERNOR_ON_BAT=$governorApply/" /etc/tlp.conf
        else
            sed -Ei "s/#?CPU_SCALING_GOVERNOR_ON_BAT=.*/#CPU_SCALING_GOVERNOR_ON_BAT=$governorApply/" /etc/tlp.conf
        fi
    fi
    echo "${powerProfile##*=}" > /var/lib/power-profiles-daemon/state_bat.conf
else
    if [[ -e /sys/devices/system/cpu/cpu0/cpufreq/energy_performance_preference ]]; then
        sed -Ei "s/#?CPU_ENERGY_PERF_POLICY_ON_AC=.*/CPU_ENERGY_PERF_POLICY_ON_AC=$System_CPU_ENERGY_PERF_POLICY/g" /etc/tlp.conf
        sed -Ei "s/#?CPU_SCALING_GOVERNOR_ON_AC=.*/#CPU_SCALING_GOVERNOR_ON_AC=$governorApply/" /etc/tlp.conf
    else
        if [[ $balanced = schedutil ]]; then
            sed -Ei "s/#?CPU_SCALING_GOVERNOR_ON_AC=.*/CPU_SCALING_GOVERNOR_ON_AC=$governorApply/" /etc/tlp.conf
        else
            sed -Ei "s/#?CPU_SCALING_GOVERNOR_ON_AC=.*/#CPU_SCALING_GOVERNOR_ON_AC=$governorApply/" /etc/tlp.conf
        fi

        
    fi
    echo "${powerProfile##*=}" > /var/lib/power-profiles-daemon/state_ac.conf
fi




