libexec=/usr/share/zentyal-network

firewall_script=/usr/share/zentyal-firewall/dhcp-firewall.pl

ebox_bound() {
    $libexec/dhcp-address.pl $interface $new_ip_address $new_subnet_mask
    $libexec/dhcp-nameservers.pl $interface $new_domain_name_servers
    $libexec/dhcp-gateway.pl $interface $ebox_routers

    if ( [ -x $firewall_script ] ); then
        $firewall_script
    fi;
}

ebox_renew() {
    change="no"
    if [ "z$new_ip_address" != "z$old_ip_address" ] ; then
        change="yes"
    fi
    if [ "z$new_subnet_mask" != "z$old_subnet_mask" ] ; then
        change="yes"
    fi
    if [ "$change" == "yes" ] ; then
        $libexec/dhcp-address.pl $interface $new_ip_address \
                    $new_subnet_mask
    fi
    if [ "z$new_domain_name_servers" != "z$old_domain_name_servers" ] ; then
        change="yes"
        $libexec/dhcp-nameservers.pl $interface $new_domain_name_servers
    fi
    if [ "z$ebox_routers" != "z$old_routers" ] ; then
        $libexec/dhcp-gateway.pl $interface $ebox_routers
    fi
    if [ "$change" == "yes" ] ; then
        if ( [ -x $firewall_script ] ); then
            $firewall_script
        fi;
    fi
}

ebox_expire() {
    $libexec/dhcp-clear.pl $interface

    if ( [ -x $firewall_script ] ); then
        $firewall_script $interface
    fi;
}

# Do nothing if network module is disbled
if  /etc/init.d/zentyal network status; then
    case $reason in
        BOUND)
            ebox_bound
            ;;
        REBOOT|RENEW|REBIND|TIMEOUT)
            ebox_renew
            ;;
        EXPIRE|FAIL|RELEASE)
            ebox_expire
            ;;
        *)
            ;;
    esac
fi
