#!/bin/sh

# adjust brightness of the OLPC XO backlight

usage()
{
        echo "usage: ${0##*/} [up|down|max|min|<0-15>|color|mono]"
        exit 1;
}

LEVEL=/sys/class/backlight/dcon-bl/brightness
if [ -e /sys/devices/platform/dcon/monochrome ]
then
    MONOMODE=/sys/devices/platform/dcon/monochrome
else
    MONOMODE=/sys/devices/platform/dcon/output
fi


read curbright < $LEVEL

case $1 in
    "") # no argument, just report current value
            echo $curbright
            exit
            ;;
    color)
            echo 0 >$MONOMODE
            exit
            ;;
    mono)
            echo 1 >$MONOMODE
            exit
            ;;
    max|high)
            newbright=15
            ;;
    min|low)
            newbright=0
            ;;
    up)
            newbright=$(($curbright + 1))
            newbright=$(( ( $newbright > 15 ) ? 15 : $newbright ))
            ;;
    down)
            newbright=$(($curbright - 1))
            newbright=$(( ( $newbright < 0 ) ? 0 : $newbright ))
            ;;

    [0-9]|1[0-5])
            newbright=$1
            ;;
    *)
            usage
            ;;
esac

echo $newbright >$LEVEL

