#!/bin/sh
#
# Author: Carsten Grohmann
#
# Licence: GPL version 2 or above
#
# $Id: power.sh,v 1.1 2005/04/02 13:41:03 carsten Exp $
# $Source: /home/cvs/project_carsten/repository/scripts/power.sh,v $
#
# Changes:
# $Log: power.sh,v $
# Revision 1.1  2005/04/02 13:41:03  carsten
# - initial import into CVS
# - version 0.1
# - script to set the cpu into a power save mode or into a performance mode
#

# enable for debugging purposes only
#set -x
#set -v

# Version number
VERSION='0.1 ($Revision: 1.1 $)'

# CPU number, count starts with 0
CPU_NUMBER=0

# directory with all needed status files
CPUFREQ_DIR=/sys/devices/system/cpu/cpu${CPU_NUMBER}/cpufreq

# Battery number, count starts with 0
BAT_NUMBER=0

# directory with the battery status files
BAT_DIR=/proc/acpi/battery/BAT${BAT_NUMBER}

# check cpufreq support
if [ ! -d $CPUFREQ_DIR ]; then
  echo "ERROR: cpufreq directory not found. Please load cpufreq module or"
  echo "       add enable cpufreq as static part of your kernel."
  exit 1
fi

# get minimum and maximum frequencies
CPU_MIN_FREQ=$(cat $CPUFREQ_DIR/scaling_available_frequencies | awk '{print $1}')
CPU_MAX_FREQ=$(cat $CPUFREQ_DIR/scaling_available_frequencies | awk '{print $NF}')

# governors to controll the power demeanour
CPU_MIN_GOV="powersave"
CPU_MAX_GOV="performance"

# check governors
cat $CPUFREQ_DIR/scaling_available_governors | grep --silent $CPU_MIN_GOV
if [ $? -ne 0 ]; then
  echo "ERROR: Governor $CPU_MIN_GOV not available."
  exit 1
fi
cat $CPUFREQ_DIR/scaling_available_governors | grep --silent $CPU_MAX_GOV
if [ $? -ne 0 ]; then
  echo "ERROR: Governor $CPU_MAX_GOV not available."
  exit 1
fi

# check battery support
if [ ! -d $BAT_DIR ]; then
  echo "ERROR: Directory with the battery status files not found."
  exit 1
fi

# analyze command line options
case $1 in
  --up|-u)
    action="up"
    ;;
  --down|-d)
    action="down"
    ;;
  --status|-s)
    action="status"
    ;;
  --battery|-b)
    action="battery"
    ;;
  --version|-s)
    action="version"
    ;;
  --help|-h|-?)
    action="usage"
    ;;
  *)
    action="usage"
    ;;
esac

# print banner
echo "power.sh Version $VERSION"

# main action
case $action in
  up)
    echo "Enable performance mode..."
    # set frequency and governor
    echo $CPU_MAX_FREQ > $CPUFREQ_DIR/scaling_max_freq
    echo $CPU_MAX_GOV > $CPUFREQ_DIR/scaling_governor
    ;;
  down)
    echo "Enable powersave mode..."
    # set frequency and governor
    echo $CPU_MIN_FREQ > $CPUFREQ_DIR/scaling_max_freq
    echo $CPU_MIN_GOV > $CPUFREQ_DIR/scaling_governor
    ;;
  status)
    echo "Current settings"
    echo "================"
    echo "Running governor:  $(cat $CPUFREQ_DIR/scaling_governor)"
    echo "Current frequency: $(cat $CPUFREQ_DIR/cpuinfo_cur_freq)"
    echo "Minimum frequency: $(cat $CPUFREQ_DIR/scaling_min_freq)"
    echo "Maximum frequency: $(cat $CPUFREQ_DIR/scaling_max_freq)"
    echo " "
    echo "General settings"
    echo "================"
    echo "Minimum frequency: $CPU_MIN_FREQ"
    echo "Maximum frequency: $CPU_MAX_FREQ"
    echo "Availably governors:   $(cat $CPUFREQ_DIR/scaling_available_governors)"
    echo "Availably frequencies: $(cat $CPUFREQ_DIR/scaling_available_frequencies)"
    ;;
  battery)
    echo "Current battery state"
    echo "====================="
    cat $BAT_DIR/state
    echo " "
    echo "General battery infos"
    echo "====================="
    cat $BAT_DIR/info
    ;;
  version)
    echo "Copyright (c) Carsten Grohmann 2005; All rights reserved."
    echo "Licence: GPL version 2 or above"
    ;;
  usage)
    echo "Usage: power.sh <Command>"
    echo "  Show data about the cpu frequencies and the cpu governor or set"
    echo "  the cpu to a performance or a powersave mode."
    echo "  This version is designed to run on single processor systems like"
    echo "  laptops."
    echo " "
    echo "Commands"
    echo "========"
    echo "  --up|-u      set the cpu to maximum performance"
    echo "  --down|-d    set the cpu to the slowest frequency"
    echo "  --status|-s  shows the cpu frequence settings"
    echo "  --battery|-b shows the current state of the battery"
    echo "  --help|-h    shows this message"
    echo "  --version|-v shows the version number of this script"
    exit
    ;;
esac

