#!/bin/bash
# Note: We use a bashism (named pipes to avoid pipelining) in this script so
# don't use /bin/sh

# ifconfig display compatability command


if [ $# -gt 1 ]; then
	echo "This command is provided for display compatablity only."
	echo "Please use the \"ip\" command for all other functionality."
	exit 1
fi

interface=""
flags=""
hwaddr=""
link=""
ip4=""
brd=""
ip6=""

warn_compat() {
	cat >&2 <<EOF

*WARNING* This command is deprecated.
Please use "ip addr" instead.

EOF
}

warn_compat

flush_values() {
	if [ -n "$interface" ]
	then
	    echo -e "$interface \tLink encap:$link $hwaddr"
	    [ -n "$ip4" ] && echo -e "\tinet addr:$ip4 $brd"
	    [ -n "$ip6" ] && echo -e "\tinet6 addr:$ip6"
	    [ -n "$flags" ] && echo -e "\t$flags"
	    echo ""
	fi
	interface=""
	flags=""
	hwaddr=""
	link=""
	ip4=""
	brd=""
	ip6=""
}

while read a b c d e f g 
do
	case $a in
	[0-9]*:) 
		flush_values
		interface=$(echo $b | sed 's/://')
		flags=$(echo $c | sed -e 's/<//' -e 's/>//')
		;;
	"link/"*)
		link=$(echo $a | sed 's,link/,,')
		[ $a == "link/ether" ] && hwaddr="HWaddr $b"
		;;
	"inet")
		ip4=$b
		[ $c == "brd" ] && brd="Bcast:$d"
		[ $c == "peer" ] && brd="Peer:$d"
		;;
	"inet6")
		ip6=$b
		;;
	*)
		;;
	esac
done < <( ip addr list $1 )

flush_values
warn_compat
