# wg_dhcp_enter_hook: Flush/delete old routing rules
#
# Note: Must be a bash-compatible script

case "$reason" in
    "BOUND"|"RENEW"|"REBIND"|"REBOOT"|"EXPIRE"|"FAIL"|"RELEASE"|"STOP")
        if [ -n "$interface" ] && grep -q "wg_${interface}" /etc/iproute2/rt_tables ; then
            # Flush interface specific routing table
            /sbin/ip route flush table wg_${interface} || true

            # Delete old rules if necessary
            if [ -n "$old_ip_address" ] ; then
                if /sbin/ip rule show | grep -q "from $old_ip_address" ; then
                    /sbin/ip rule del from $old_ip_address table wg_${interface} || true
                fi
                if /sbin/ip rule show | grep -q "to $old_ip_address" ; then
                    /sbin/ip rule del to $old_ip_address table wg_${interface} || true
                fi
            fi
        fi
    ;;
esac
