#!/usr/local/bin/perl

use strict;
use warnings;

use Astro::SIMBAD::Client;
use Getopt::Long 2.33;
use Pod::Usage;

our $VERSION = '0.047_01';

my %opt;

GetOptions (\%opt,
    help => sub { pod2usage( { -verbose => 2 } ) },
    qw{ dumper! json! url_query! yaml! } ) and @ARGV
    or pod2usage( { -verbose => 0 } );

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{json} ) {
    eval {
	require JSON;
	push @dump_as, [ 'JSON', sub { JSON::to_json( $_[0], { pretty =>
			1 } ) } ];
	1;
    } or die ( $@ || 'Unable to load JSON for unknown reason' );
}

if ($opt{yaml}) {
    eval {
	require YAML;
	push @dump_as, ['YAML::Dump', YAML->can('Dump')];
	1;
    } or die ( $@ || 'Unable to load YAML 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 -json

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

=item -url_query

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

=item -version

This option displays the version and then exits.

=item -yaml

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

=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-2025 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 files F<LICENSE-Artistic> and F<LICENSE-GNU>.

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 :
