#! /usr/bin/perl -w
# $Header: $
# HACK to allow "cl-onserver --xen-clients" to kind of work

# Need to be able to find xe
$ENV{"PATH"} .= ":/opt/xensource/bin";

# The command to run
$cmd = "xe vm-list";

# emulate list
&list if $#ARGV == 0 && $ARGV[0] eq "list";

# Only emulate "list" so far
die("$0: called with $#ARGV @ARGV\n");

sub list {
	open(XE, "$cmd|") || die("$0: $cmd open failed $!\n");
	# uuid ( RO)           : 2221065c-6381-53ba-0aea-796294051000
	#      name-label ( RW): localhost.localdomain (Red Hat 4.6)
	#     power-state ( RO): running
	print "Name                                        ID   Mem VCPUs      State   Time(s)\n";
	while(<XE>) {
		chomp;
		$uid   = $1 if /uuid.*:\s+(\S+)/;
		$name  = $1 if /name-label[^:]*:\s+(.+)/;
		$state = $1 if /power-state.*:\s+(\S+)/;
		&dump if /^$/;
	}
	close(XE) || die("$0: $cmd close failed $!\n");
	&dump;
	exit(0);
}

# dump the pending data
sub dump {
	return unless $uid;
	my ($n) = $name;
	my ($s) = "?";
	$s = "r" if $state eq "running";
	$s = "s" if $state eq "suspended";
	$n = "Domain-0" if $n =~ /Control domain on host/;
	$n =~ s/\s+/_/g;
	$n = $uid if $uid && $n =~ /\s/;
	$n = $uid if $uid && length($n) > 23;
	print "$n 0 0 0 $s----- 0.0\n";
	$uid = '';
}
