#!/bin/sh

if [ "$ACTION" == "NOTIFY_MASTER" ]; then
    # Enable force option for all dnsmasq sections
    dnsmasq_sections=$(uci show dhcp | grep "=dnsmasq" | cut -d. -f2 | cut -d= -f1)
    for section in $dnsmasq_sections; do
        uci set dhcp.$section.force='1'
    done
    uci commit dhcp
elif [ "$ACTION" == "NOTIFY_BACKUP" ]; then
    # Disable force option for all dnsmasq sections
    dnsmasq_sections=$(uci show dhcp | grep "=dnsmasq" | cut -d. -f2 | cut -d= -f1)
    for section in $dnsmasq_sections; do
        uci set dhcp.$section.force='0'
    done
    uci commit dhcp
elif [ "$ACTION" != "startup" ]; then
    # Sometimes dnsmasq starts when it should not: stop it at every sync and during shutdown
    /etc/init.d/dnsmasq stop 2&>/dev/null
fi
