#!/usr/local/bin/perl

use strict;
use warnings;

use Astro::SIMBAD::Client;
use Getopt::Long;

my $usage = <<eod;

Query Simbad 4 for a list of objects in VOTable format.

usage: votable [options] object ...

where the valid options are:
  -dumper
    causes the result to be parsed by Parse_VO_Table and then dumped by
    Data::Dumper::Dumper;
  -help
    displays this text;
  -url_query
    performs a query by URL, rather than a SOAP query;
  -yaml
    causes the result to be parsed by Parse_VO_Table and then dumped by
    YAML::Dump;
eod

my %opt;

(GetOptions (\%opt,
    help => sub {print $usage; exit},
    qw{dumper url_query yaml}) && @ARGV)
    or die $usage;

my $simbad = Astro::SIMBAD::Client->new (type => 'vo');

my @dump_as;

if ($opt{dumper}) {
    eval {
	require Data::Dumper;
	push @dump_as, ['Data::Dumper::Dumper', Data::Dumper->can('Dumper')];
	1;
    } or die ( $@ || 'Unable to load Data::Dumper for unknown reason' );
}

if ($opt{yaml}) {
    eval {
	require YAML;
	push @dump_as, ['YAML::Dump', YAML->can('Dump')];
	1;
    } or eval {
	require YAML::Syck;
	push @dump_as, ['YAML::Syck::Dump', YAML::Syck->can('Dump')];
	1;
    } or die ( $@ || 'Unable to load YAML or YAML::Syck for unknown reason' );
}

$simbad->set (parser => {vo => 'Parse_VO_Table'}) if @dump_as;

foreach my $obj (@ARGV) {
    my $rslt = $opt{url_query} ?
	$simbad->url_query (id => Ident => $obj) :
	$simbad->query (id => $obj);
    if (!$rslt) {
	warn <<eod;
Warning - Object $obj not found.
eod
    } elsif (@dump_as) {
	foreach (@dump_as) {
	    print "\n$_->[0] output:\n", $_->[1]->($rslt);
	}
    } else {
	print $rslt;
    }
}

__END__

=head1 TITLE

votable - Download SIMBAD data in votable format

=head1 SYNOPSIS

 votable Arcturus >arcturus.vo
 votable -help

=head1 OPTIONS

=over

=item -dumper

This option causes the result to be parsed, and displayed by
L<Data::Dumper|Data::Dumper>.

=item -help

This option displays documentation and then exits.

=item -url_query

This option causes SIMBAD to be queried using a URL query rather than a
SOAP query.

=item -yaml

This option causes the result to be parsed, and displayed by either
L<YAML|YAML> or L<YAML::Syck|YAML::Syck>.

=back

=head1 DETAILS

This script takes as its arguments the names of objects to be looked up
in the SIMBAD database and returned in C<votable> format. The return is
printed to standard out, possibly after parsing.

=head1 AUTHOR

Thomas R. Wyant, III F<wyant at cpan dot org>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2006, 2008, 2010-2012 by Thomas R. Wyant, III

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl 5.10.0. For more details, see the full text
of the licenses in the directory LICENSES.

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.

# ex: set textwidth=72 :
