#!/bin/bash

## set path, filename, range max for script use
datapath=/usr/local/tmp/$USER
bldata="$datapath/nvbl.txt"
blpath=/sys/class/backlight/acpi_video0
blfn="$blpath/brightness"
test -f "$blfn" || {
    printf "error: sysfs backlight not found\n'%s'\n" "$blfn"
    exit 1
}

rmin=0
rmax=`<"$blpath/max_brightness"`
nsteps="20"
step=$((rmax/nsteps))
maxlim=$((rmax - 5))    ## value arrived at as max desireable value

declare -i newbl

function usage {

    local ecode=${2:-0}

    test -n "$1" && printf "\n %s\n" "$1" >&2

cat >&2 << helpMessage

 usage: ${0//*\//} int [${rmin}-${rmax}] (screen brightness)

  This script provides an alternate method for setting the laptop LCD backlight brightness
  on HP laptops as workaround for broken backlight control recent Xorg and kernels.
  It makes use of '$blfn' to set brightness level
  between 0-${rmax} where min=${rmin}, max=${rmax}. Additional options --increase/--decrease incrementally
  increase/decrease the brightness by 1; or --check to return the current backlight level.

 Options:

    -h  | --help      program help (this file)
    -c  | --check     returns the current backlight brightness   (default)
    -d  | --decrease  decrease current backlight brightness by 1
    -i  | --increase  increase current backlight brightness by 1
    +/-               increase/decrease current backlight brightness by 1
    -s  | --startup   set to saved value in '$bldata'

helpMessage

    exit $ecode;
}

# test -n "$1" || usage "error: insufficient input" 1
value=${1:--c}

## get current brightness value
bl=`<"$blfn"`

## process options based on first argument (all options are exclusive)
# case "$1" in
case "$value" in

    -h | --help     )
        usage
        ;;

    -c | --check    )
        printf "Current Brightness: %s\n(%d-%d)\n" "$bl" $rmin $rmax
        newbl="$bl"
        ;;

    - | -d | --decrease )
        bl=`<"$blfn"`
        (( bl >= step )) && newbl=$((bl - step)) || newbl="$rmin"
        test "$UID" -eq "0" && \
            echo "$newbl" > "$blfn" || \
            sudo sh -c "echo '"$newbl"' > '"$blfn"'"
        ;;

    + | -i | --increase )
        bl=`<"$blfn"`
        (( bl < maxlim )) && newbl=$((bl + step)) || newbl=$maxlim
        test "$UID" -eq "0" && \
            echo "$newbl" > "$blfn" || \
            sudo sh -c "echo '"$newbl"' > '"$blfn"'"
        ;;

    -s | --startup )
        if test -r "$bldata"
        then
            newbl=`<"$bldata"`
            if test "$newbl" -ge "$rmin" -a "$newbl" -le "$rmax"
            then
                test "$UID" -eq "0" && \
                    echo "$newbl" > "$blfn" || \
                    sudo sh -c "echo '"$newbl"' > '"$blfn"'"
            fi
        fi
        exit 0
        ;;

    [0-9] | 1[0-9] | 20 )   ## set a max of 14 -- others too bright
        [ "$value" -gt 15 ] && newbl=15 || newbl="$value"
        test "$UID" -eq "0" && \
            echo "$newbl" > "$blfn" || \
            sudo sh -c "echo '"$newbl"' > '"$blfn"'"
        ;;

    *  )
        usage "error invalid brightness argument" 1
        ;;

esac

test -d "$datapath" || mkdir -p "$datapath"
test -d "$datapath" || exit 1

echo "$newbl" > "$bldata"

exit 0
