#!/bin/bash
IPMAN_LIB_PATH=$(dirname ${BASH_SOURCE[0]})
source "${IPMAN_LIB_PATH}/ip-functions.sh"
source "${IPMAN_LIB_PATH}/config.sh"

function _get_net_status() {
    local interface=${1}
    local net_type=${2}
    local interface_json=${3}

    if [[ $(jq -r ".ipv4.${net_type}.enabled" <<< "${interface_json}") == "true" ]] &&
       [[ $(systemctl is-active "network-${net_type}@${interface}.service") == "active" ]]; then
        local ip
        ip=$(ipman_get_ip_address_from_labeled_interface_name "${interface}" "${net_type}")
        if [[ $? -ne 0 ]]; then
            if [[ "${net_type}" == "dhcp" ]]; then
                if [[ $(jq -r ".ipv4.dhcp.linklocalFallback" <<< "${interface_json}") == "true" ]]; then
                    local ll_ip
                    ll_ip=$(ipman_get_ip_address_from_labeled_interface_name "${interface}" "linklocal")
                    if [[ $? -eq 0 ]]; then
                        echo -e "fallback\n${ll_ip}";
                        return 0;
                    fi
                fi
            fi

            echo "pending";
            return 0;
        fi
        local valid=""
        valid="valid\n${ip}"

        # Get gateway from interface (dhcp/static)
        if [[ "${net_type}" == "dhcp" || "${net_type}" == "static" ]]; then
            local gateway
            gateway=$(ipman_get_default_route_gateway "${interface}" "${net_type}")
            if [[ -n "${gateway}" ]]; then
                valid+="\n${gateway}"
            fi
        fi

        echo -e "${valid}";
    else
        echo "disabled";
    fi
}

function get_status() {
    local dynamic_config="${1}"
    shift
    local pos_args=("${@}")
    
    if [[ ${#pos_args[@]} -gt 2 ]]; then
        echo "${#pos_args[@]} !! Too much argument for: status <interface> <static|dhcp|linklocal>" 
        return 1
    fi

    if ! config_data=$(ipman_read_config "${dynamic_config}"); then
        echo "Status command failed! Reason: Dynamic config read failed."
        return 1
    fi

    local interface_names;
    interface_names=$("${IPMAN_LIB_PATH}"/ipman-list-interfaces "${config_data}")
    local interfaces=${interface_names}
    if [[ ${#pos_args[@]} -gt 0 ]]; then
        interfaces="${pos_args[0]}"
        if ! grep -q "${interfaces}" <<< "${interface_names}"; then
            echo "Invalid interface name: ${interfaces}. Possible interface names: ${interface_names}"
            return 1
        fi
    fi

    for interface in ${interfaces}; do
        local interface_json
        if ! interface_json=$(ipman_get_interface_config "${interface}" "${config_data}"); then
            echo "Fail to get interface <${interface}> details."
            return 1
        fi

        if [[ ${#pos_args[@]} -gt 1 ]]; then
            local net_type="${pos_args[1]}"
            case ${net_type} in
                static|linklocal|dhcp)
                    # print header only if multiple statuses shown
                    [[ "${interface}" != "${interfaces}" ]] && echo "Status for ${interface} ${net_type}:"
                    _get_net_status "${interface}" "${net_type}" "${interface_json}"
                    ;;
                *)
                    echo "Invalid network type: ${net_type}. Possible network types: static|dhcp|linklocal"
                    return 1
                    ;;
            esac
        else
            for net_type in "static" "dhcp" "linklocal"; do
                echo "Status for ${interface} ${net_type}:"
                _get_net_status "${interface}" "${net_type}" "${interface_json}"
            done
        fi
    done
}

get_status "${@}"