#!/bin/bash

# This hook using for prevent from install nexthop IP (if multiwan autostart enabled).

DEBUG="yes"
DEBUG_FILE="/tmp/multiwan_dhcp-hook.debug"

. /opt/multiwan/lib/bash/common_vars
. /opt/multiwan/lib/bash/common_lib
. /opt/multiwan/lib/bash/dhcp_hook_lib

function main {

    local RESULT=""
    
    debug "INFO: reason=$reason, interface=$interface, new_routers=$new_routers, old_routers=$old_routers, new_dhcp_lease_time=$new_dhcp_lease_time."
    
    # Processing reason REBOOT is required to install nexthop in case of emergency gateway reboot without release DHCP address.
    if [[ $reason == "BOUND" ]] || [[ $reason == "RENEW" ]] || [[ $reason == "REBOOT" ]]; then
        write_first_router_ip_to_file "$interface" "$new_routers"
    
        check_if_multiwan_autostart_enabled
        RESULT=$?
        if (( $RESULT == 0 )); then 
            # If multiwan autostart was enabled then only it insert nexthop IP in to route table.
            unset new_routers
        fi        
    fi
}

main
