#! /bin/sh
#
### BEGIN INIT INFO
# Provides:          udplogger
# Required-Start:    $remote_fs $syslog $network
# Should-Start:
# Required-Stop:     $network
# Should-Stop:
# Default-Start:     2 3 5
# Default-Stop:      0 1 2 6
# X-Systemd-RemainAfterExit: true
# Short-Description: Start udplogger to log remote netconsole messages
# Description:       Start udplogger to log remote netconsole messages.
### END INIT INFO
  
SYSTEMD_NO_WRAP=1 . /etc/rc.status

# Reset status of this service
rc_reset

# Check for missing binaries (stale symlinks should not happen)
UDPLOGGER_BIN=/usr/bin/udplogger
test -x $UDPLOGGER_BIN || exit 5

logdir=/var/log/netconsole
if [ ! -d "$logdir" ]; then
  mkdir -p "$logdir"
  chmod 750 "$logdir"
fi

# IP and port of the local listening address for netconsole messages:
if [ -r /etc/udplogger.conf ]; then
  . /etc/udplogger.conf
fi
IPOPT=""
if [ "$NETCONSOLE_IP" ]; then
  IPOPT="ip=$NETCONSOLE_IP"
fi
PORTOPT=""
if [ "$NETCONSOLE_PORT" ]; then
  PORTOPT="port=$NETCONSOLE_PORT"
fi

case "$1" in
    start)
	echo -n "Start udplogger "
        startproc $UDPLOGGER_BIN $IPOPT $PORTOPT dir="$logdir" perm=640
	rc_status -v
	;;
    stop)
	echo -n "Stop udplogger "
	killproc -TERM $UDPLOGGER_BIN
	rc_status -v
	;;
    try-restart|condrestart)
	$0 status
	if test $? = 0; then
		$0 restart
	else
		rc_reset	# Not running is not a failure.
	fi
	rc_status
	;;
    restart)
	## Stop the service and regardless of whether it was
	## running or not, start it again.
	$0 stop
	$0 start
	rc_status
	;;
    force-reload)
	$0 restart
	rc_status -v
	;;
    reload)
	$0 restart
	rc_status -v
	;;
    status)
	echo -n "Checking for service udplogger "
        checkproc $UDPLOGGER_BIN
	rc_status -v
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload}"
	exit 1
	;;
esac
rc_exit
