#!/usr/bin/execlineb -P
# dhcrelay in foreground (-d). SERVERS/OPTIONS/INTERFACES from
# /etc/default/isc-dhcp-relay. For multiple -i flags, set IFACE_ARGS in
# s6-rc config (e.g. IFACE_ARGS="-i eth0 -i eth1"); otherwise a single
# INTERFACES value is passed as -i ${INTERFACES}.

ifelse { test -f /etc/default/isc-dhcp-relay } {
	envfile /etc/default/isc-dhcp-relay
	importas -sCuD "" SERVERS SERVERS
	importas -sCuD "" OPTIONS OPTIONS
	importas -sCuD "" INTERFACES INTERFACES
	envfile -I /etc/s6-rc/config/isc-dhcp-relay.conf
	importas -sCuD "" IFACE_ARGS IFACE_ARGS

	ifelse { test -z "${SERVERS}" } {
		s6-pause
	} {
		ifelse { test -n "${IFACE_ARGS}" } {
			fdmove -c 2 1
			exec /usr/sbin/dhcrelay -d -q ${OPTIONS} ${IFACE_ARGS} ${SERVERS}
		} {
			ifelse { test -n "${INTERFACES}" } {
				fdmove -c 2 1
				exec /usr/sbin/dhcrelay -d -q ${OPTIONS} -i ${INTERFACES} ${SERVERS}
			} {
				fdmove -c 2 1
				exec /usr/sbin/dhcrelay -d -q ${OPTIONS} ${SERVERS}
			}
		}
	}
}
s6-pause
