#!/bin/sh

function usage {
	echo "Usage: $0 [-a | -d domain-id | -t | -v]"
	echo "  -a : print all xenstore nodes\n"
	echo "  -d : print /local/domain nodes for a specified domain\n"
	echo "  -t : print /tool nodes\n"
	echo "  -v : print /vm nodes\n"
	exit 1
}

if [ $# == 0 ] ; then
	usage
	exit 
fi

function getval() {
	local arg=${1}
	val=`xenstore-list $arg`
#	echo "dumpkey called with $arg, value $val"
	if [ x"$val" != "x" ] ; then
		for key in $val ; do
			getval $arg/$key
		done
	else
		echo -n $arg'='
	    xenstore-read $arg    
	fi
}

while getopts "d:atv" opt
do
  case $opt in
      a  ) args="/vm /local/domain /tool"
		   echo "Dumping all xenstore pairs:"
		   ;;
      d  ) domainID=$OPTARG
		   echo "Printing nodes for domain ID : $domainID"
		   args="/local/domain/${domainID}"
		   ;;
      t  ) args="/tool"
		   echo "Dumping xenstore /tool info:"
		   ;;
      v  ) args="/vm"
		   echo "Dumping xenstore /vm info:"
		   ;;
      \? ) usage
           exit 1;;
  esac
done

for node in ${args} ; do
	getval $node
done

