#!/usr/bin/perl

=head1 NAME

xacobeo - Graphical interface for running XPath queries.

=head1 SYNOPSIS

xacobeo [OPTION]... [file [xpath]]

Options:

   -h, --help            brief help message

Where I<file> is a valid XML document and I<xpath> a valid XPath query.

=head1 OPTIONS

=over 8

=item B<--help>

Print a brief help message and exits.

=back

=head1 DESCRIPTION

This program provides a simple graphical user interface (GUI) for executing
XPath queries and seeing their results.

The GUI tries to provide all the elements that are needed in order to write,
test and execute XPath queries without too many troubles. It displays the
Document Object Model (DOM) and the namespaces used. The program registers the
namespaces automatically and each element is displayed with it's associated
namespaces. All is performed with the idea of being able of running an XPath
query as soon as possible without having to fight with the document's namespaces
and by seeing automatically under which namespace each element is.

This program is not an XML editor, at least not at this point, it's meant to be
used for constructing and executing XPath queries.

=head1 RATIONALE

The main idea behind this application is to provide a simple way for building
XPath queries that will be latter integrated in to a program or XSLT
transformation paths. Therefore, this program goal is to load an XML document
and to display it as an XML parser sees it. Thus each node element is prefixed
with it's namespace.

=head1 IMPLEMENTATION

This program uses L<XML::LibXML> (libxml2) for all XML manipulations and L<Gtk2>
for the graphical interface.

=head1 LIMITATIONS

For the moment, the program focuses only on XPath and doesn't allow the XML
document to be edited.

=head1 AUTHOR

Emmanuel Rodriguez E<lt>potyl@cpan.orgE<gt>.

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2008 by Emmanuel Rodriguez.

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.8.8 or,
at your option, any later version of Perl 5 you may have available.

=cut

use strict;
use warnings;
use open qw(:std :utf8); # STD* in UTF-8

use Gtk2 qw(-init);

use Pod::Usage;
use Getopt::Long qw(:config auto_help);
use File::Spec::Functions;
use FindBin;

use Xacobeo::UI;
use Xacobeo::I18n;


exit main();


#
# Main entry point of the program
#
sub main {

	# Parse the command line options
	GetOptions() or pod2usage(2);
	
	# Create a new instance of this application
	my $folder = find_app_folder();
	my $domain = 'xacobeo';
	Xacobeo::I18n->init($domain => catdir($folder, 'share', 'locale'));

	my $xacobeo = Xacobeo::UI->new($folder, $domain);
	
	if (@ARGV) {
		my ($source, $xpath) = @ARGV;
		$xacobeo->load_file($source);
		$xacobeo->set_xpath($xpath) if defined $xpath;
	}
	
	# Start the main loop
	Gtk2->main();
	
	return 0;
}


# Return the root folder of the application once installed. The 'root' folder is
# the one where the installation is done, the root folder hierarchy is as
# follows:
#
# (root)
# |-- bin
# |-- lib
# |   `-- perl5
# |       |-- Xacobeo
# |       `-- i486-linux-gnu-thread-multi
# |               `-- Xacobeo
# |           `-- auto
# |               `-- Xacobeo
# |-- man
# |   |-- man1
# |   `-- man3
# `-- share
#     |-- applications
#     |-- pixmaps
#     `-- xacobeo
sub find_app_folder {
	return catdir($FindBin::Bin, '..');
}
