#!/bin/sh

run_dir=/var/run
sled_f=$run_dir/statusled/mode
conf_dir=/etc/sysconfig
sled_cf=$conf_dir/statusled.conf
nled_cf=$conf_dir/networkled.conf
pled_cf=$conf_dir/powerled.conf

case $1 in
	# Single argument targets to replace double argument targets eventually.
	# This moves all product specific behaviour to set_led as should be
	# according to TODO Linux Application trouble report #1.
	flash_loading)
		powerled flash &
		;;

	factory_default)
		statusled yellow
		;;

	axisns_req)
		statusled usage
		if [ -d $run_dir ]; then
			echo "STATUSLED=usage" > $sled_f
		fi
		;;

	axisns_failed)
		statusled flash
		if [ -d $run_dir ]; then
			echo "STATUSLED=flash" > $sled_f
		fi
		;;

	statusled)
		# If the status LED is in "usage"/"flash" state and we're
		# switching state, kill the background process that is
		# controlling the LED.
		if [ -r $sled_f ]; then
			. $sled_f
			if [ "$STATUSLED" = usage -a $2 != usage -o \
			     "$STATUSLED" = assistant_red -a $2 != assistant_red -o \
			     "$STATUSLED" = assistant_yellow -a $2 != assistant_yellow -o \
			     "$STATUSLED" = assistant_green -a $2 != assistant_green -o \
			     "$STATUSLED" = flash ]; then
				killall statusled
			fi
		fi

		state=$2
		case $state in
			normal|booted)
				if [ -r $sled_cf ]; then
					# Check if "normal" is on or off.
					. $sled_cf
					if [ "$USAGE" = on ]; then
						statusled green
					else
						statusled off
					fi
				else
					statusled green
				fi
				state=normal
				;;
			critical_event)
				statusled red
				;;
			non_critical_event)
				statusled yellow
				;;
			boot)
				if [ -r $sled_cf ]; then
					# Check if "normal" is on or off.
					. $sled_cf
					if [ "$USAGE" = on ]; then
						statusled yellow
					else
						statusled off
					fi
				else
					statusled yellow
				fi
				;;
			flash_write)
				statusled yellow
				;;
			factory_default)
				statusled yellow
				;;
			ser_no_error)
				statusled flash 10
				;;
			usage)
				if [ -r $sled_cf ]; then
					. $sled_cf
					. $sled_f
					# Error states have higher priority
					# than the "usage" state, so only
					# change state if we're in "normal" or
					# "off".
					if [ "$USAGE" = flash -a \( "$STATUSLED" = normal -o "$STATUSLED" = off \) ]; then
						statusled usage $INTERVAL
					else
						state=$STATUSLED
					fi
				else
					state=$STATUSLED
				fi
				;;
			assistant_red)
				if [ "$STATUSLED" != assistant_red ]; then
					statusled assistant red
				fi
				;;
			assistant_yellow)
				if [ "$STATUSLED" != assistant_yellow ]; then
					statusled assistant yellow
				fi
				;;
			assistant_green)
				if [ "$STATUSLED" != assistant_green ]; then
					statusled assistant green
				fi
				;;
			off)
				statusled off
				;;
			*)
				echo "The $2 state is not supported by $1"
				exit 1
				;;
		esac
		if [ -d $run_dir ]; then
			echo "STATUSLED=$state" > $sled_f
		fi
		;;
	powerled)
		case $2 in
			flash_write)
				powerled flash &
				;;
			off)
				powerled off
				;;
			*)
				echo "The $2 state is not supported by $1"
				exit 1
				;;
		esac
		;;
	powerledmode)
		if [ -r $pled_cf ]; then
			# Check if "on" or "off".
			. $pled_cf
			if [ "$USAGE" = on ]; then
				powerledmode on
			else
				powerledmode off
			fi
		fi
		;;
	networkled)
		case $2 in
			10Mbit)
				networkled yellow
				;;
			flash_write)
				networkled flash &
				;;
			off)
				networkled off
				;;
			*)
				echo "The $2 state is not supported by $1"
				exit 1
				;;
		esac
		;;
	networkledmode)
		if [ -r $nled_cf ]; then
			# Check if "on" or "off".
			. $nled_cf
			if [ "$USAGE" = on ]; then
				networkledmode --enable
			else
				networkledmode --disable
				networkled off
			fi
		fi
		;;
	link_up)
		device=$2
		# Add product specific behaviour here
		;;
	link_down)
		device=$2
		# Add product specific behaviour here
		;;
	*)
		echo "$1 is a non valid led"
		exit 1
		;;
esac
