#!/bin/sh /etc/rc.common
# Copyright (C) 2012 OpenWrt.org

START=91

SERVICE_SIG="KILL"
SERVICE_PID_FILE="/var/run/dhcrelay4.pid"
SERVICE_USE_PID=1

start() {
	. /lib/functions/network.sh
	config_load dhcrelay
	local args=""

	local enabled
	config_get_bool enabled ipv4 enabled 0
	[ "$enabled" -eq 0 ] && return 0

	# listen interfaces
	local interfaces
	local ifname
	config_get interfaces ipv4 interfaces
	for net in $interfaces; do
		if network_get_device ifname "$net"; then
			append args "-i $ifname"
		fi
	done

	# link selection sub-option (RFC3527)
	local link_selection
	config_get link_selection ipv4 link_selection
	if network_get_device ifname "$link_selection"; then
		append args "-U $ifname"
	fi

	# relay mode
	local relay_mode
	config_get relay_mode ipv4 relay_mode
	[ -n "$relay_mode" ] && append args "-m $relay_mode"

	# dhcp server address
	local server
	config_get server ipv4 dhcpserver
	[ -n "$server" ] || return 0
	append args "$server"

	service_start /usr/sbin/dhcrelay -4 -q \
		-pf $SERVICE_PID_FILE $args
}

stop() {
	service_stop /usr/sbin/dhcrelay
}
