#!/bin/sh

NAME="init_hw"
DESC="Initialize HW"
STORE_WLAN_ADDR="/opt/gira/bin/store_wlan_addr"

LEDS_DIR="/sys/devices/soc0/leds.22/leds"
BACKLIGHT_DIR="/sys/class/backlight/backlight.23"

set_leds() {
    VALUE=$1
	find ${LEDS_DIR} -name brightness | while read -r led
    do
		write_value "$led" $VALUE
	done
}

write_value() {
DEST="$1"
VALUE=$2
cat > "$DEST" << EOT
$VALUE
EOT
}

read_value() {
	VALUE=`cat "$1"`
}

set_leds_on() {
	set_leds 255
}

set_leds_off() {
	set_leds 0
}

set_brightness_max() {
	read_value ${BACKLIGHT_DIR}/max_brightness
	write_value ${BACKLIGHT_DIR}/brightness $VALUE
}

set_brightness_min() {
	write_value ${BACKLIGHT_DIR}/brightness 0
}

start() {
  echo "start"
}

stop() {
  echo "stop"
}

case "$1" in
  start)
    echo -n "Starting $DESC: "
	start
	start-stop-daemon --start -b -x "$STORE_WLAN_ADDR" 
    echo "$NAME."
    ;;
  stop)
    echo -n "Stopping $DESC: "
	stop
    echo "$NAME."
    ;;
  restart|force-reload)
    echo -n "Restarting $DESC: "
	stop
	start
    echo "$NAME."
    ;;
  *)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start|stop|restart}" >&2
    exit 1
    ;;
esac

exit 0
