#!/bin/sh
#
# Copyright (C) 2005 Richard Hughes <richard@hughsie.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# Check for environment variables
if [ "$HAL_PROP_LAPTOP_PANEL_ACPI_METHOD" == "" ] || 
   [ "$HAL_PROP_LINUX_ACPI_PATH" == "" ] ||
   [ "$HAL_PROP_LAPTOP_PANEL_NUM_LEVELS" == "" ] ; then
        echo "Missing or empty environment variable(s)." >&2
        echo "This script should be started by hald." >&2
        exit 1
fi

# read value for set brightness
read value

# Check for file existance and that it's writable
if [ ! -w $HAL_PROP_LINUX_ACPI_PATH ]; then
	echo "org.freedesktop.Hal.Device.LaptopPanel.NotSupported" >&2
	echo "$1 not writable!" >&2
	exit 1
fi

# Check for values outside range
if [ ${value} -lt 0 ] || [ ${value} -gt $HAL_PROP_LAPTOP_PANEL_NUM_LEVELS ]; then
	echo "org.freedesktop.Hal.Device.LaptopPanel.Invalid" >&2
	echo "Brightness has to be between 0 and $HAL_PROP_LAPTOP_PANEL_NUM_LEVELS!" >&2
	exit 1
fi

if [ "$HAL_PROP_LAPTOP_PANEL_ACPI_METHOD" == "toshiba" ]; then
	# echo "brightness: {0..x}" >/proc/acpi/toshiba/lcd
	echo "brightness: $value" > $HAL_PROP_LINUX_ACPI_PATH
elif [ "$HAL_PROP_LAPTOP_PANEL_ACPI_METHOD" == "asus" ]; then
	# echo {0..15} > /proc/acpi/asus/brn
	# http://www.taupro.com/wiki/ChemBook/LCDdisplayPowerConfiguration
	echo "$value" > $HAL_PROP_LINUX_ACPI_PATH
elif [ "$HAL_PROP_LAPTOP_PANEL_ACPI_METHOD" == "panasonic" ]; then
	# echo {0..15} > /proc/acpi/pcc/brightness
	# http://readlist.com/lists/vger.kernel.org/linux-kernel/7/36405.html
	echo "$value" > $HAL_PROP_LINUX_ACPI_PATH
elif [ "$HAL_PROP_LAPTOP_PANEL_ACPI_METHOD" == "ibm" ]; then
	# echo "level {0..7}" > /proc/acpi/ibm/brightness
	# http://ibm-acpi.sourceforge.net/README
	echo "level $value" > $HAL_PROP_LINUX_ACPI_PATH
elif [ "$HAL_PROP_LAPTOP_PANEL_ACPI_METHOD" == "sony" ]; then
	# echo "{1..8}" > /proc/acpi/sony/brightness
	# http://popies.net/sonypi/2.6-sony_acpi4.patch
	echo "$value" > $HAL_PROP_LINUX_ACPI_PATH
else
	echo "org.freedesktop.Hal.Device.LaptopPanel.NotSupported" >&2
	echo "No ACPI method found" >&2
	exit 1
	fi

exit 0
