#!/bin/bash

echo $1 > /tmp/sony

BRIGHTNESS=$(cat /sys/class/backlight/sony/brightness)

if [ "$BRIGHTNESS" -gt 7 ]; then
	BRIGHTNESS=0
fi

if [ "x$1" = "xdown" ]; then
   if [ "x$BRIGHTNESS" != "x0" ]; then
      BRIGHTNESS=$(( $BRIGHTNESS - 1 ))
      echo $BRIGHTNESS > /sys/class/backlight/sony/brightness
   else
      [ -x /usr/bin/spicctrl ] && /usr/bin/spicctrl -b 0   
   fi
   # Recent nvidia Sonys have ACPI methods that do nothing. Thanks, Sony.
   # [ -x /usr/bin/smartdimmer ] && smartdimmer -d 2>/dev/null
   if [ -x /usr/bin/smartdimmer ] 
   then
       BRIGHTNESS=`smartdimmer -g | awk '{print $3}'`
       NEWBRIGHT=`expr $BRIGHTNESS - 5`
       smartdimmer -s $NEWBRIGHT 2>/dev/null
   fi
elif [ "x$1" = "xup" ]; then
   if [ "x$BRIGHTNESS" != "x7" ]; then
      BRIGHTNESS=$(( $BRIGHTNESS + 1 ))
      echo $BRIGHTNESS > /sys/class/backlight/sony/brightness
   fi
   # [ -x /usr/bin/smartdimmer ] && smartdimmer -i 2>/dev/null
   if [ -x /usr/bin/smartdimmer ] 
   then
       BRIGHTNESS=`/usr/bin/smartdimmer -g | awk '{print $3}'`
       NEWBRIGHT=`expr $BRIGHTNESS + 5`
       smartdimmer -s $NEWBRIGHT 2>/dev/null
   fi
else
   echo >&2 Unknown argument $1
fi

