#!/bin/sh
#
# slcand	Userspace daemon for serial line (SLCAN) CAN interfaces
#
# chkconfig:	2345 26 74
# description:	slcand attaches a serial line CAN adapter to the \
#		kernel's slcan line discipline and brings up a CAN \
#		network interface for it.
#
# config:	/etc/sysconfig/slcand

# Source function library
. /etc/rc.d/init.d/functions

# Get service config
[ -f /etc/sysconfig/slcand ] && . /etc/sysconfig/slcand

# no tty configured. exit silently
if [ -z "$SLCAND_TTY" ]; then
	case "$1" in
		start|stop|restart|reload|force-reload)
		exit 0
		;;
	esac
fi

start() {
	if [ ! -f /var/lock/subsys/slcand ]; then
		msg_starting "slcand ($SLCAND_TTY -> $SLCAND_IF)"
		daemon /usr/bin/slcand ${SLCAND_OPT} ${SLCAND_TTY} ${SLCAND_IF}
		RETVAL=$?
		if [ $RETVAL -eq 0 ]; then
			sleep 1
			ip link set ${SLCAND_IF} up
			touch /var/lock/subsys/slcand
		fi
	else
		msg_already_running "slcand"
	fi
}

stop() {
	if [ -f /var/lock/subsys/slcand ]; then
		msg_stopping "slcand"
		ip link set ${SLCAND_IF} down 2>/dev/null
		killproc slcand
		rm -f /var/lock/subsys/slcand
	else
		msg_not_running "slcand"
	fi
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
	status slcand
	RETVAL=$?
	;;
  restart|force-reload)
	stop
	start
	;;
  *)
	msg_usage "$0 {start|stop|restart|force-reload|status}"
	exit 3
esac

exit $RETVAL
