#!/bin/sh

dhcp4_discover () {
	ifup lan_dhcp
	ifup lan_dhcpv6
}

dhcp4_kill () {
	ifdown lan_dhcp
	ifdown lan_dhcpv6
}

dnsmasq_start () {
	rm /tmp/dnsmasq.d/disable-dhcp-server-in-br-lan
	/etc/init.d/dnsmasq restart
}

dnsmasq_stop () {
	echo '### generated by batman-adv-auto-gw-mode ###' > /tmp/dnsmasq.d/disable-dhcp-server-in-br-lan
	echo no-dhcp-interface=br-lan >> /tmp/dnsmasq.d/disable-dhcp-server-in-br-lan
	/etc/init.d/dnsmasq restart
}

if [ "$BATTYPE" = "gw" ] ; then
	case "$BATACTION" in
		add) dnsmasq_stop ; dhcp4_discover ;;
		del) dhcp4_kill ; dnsmasq_start ;;
		change) dhcp4_kill ; sleep 5 ; dhcp4_discover ;;
	esac
fi
