#! /bin/sh
### BEGIN INIT INFO
# Provides:          config_unique
# Required-Start:    
# Should-Start:      
# Required-Stop:     
# Default-Start:     S
# Default-Stop:
# Short-Description: Configures GPIO for Lab 07.
# Description:       Configures GPIO for Lab 07.
### END INIT INFO

boardId=`cat /sys/devices/virtual/dmi/id/board_name`

case "$1" in
  start|restart|force-reload)
    case $boardId in
      "Galileo")
  if [ ! -d /sys/class/gpio/gpio37 ] ; then
	    echo -n "37" > /sys/class/gpio/export
	fi
	echo -n "out" > /sys/class/gpio/gpio37/direction
	echo -n "0" > /sys/class/gpio/gpio37/value
	
  # gpio44 = in = neither pull-up nor pull-down (apparently)
	if [ ! -d /sys/class/gpio/gpio44 ] ; then
      echo -n "44" > /sys/class/gpio/export
  fi
  echo -n "in" > /sys/class/gpio/gpio44/direction

  chgrp adc /sys/bus/iio/devices/iio:device0/in_voltage0_raw
  chgrp adc /sys/bus/iio/devices/iio:device0/in_voltage0_scale
  chmod g+r /sys/bus/iio/devices/iio:device0/in_voltage0_raw
  chmod g+r /sys/bus/iio/devices/iio:device0/in_voltage0_scale
    ;;
    esac
    ;;
  stop)
    case $boardId in
      "Galileo")
	echo -n "37" > /sys/class/gpio/unexport
  echo -n "44" > /sys/class/gpio/unexport
	;;
    esac
    ;;
  status)
    ;;
  *)
    echo -n "Usage: $0 " 
    echo "{start|stop|restart|force-reload|status}"
    exit 1
    ;;
esac

exit 0
