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", "template=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 %template_parameters;

if (@ARGV) {
    if ($ARGV[0] eq '-') {
        while (<>) {
            my ($key, $val) = split(/(?:\s+|\s?=\s?)/, $_, 2);
            chomp $val;
            if ($key eq "name") {
                $config->template($val);
            } else {
                if ($key) {
                    pod2usage("Could not parse parameters") unless $val;
                    $template_parameters{$key} = $val;
                }
            }
        }
    } else {
        for my $pair (@ARGV) {
            my ($k, $v) = split(/=/, $pair, 2);
            pod2usage("Could not parse parameters") unless $v;
            $template_parameters{$k} = $v;
        }
    } 
}
pod2usage("template name is required") unless $config->template;
pod2usage("No parameters supplied") unless %template_parameters;

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 $template = $service->template($config->template);

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

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

exit;

__END__

=head1 NAME

run-im-template - Run an InterMine template by passing a list of parameters

=head1 SYNOPSIS

    run-im-query [options] parameter=value...

    Options:
      --template the name of the template to query
      --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 

    EXAMPLE:
    run-im-template --url www.flymine.org/query --template Pathway_Genes opA=eq valueA="Pentose phosphate pathway" opB=eq valueB="KEGG pathways data set" opC=eq valueC="Drosophila*"

=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<--template>

The name of the template to run

=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