#!/usr/bin/sh

# This is a minimal version of the actual kbd_backlight script, with
# no support for relative brightnesses and always-zero exit code

SYS_KBD_BACKLIGHT="/sys/class/leds/kbd_backlight/brightness"

is_integer() {
    case "$1" in
    *[!0123456789]*) return 1 ;;
    '') return 1 ;;
    *) return 0 ;;
    esac
}

if [ -f "$SYS_KBD_BACKLIGHT" ]; then
    KBD_BACKLIGHT_BRIGHTNESS="$1"
    if is_integer "$KBD_BACKLIGHT_BRIGHTNESS" &&
        [ "$KBD_BACKLIGHT_BRIGHTNESS" -ge 0 ] && [ "$KBD_BACKLIGHT_BRIGHTNESS" -le 255 ]; then
        echo "$KBD_BACKLIGHT_BRIGHTNESS" >"$SYS_KBD_BACKLIGHT" 2>/dev/null
    fi
fi
