# To use it you need to include:
# /opt/multiwan/lib/bash/common_lib
# /opt/multiwan/lib/bash/common_vars

function debug {

    if [[ $DEBUG != "yes" ]]; then
        return 0
    fi

    local MSG="$1"

    # Example: 13/09/2018 08:47:22 ...
    $ECHO $($DATE +"%d/%m/%Y %X") "$MSG" >> $DEBUG_FILE

}

function check_if_multiwan_autostart_enabled {

    if is_openwrt_os; then
        if $OPENWRT_SERVICE multiwan enabled > /dev/null 2>&1; then
            return 0
        fi
    elif is_debian_os || is_astra_os; then
        if is_used_systemd; then
            if $SYSTEMCTL -q is-enabled multiwan.service > /dev/null 2>&1; then
                return 0
            fi
        else
            # Note: Default Debian installation does not make any difference between runlevels 2-5.
            #       See https://wiki.debian.org/RunLevel.
            if $LS /etc/rc[2-5].d/S[0-9][0-9]multiwan > /dev/null 2>&1; then
                return 0
            fi
        fi
    else
        # Can not properly check.
        to_syslog "error" "impossible to determine whether the multiwan service should be started or not."
        return 1
    fi

    return 1
}

function get_multiwan_daemon_pid {
    # $MULTIWAN_FILE_PID is global variable.
    local DAEMON_PID=""
    local OUT=""
    local RESULT=""

    OUT=$(check_multiwan_pid_file $MULTIWAN_FILE_PID)
    RESULT=$?
    if (( $RESULT != 0 )); then
        to_syslog "error" "$OUT"
        return 1
    fi

    $ECHO "$OUT"
    return 0
}

function send_signal_to_multiwan_for_reload_dhcp_configuration {

    local SIGNAL_NAME="SIGUSR1"
    local MULTIWAN_PID=""
    local RESULT=""

    MULTIWAN_PID=$(get_multiwan_daemon_pid)
    RESULT=$?
    if (( $RESULT == 0 )); then
        $KILL -s $SIGNAL_NAME $MULTIWAN_PID
        RESULT=$?
        if (( $RESULT != 0 )); then
            to_syslog "error" "can not send signal \"$SIGNAL_NAME\" to multiwan daemon with PIDs \"$MULTIWAN_PID\"."
            return 1
        fi
    else
        to_syslog "error" "can not send signal \"$SIGNAL_NAME\" to multiwan daemon, because obtaining of PID has failed."
        return 1
    fi

    return 0
}

function write_first_router_ip_to_file {
    # $MULTIWAN_RUN_DIR is global variable.
    # $FILE_SUFFIX_FOR_IFACE_TO_STORE_NEXTHOP_ROUTER_IP_FROM_DHCP is global variable.
    local IFACE="$1"
    local ROUTERS="$2"
    local FILE="$MULTIWAN_RUN_DIR/$IFACE.$FILE_SUFFIX_FOR_IFACE_TO_STORE_NEXTHOP_ROUTER_IP_FROM_DHCP"
    local FILE_TMP="$FILE.tmp"
    local ROUTER=""
    local RESULT=""

    if [[ $ROUTERS == "" ]]; then
        return 0
    fi

    for ROUTER in $ROUTERS; do
        # Support only first nexthop IP in list.
        break
    done

    # Create dir for store runtime files (in case when daemon is not started).
    if ! create_run_dir $MULTIWAN_RUN_DIR ; then
        to_syslog "error" "can not create directory $MULTIWAN_RUN_DIR"
        return 1
    fi

    # Save the default router IP to temporary file.
    $ECHO $ROUTER > "$FILE_TMP"
    RESULT=$?
    if (( $RESULT != 0 )); then
        to_syslog "error" "can not write the nexhop IP \"$ROUTER\" to the file \"$FILE_TMP\"."
        return 1
    fi

    # Save the default router IP to the file $FILE. $FILE is read
    # by multiwan daemon ('reload_dhcp' function).
    $MV $FILE_TMP $FILE # Rename is atomic operation.
    RESULT=$?
    if (( $RESULT != 0 )); then
        to_syslog "error" "can not rename file \"$FILE_TMP\" to \"$FILE\"."
        return 1
    fi    
    
    return 0
}