#!/bin/bash

export lcd_backlight=/sys/class/backlight/backlight-0/brightness

if test -e /sys/class/leds/lcd-backlight/brightness; then
    lcd_backlight=/sys/class/leds/lcd-backlight/brightness
fi

if test -e /sys/devices/platform/pwm-backlight.0/backlight/lcd-bl/brightness; then
    lcd_backlight=/sys/devices/platform/pwm-backlight.0/backlight/lcd-bl/brightness
fi

function max-lcd-backlight() {
    # on some systems 100 is the max backlight, but on some others,
    # 255 is the max:-(
    
    echo 100 > $lcd_backlight
    echo 255 > $lcd_backlight >/dev/null 2>&1 
}

export -f max-lcd-backlight

################################################################

export key_backlight=/sys/class/leds/button-backlight/brightness

################################################################

# work around the touch test event node:

function test-touch() {
    node=$(bash list-input-devs|grep -e -ts|grep -v vnc-ts|awk '{print $1}'|sed 's!/sys/class/input/input!/dev/input/event!')
    if test "$node"; then
	export TOUCH_DEVICE=$node
    fi
    command test-touch "$@"
}

export -f test-touch

################################################################

# work around the key test event node:

function test-keyboard() {
    node=$(bash list-input-devs|grep -e -key|grep -v vnc-ts|awk '{print $1}'|sed 's!/sys/class/input/input!/dev/input/event!')
    if test "$node"; then
	export KBD_DEVICE=$node
    fi
    command test-keyboard "$@"
}

export -f test-keyboard

################################################################

function warn() {
    echo "$@"
}
export -f warn

################################################################
function loop() {
    touch /data/loop-$(basename $0)

    while test -e /data/loop-$(basename $0); do
	"$@"
    done >/dev/null 2>&1 &
}

function stop-loop() {
    rm /data/loop-${1:-$(basename $0)} || true
}

export -f loop stop-loop
