The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/perl
#ABSTRACT: DAIA command line client
#PODNAME: daia
our $VERSION = '0.43'; #VERSION

use strict;
use warnings;

use DAIA;
use Data::Dumper;
use JSON;
use Getopt::Long;
use Pod::Usage;

my %formats = DAIA->formats;
my ($help,$man,$input,$output,$debug,$callback,$version);

GetOptions(
    'help|?'     => \$help,
    'man'        => \$man,
    'output:s'   => \$output,
    'input:s'    => \$input,
    'debug'      => \$debug,
    'version'    => \$version,
    'callback:s' => \$callback
);
pod2usage(-verbose => 2) if defined $man;
pod2usage(-verbose => 1) if defined $help;

if ($version) {
    print "This is DAIA version ". $DAIA::VERSION . " with the following output formats:\n";
    print join "\n", map({ "  $_" } keys %formats), '';
    exit;
}

$Carp::Verbose = 1 if $debug;
$output ||= 'json';
$callback = "" unless $callback and $callback =~ /^[a-z][a-z0-9._\[\]]*$/i;

pod2usage(-message => "Unsupported output format $output")
    unless grep { $_ eq $output } keys %formats;

# TODO: support validating
# my $xsd = "daia.xsd";

my $url = \*STDIN;

if (@ARGV) {
    if ($ARGV[0] =~ /^\s*http[s]?:\/\//) {
        $url = shift @ARGV;
    } elsif ($ARGV[0] eq '-') {
        shift @ARGV;
        pod2usage( -message => 'cannot read from STDIN plus other sources' ) if @ARGV;
    } else {
        $url = undef;
    }
}

binmode STDOUT, "utf8";

my @daiaobjs;

if( $url and not @ARGV ) {
    @daiaobjs = eval { DAIA->parse( file => $url, format => $input ) };
    goto OUTPUT;
}

while (@ARGV) {
    my $id = shift @ARGV;

    if ($url) { $id = $url . ($url =~ /\?/ ? '&' : '?') . "id=$id"; }
    @daiaobjs = eval { DAIA->parse( file => $id, format => $input ) };

OUTPUT:
    if ($@) {
        unless ($debug) {
            $@ =~ s/DAIA::([A-Z]+::)?[a-z_]+\(\)://ig;
            $@ =~ s/ at .* line.*//g;
        }
        print STDERR "$@\n";
        exit 1;
    }
    foreach my $daia (@daiaobjs) {
        if ($output eq 'xml') {
            print $daia->xml(xmlns => 1,header=>1);
        } elsif ($output eq 'dump') {
            print Dumper($daia);
        } else {
            print $daia->serialize($output);
        }
    }
    print "\n";
}

__END__

=pod

=encoding UTF-8

=head1 NAME

daia - DAIA command line client

=head1 VERSION

version 0.43

=head1 SYNOPSIS

daia [<OPTIONS>] [ <FILES> | <URL> [<IDS>] ]

 Options:
   -?|-h|-help      show this help
   -man             full documentation
   -input FORMAT    set input format (json or xml)
   -output FORMAT   set output format
      json             DAIA/JSON (default)
      xml              DAIA/XML
      rdfjson          DAIA/RDF as RDF/JSON
      dump             Perl dump format (only for debugging)
      ...              see -version for a full list
  -callback        add callback method (if out=json)
  -debug           enable debugging mode
  -version         show CPAN module version and list output formats

=head1 DESCRIPTION

This command line client can be used to query and transform DAIA. You can pass
either an URL and and identifier or a file or DAIA format is read from STDIN.
The serialization input format (JSON or XML) can be specified or it will be
guessed. 

=head1 EXAMPLES

To get usage information call this script with C<-?>, C<-h> or C<--help> as 
only parameter. Options can be passed as C<key=value> pairs and the first 
parameter is treated as filename or URL to read from (use '-' for STDIN as
set by default).

  daia -o json input.xml   # convert to DAIA/JSON (default)
  daia -o xml  input.json  # convert to DAIA/XML
  daia -o xml http://example.org/ id1 id2 id3
  daia http://example.org/?id=123

=head1 SEE ALSO

See L<Plack::App::DAIA> and L<Plack::App::DAIA::Validator> for DAIA server
implementations. 

=head1 AUTHOR

Jakob Voß

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2013 by Jakob Voß.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut