#!/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" == "" ]; then
	echo "Missing or empty environment variable(s)." >&2
	echo "This script should be started by hald." >&2
	exit 1
fi

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

if [ "$HAL_PROP_LAPTOP_PANEL_ACPI_METHOD" == "toshiba" ]; then
	# cat /proc/acpi/toshiba/lcd
	#  brightness:              5
	#  brightness_levels:       8
	value="`cat $HAL_PROP_LINUX_ACPI_PATH | grep brightness: | awk '{print $2;}'`"
elif [ "$HAL_PROP_LAPTOP_PANEL_ACPI_METHOD" == "asus" ]; then
	# cat /proc/acpi/asus/brn
	#  5
	value="`cat $HAL_PROP_LINUX_ACPI_PATH`"
elif [ "$HAL_PROP_LAPTOP_PANEL_ACPI_METHOD" == "panasonic" ]; then
	# cat /proc/acpi/pcc/brightness
	#  5
	value="`cat $HAL_PROP_LINUX_ACPI_PATH`"
elif [ "$HAL_PROP_LAPTOP_PANEL_ACPI_METHOD" == "ibm" ]; then
	# cat /proc/acpi/ibm/brightness
	#  level:          5
	#  commands:       up, down
	#  commands:       level <level> (<level> is 0-7)
	value="`cat $HAL_PROP_LINUX_ACPI_PATH | grep level: | awk '{print $2;}'`"
elif [ "$HAL_PROP_LAPTOP_PANEL_ACPI_METHOD" == "sony" ]; then
	# cat /proc/acpi/sony/brightness
	#  5
	value="`cat $HAL_PROP_LINUX_ACPI_PATH`"
	let "value = ${value} - 1"
else
	echo "org.freedesktop.Hal.Device.LaptopPanel.NotSupported" >&2
	echo "No ACPI method found" >&2
	exit 1
	fi

exit ${value}
