#! /bin/sh
### BEGIN INIT INFO
# Provides:          vlan-config
# Required-Start:    kmod $local_fs
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: vlan-config script
# Description:       this script configures the Marvell 88E6141 managed switch and applies any vlan config
### END INIT INFO

. /lib/lsb/init-functions

# Globals
NAME=vlan-config
DESC="VLAN Configurator for the Parking Controller MV88E6141 Switch"

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
VLAN_CONFIG=/etc/vlan-config/vlan-config.conf

# Include defaults if available
if [ -f /etc/default/vlan-config ]
then
	. /etc/default/vlan-config
fi

set -e

# Functions
start() {
	if [ ! -f "${VLAN_CONFIG}" ]; then
		echo "Config file ${VLAN_CONFIG} not found!"
		exit 1
	fi
	
	# Reset the MV88E6141 switch:
	echo 8 > /sys/class/gpio/export || true
	echo out > /sys/class/gpio/gpio8/direction
	echo 0 > /sys/class/gpio/gpio8/value
	sleep 0.1
	echo 1 > /sys/class/gpio/gpio8/value
	echo 8 > /sys/class/gpio/unexport || true
	sleep 2

	# Configure and enable the MV88E6141 switch:
	/usr/sbin/vlan_config "${VLAN_CONFIG}"
}

case "$1" in
  start)
	start
	;;

  stop)
	;;

  restart|force-reload)
	start
	;;

  *)
	echo "Usage: $0 {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
