#!/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')];
    };
    $@ and die $@;
}
if ($opt{yaml}) {
    eval {
	require YAML;
	push @dump_as, ['YAML::Dump', YAML->can('Dump')];
	1;
    } || eval {
	require YAML::Syck;
	push @dump_as, ['YAML::Syck::Dump', YAML::Syck->can('Dump')];
	1;
    };
    $@ and die $@;
}

$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__

Copyright 2006, 2008 by Thomas R. Wyant, III (F<wyant at cpan dot org>).
All rights reserved.

This script is free software; you can use it, redistribute it
and/or modify it under the same terms as Perl itself. Please see
L<http://perldoc.perl.org/index-licence.html> for the current licenses.

