#!/bin/sh
#
# Copyright (c) Citrix Systems 2008. All rights reserved.
#

# Usage: ssh root@<host> @LIBEXECDIR@/interface-visualise > <host>-network.dot
#        dotty <host>-network.dot

echo "digraph G {"
for i in ${1:-/etc/sysconfig/network-scripts}/ifcfg-* ; do
    case $i in
	*.xapi-new) continue ;;
	*.xapi-old) continue ;;
	*) ;;
    esac

    echo "$i" 1>&2
    (
	source "$i"
	if [ X"${XEMANAGED}" = "Xyes" ] ; then
	    XEMANAGED="\nXE"
	fi
	if [ -n "${BOOTPROTO}" ] ; then
	    echo "\"${DEVICE}\" [label=\"\N\n(${BOOTPROTO})${XEMANAGED}\"]"
	else
	    echo "\"${DEVICE}\" [label=\"\N${XEMANAGED}\"]"
	fi
	if [ -n "${BRIDGE}" ] ; then
	    echo "\"${BRIDGE}\" -> \"${DEVICE}\""
	fi
	if [ -n "${MASTER}" ] ; then
	    echo "\"${MASTER}\" -> \"${DEVICE}\""
	fi
	if [ "${VLAN}"X = "yesX" ] && [ -z "${PHYSDEV}" ] ; then
	    PHYSDEV=${DEVICE%.*}
	    #PHYSDEV=$(echo ${DEVICE} | LC_ALL=C sed 's/^[a-z0-9]*\.0*//')
	fi
	if [ -n "${PHYSDEV}" ] ; then
	    echo "\"${DEVICE}\" -> \"${PHYSDEV}\""	    
	fi
    )
done
echo "}"
