#!/bin/sh
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


set -e

XMLFILE=$1
TYPE=$2

if [ -z "$XMLFILE" ]; then
  echo "syntax: $0 XMLFILE [TYPE]"
  exit 1
fi

if [ ! -f "$XMLFILE" ]; then
  echo "$0: document $XMLFILE does not exist"
  exit 2
fi

if [ -z "$TYPE" ]; then
  ROOT=`xmllint --stream --debug $XMLFILE 2>/dev/null | grep "^0 1 " | awk '{ print $3 }'`
  case $ROOT in
     *domain*)
        TYPE="domain"
        ;;
     *network*)
        TYPE="network"
        ;;
     *pool*)
        TYPE="storagepool"
        ;;
     *volume*)
        TYPE="storagevol"
        ;;
     *capabilities*)
        TYPE="capability"
        ;;
     *device*)
        TYPE="nodedev"
        ;;
     *)
       echo "$0: cannot determine schema type for $XMLFILE"
       exit 3
  esac
fi

SCHEMA="@SCHEMADIR@/${TYPE}.rng"

if [ ! -f "$SCHEMA" ]; then
  echo "$0: schema $SCHEMA does not exist"
  exit 4
fi

xmllint --noout --relaxng $SCHEMA $XMLFILE

exit 0

: <<=cut
=pod

=head1 NAME

  virt-xml-validate - validate libvirt XML files against a schema

=head1 SYNOPSIS

  virt-xml-validate XML-FILE [SCHEMA-NAME]

=head1 DESCRIPTION

Validates a libvirt XML for compliance with the published schema.
The first compulsory argument is the path to the XML file to be
validated. The optional second argument is the name of the schema
to validate against. If omitted, the schema name will be inferred
from the name of the root element in the XML document.

Valid schema names currently include

=over 4

=item C<domain>

The schema for the XML format used by guest domains configuration

=item C<network>

The schema for the XML format used by virtual network configuration

=item C<storagepool>

The schema for the XML format used by storage pool configuration

=item C<storagevol>

The schema for the XML format used by storage volume descriptions

=item C<nodedev>

The schema for the XML format used by node device descriptions

=item C<capability>

The schema for the XML format used to declare driver capabilities

=back

=head1 EXIT STATUS

Upon successful validation, an exit status of 0 will be set. Upon
failure a non-zero status will be set.

=head1 AUTHOR

Daniel P.Berrange

=head1 BUGS

Report any bugs discovered to the libvirt community via the
mailing list C<http://libvirt.org/contact.html> or bug tracker C<http://libvirt.org/bugs.html>.
Alternatively report bugs to your software distributor / vendor.

=head1 COPYRIGHT

Copyright (C) 2009-2010 by Red Hat, Inc.
Copyright (C) 2009 by Daniel P. Berrange

=head1 LICENSE

virt-xml-validate is distributed under the terms of the GNU GPL v2+.
This is free software; see the source for copying conditions. There
is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE

=head1 SEE ALSO

C<virsh(1)>, online XML format descriptions C<http://libvirt.org/format.html>

=cut
