#!/bin/sh
export Lang=en_US
export LC_ALL=en_US
vm_vnc_port_start=5900

XM_LIST=`mktemp 2>/dev/null`

if [ "x${XM_LIST}" = "x" -o ! -f $XM_LIST ]; then
    exit 0
fi

virsh list 2>/dev/null | awk '{if ($1 ~ /^[0-9]+$/ && $2 !~ /STORAGENODE/ && $2 !~ /Domain-0/) print $1" "$2 }' |\
while read vmid identifier ; do
    vm_vnc_port=`virsh vncdisplay $identifier 2>/dev/null | sed 's/.*://g' 2>/dev/null`
    [ -z $vm_vnc_port ] && continue
    vm_vnc_port=$(($vm_vnc_port_start+$vm_vnc_port))
    printf "\t%s\t%s\t%s\n" $identifier $vmid $vm_vnc_port >> $XM_LIST
    unset vm_vnc_port
    unset vmid
    unset identifier
done
unset vm_vnc_port_start

cat $XM_LIST
rm -f $XM_LIST

exit 1
