The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/perl

use strict;
use warnings;

use AppConfig (':argcount');
use Pod::Usage;
use Webservice::InterMine;

my $config = AppConfig->new(
    "size=s", "start=s", "url=s", "user=s", "pass=s", "help!", "man!", "count!"
);

pod2usage(1) if $config->help;
pod2usage( -exitstatus => 0, -verbose => 2 ) if $config->man;

my $config_file = $ENV{HOME} . '/.webservice-intermine.config';
$config->file($config_file) if (-r $config_file);
$config->getopt();

pod2usage("url is required") unless $config->url;

my $file = shift(@ARGV) or pod2usage(2);
my $string;

if ($file eq '-') {
    # read query from std in
    while (<>) {
        $string .= $_;
    }
}

my @service_args = ($config->url);
push @service_args, $config->user, $config->pass if ($config->user && $config->pass);

my $service = Webservice::InterMine->get_service(@service_args);

my @load_args = $string 
    ? (source_string => $string) 
    : (source_file => $file);
my $query = $service->new_from_xml(@load_args);

my $as = $config->count ? "count" : "string";

print $query->results(as => $as, start => $config->start, size => $config->size), "\n";

exit;

__END__

=head1 NAME

run-im-query - Run an InterMine query stored as an XML file

=head1 SYNOPSIS

    run-im-query [options] file

    Options:
      --url     the webservice url
      --start   the index of the first result to return (starts at 0)
      --size    the total number of results to return
      --count   don't return results - just return the count of rows
      --user    user name for queries that require authentication
      --pass    password for queries that require authentication 

    file should be a readable XML file.

    EXAMPLE: 
    run-im-query --url www.flymine.org path/to/query.xml

=head1 OPTIONS

=over 8

=item B<--help>

Print a brief help message and exits.

=item B<--man>

Prints the manual page and exits.

=item B<--url>

The url of the webservice to use.

=item B<--start>

The index of the first result to include (starts at 0)

=item B<--size>

The maximum size of the result set to return.

=item B<--count>

Just print the count of the results for this query (incompatible 
with size and start).

=back

=head2 DEFAULT CONFIGURATION

The url option will need to be provided for each query. It (and all 
other options) can be set in a configuration file. The location for this
configuration is B< ~/.webservice-intermine.config > 

eg:

  # Webservice::InterMine configuration

  --url www.flymine.org/query
  --size 50