#!/bin/sh

### BEGIN INIT INFO
# Provides:          Antzer
# Required-Start:
# Required-Stop:
# Default-Start:
# Default-Stop:
# Short-Description: 
### END INIT INFO

mount_faro () {
	#sleep 2 ,if needed
	echo "start Antzer Faro!!"
	modprobe can
	modprobe can_raw
	insmod /opt/antzer/faro_can.ko # modprobe faro_can
	/opt/antzer/farocand -s33 -S921600 /dev/ttyUSB0 faro_can0 faro_can1
	ip link set faro_can0 up
	ip link set faro_can1 up
}

unmount_faro () {
  echo "stop Antzer Faro!!"
  ip link set faro_can0 down
  ip link set faro_can1 down
  ifconfig faro_can0 down
  ifconfig faro_can1 down
  killall farocand
  sleep 5
  rmmod faro_can
  rmmod can_raw
  rmmod can
}

reload_faro () {
  echo "restart Antzer Faro!!"
  ip link set faro_can0 down
  ip link set faro_can1 down
  /opt/antzer/farocand -s33 -S921600 /dev/ttyUSB0 faro_can0 faro_can1
  ip link set faro_can0 up
  ip link set faro_can1 up
}

case "$1" in
  "")
	echo "Warning: mountdevsubfs should be called with the 'start' argument." >&2
	mount_faro
	;;
  start)
	mount_faro
	;;
  restart|reload|force-reload)
	reload_faro
	;;
  stop)
	unmount_faro
	# No-op
	;;
  *)
	echo "Usage: [start|stop]"
	exit 3
	;;
esac
