#!/bin/sh

# adjust brightness of the OLPC XO backlight.
# note that brightness is also adjusted automatically by powerd,
# for idle screen dimming and for ambient-light backlight
# control.

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

LEVEL=/sys/class/backlight/dcon-bl/brightness  # assume DCON
if [ ! -e $LEVEL -a -d /sys/class/backlight ]
then # but then choose the first backlight we find
    BACKLIGHT=$(ls /sys/class/backlight | sed 1q )
    if [ "$BACKLIGHT" ]
    then
        # assume levels of 0-15 for now -- use max_brightness later
        LEVEL=/sys/class/backlight/$BACKLIGHT/brightness
    fi
fi
if [ -e /sys/devices/platform/dcon/monochrome ]
then
    MONO_MODE=/sys/devices/platform/dcon/monochrome
elif [ -e /sys/devices/platform/dcon/output ]
then
    MONO_MODE=/sys/devices/platform/dcon/output
fi

set_mono()
{
    test "$MONO_MODE" && echo 1 > $MONO_MODE
}

set_color()
{
    test "$MONO_MODE" && echo 0 > $MONO_MODE
}


MONO_LOCKDIR=/tmp/olpc-brightness

set_mono_mode()
{
    # create a directory to hold our flag, so that anyone
    # can create or delete it.
    mkdir -p $MONO_LOCKDIR
    chmod a+w $MONO_LOCKDIR
    touch $MONO_LOCKDIR/mono_lock
}

clear_mono_mode()
{
    rm -f $MONO_LOCKDIR/mono_lock
}

is_mono_mode()
{
    test -e $MONO_LOCKDIR/mono_lock
}

set_bright()
{
    echo $1 > $LEVEL
}

read curbright < $LEVEL

case $1 in

    # no argument, just report current value
    "")
            echo $curbright
            ;;

    # set a numeric level.  not used much
    [0-9]|1[0-5])
            set_bright $1
            ;;

    # the next three options set, clear, and test "monochrome
    # mode" in this mode, the screen is kept in monochrome mode
    # all the time.  otherwise, it's set to color when the
    # backlight is on, and to monochrome when it's off (i.e., at
    # brightness 0)
    color)
            clear_mono_mode
            set_color
            ;;
    mono)
            set_mono
            set_mono_mode
            ;;

    is_mono)
            is_mono_mode || exit 0
            exit 1
            ;;

    # used by powerd, to avoid knowing details of "monochrome mode"
    maybe_color)
            is_mono_mode || set_color
            ;;

    # these two are typically invoked with alt-brightness up/down
    max|high)
            is_mono_mode || set_color
            set_bright 15
            ;;
    min|low)
            set_bright 0
            set_mono
            ;;

    # the next two are the standard brighten/dim operations. 
    # dimming to 0 changes from color to monochrome, and
    # brightening always switches to color, unless we're in
    # "monochrome mode".
    up)
            newbright=$(( curbright + 1 ))
            if [ $newbright -gt 15 ]
            then
                newbright=15
            fi
            is_mono_mode || set_color
            set_bright $newbright
            ;;
    down)
            newbright=$(( curbright - 1 ))
            if [ $newbright -le 0 ]
            then
                set_mono
                set_bright 0
            else
                set_bright $newbright
            fi
            ;;

    *)
            usage
            ;;
esac

