#!/bin/sh

# cpufreq script
# Copyright 2017 David Woodfall <dave@studioware.org>
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ''AS IS'' AND ANY EXPRESS OR IMPLIED
# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

set -e

CPUS=$(($(grep "cpu cores" /proc/cpuinfo | wc -l)-1))

ondemand()
{
    for c in `seq 0 $CPUS`
    do
        echo ondemand > /sys/devices/system/cpu/cpu$c/cpufreq/scaling_governor
    done
}

performance()
{
    for c in `seq 0 $CPUS`
    do
        echo performance > /sys/devices/system/cpu/cpu$c/cpufreq/scaling_governor
    done
}

powersave()
{
    for c in `seq 0 $CPUS`
    do
        echo powersave > /sys/devices/system/cpu/cpu$c/cpufreq/scaling_governor
    done
}

userspace()
{
    for c in `seq 0 $CPUS`
    do
        echo userspace > /sys/devices/system/cpu/cpu$c/cpufreq/scaling_governor
    done
}

status()
{
    echo cpufreq -h for usage and help.
    echo

    for c in `seq 0 $CPUS`
    do
        echo "CPU scaling governor $c: $(cat /sys/devices/system/cpu/cpu$c/cpufreq/scaling_governor)"
    done

    echo
    echo "Available governors are:"

    cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors
    echo
}

usage()
{
   cat<<EOF
cpufreq by dive
Poll or change CPU governor

Usage:
cpufreq <option>

With no option, give status and exit.

Options are:

-h				Give usage and help.
on				Set CPU to ondemand.
perf				Set CPU to performace.
power				Set CPU to powersave.
user				Set CPU to userspace.
status				Give current governors and exit.
EOF

    echo
    echo "Available governors are:"

    echo
    cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors
    echo
}

case $1 in
    '')
        status
        ;;
    '-h')
        usage
        ;;
    'power')
        powersave
        ;;
    'on')
        ondemand
        ;;
    'perf')
        performance
        ;;
    'off')
        powersave
        ;;
    'user')
        userspace
        ;;
    'status')
        status
        exit
        ;;
    '')
        status
        exit
        ;;
esac

echo "CPU scaling governor is now $(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor)"
