#!/bin/bash
#
# Copyright (c) 2013 National Instruments
#

arch=`uname -m`

if [ "$arch" = "armv7l" ]; then
WD_MODE_PATH=/sys/bus/i2c/devices/0-0040/watchdog_mode
elif [ "$arch" = "x86_64" ]; then
WD_MODE_PATH=/sys/bus/acpi/devices/NIC775C\:00/watchdog_mode
fi

STATUS_LED_PATH=/sys/class/leds/nilrt:status:yellow/brightness

# Blink pattern bitfields, i.e. 1010101 = 4 blinks
pattern[0]=0
pattern[1]=$((2#0000001))
pattern[2]=$((2#0000101))
pattern[3]=$((2#0010101))
pattern[4]=$((2#1010101))
blink_max=4

case "$1" in
  init)
	# switch LED watchdog from boot mode to user mode, and turn off the LED
	if [ -e $WD_MODE_PATH ]; then
		echo -n user > $WD_MODE_PATH 2>/dev/null
	fi
	if [ -e $STATUS_LED_PATH ]; then
		echo 0 > $STATUS_LED_PATH 2>/dev/null
	fi
	;;
  blink_count)
	if [ -z "$2" ]; then
		echo "No blink count specified."
	elif [ $2 -gt $blink_max ]; then
		# let bash handle reporting the error for non-numeric params
		echo "$2 is not a supported blink count."
	elif [ -e $STATUS_LED_PATH ]; then
		echo ${pattern[$2]} > $STATUS_LED_PATH 2>/dev/null
	fi
	;;
  blink_pattern)
	if [ -z "$2" ]; then
		echo "No blink pattern specified."
	elif [ -e $STATUS_LED_PATH ]; then
		echo $2 > $STATUS_LED_PATH 2>/dev/null
	fi
	;;
  *)
	echo "Usage: $0 {init|blink_count N|blink_pattern P}"
	;;
esac

exit 0

