#!/bin/bash

test -n "${OS_VERSION}" || source /etc/init.d/base

msg_info() {
    echo " # $1"
}

show_iface_ip_addr() {
    addr=$(ip addr show dev $1 2>/dev/null | grep inet | tr -s ' ' | sed -r 's/^\s+//' | \
           cut -d ' ' -f 2 | grep -iv fe80 | sed 'N;s/\n/, /')
    test -n "${addr}" && msg_info "Interface $1 has IP address ${addr}"
}

show_gateway() {
    gateway=$(ip route | grep default | cut -d ' ' -f 3)
    test -n "${gateway}" && msg_info "Default gateway is ${gateway}"
}

show_dns() {
    test -r /etc/resolv.conf || return
    dns=$(cat /etc/resolv.conf | grep nameserver | head -n 1 | cut -d ' ' -f 2)
    test -n "${dns}" && msg_info "DNS server address is ${dns}"
}

case "$1" in
    start)
        test -n "${OS_ETH}" && show_iface_ip_addr ${OS_ETH}
        test -n "${OS_WLAN}" && show_iface_ip_addr ${OS_WLAN}
        test -n "${OS_WLAN1}" && show_iface_ip_addr ${OS_WLAN1}
        test -n "${OS_PPP}" && show_iface_ip_addr ${OS_PPP}
        show_gateway
        show_dns
        ;;

    stop)
        true
        ;;

    *)
        echo "Usage: $0 {start}"
        exit 1
esac

exit 0
