#!/bin/sh

# adjust brightness of the OLPC XO backlight

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

LEVEL=/sys/class/backlight/dcon-bl/brightness
MONOMODE=/sys/devices/platform/dcon/output

read curbright < $LEVEL

case $1 in
    "")	# no argument, just report current value
	    echo $curbright
	    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

monochrome=$(( newbright == 0 ))

# POWEREVENTS=/var/run/powerevents
# echo "brightness $newbright $monochrome" > $POWEREVENTS

echo $newbright >$LEVEL
echo $monochrome >$MONOMODE
