#!/bin/bash

led_path=""
action=""
all_led=""

set_led_path () {
        case "$1" in
                RED)
                        led_path="/sys/class/leds/uc811x:DIA1/"
                        ;;
                YELLOW)
                        led_path="/sys/class/leds/uc811x:DIA2/"
                        ;;
                GREEN)
                        led_path="/sys/class/leds/uc811x:DIA3/"
                        ;;
                ALL)
                        all_led="TRUE"
                        ;;
                *)
                        exit -1
                        ;;
        esac
}

exec_led_action () {
        case "$1" in
                0)      # Turn off led
                        echo 0 > "${led_path}brightness"
                        ;;
                1)      # Turn on led
                        echo 1 > "${led_path}brightness"
                        ;;
                2)      # Switch led to blink mode
                        echo heartbeat > "${led_path}trigger"
                        ;;
                *)
                        exit -1
                        ;;
        esac
}

set_led_path $1
if [ X"$all_led" == X"TRUE" ]; then
        set_led_path "RED"
        exec_led_action $2
        set_led_path "YELLOW"
        exec_led_action $2
        set_led_path "GREEN"
        exec_led_action $2
else
        exec_led_action $2
fi

exit 0
