#! /usr/bin/busybox ash
#
# Written by Miquel van Smoorenburg <miquels@cistron.nl>.
# Modified for Debian GNU/Linux by Ian Murdock <imurdock@gnu.ai.mit.edu>
# and Axel Beckert <abe@deuxchevaux.org>.
# Entirely rewritten by Mjt in 2023,
# Copyright (c) 2023 Michael Tokarev <mjt@tls.msk.ru>
#
### BEGIN INIT INFO
# Provides:          udhcpd
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start busybox udhcpd at boot time
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=udhcpd
DAEMON=/usr/sbin/$NAME
DESC="Busybox based DHCP server"

test -x $DAEMON || exit 0

. /lib/lsb/init-functions

DHCPD_OPTS="-S"	# Additional options given to the server
# Include defaults if available
if [ -f /etc/default/udhcpd ] ; then
	. /etc/default/udhcpd
fi
DAEMON_OPTS="$DHCP_OPTS"

if [ "$DHCPD_ENABLED" = "no" ]; then
	echo $NAME: Disabled. Edit /etc/default/udhcpd to enable it.
	exit 0
fi

daemon() {
	case "$1" in
		(start)  args="-S -o";    act="Starting" ;;
		(stop)   args="-K -o -q"; act="Stopping" ;;
		(status) args="-K -t -q"; act="Checking" ;;
	esac
	log_daemon_msg "$act $DESC " "$NAME"
	busybox start-stop-daemon $args -p /var/run/$NAME.pid \
		-x $DAEMON -- $DAEMON_OPTS
	rc=$?
	log_end_msg $?
	return $?
}

case "$1" in
    (start|stop|status)
	daemon $1
	;;
    (restart|force-reload)
	daemon stop
	daemon start
	;;
    (*)
	echo "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload|status}" >&2
	exit 1
	;;
esac
