#! /usr/bin/env perl

# $Id: detDSFetch 181 2007-03-09 00:17:10Z yamada $

#use lib '/home/yamada/prj/panstarrs/mops/src';

use warnings;
use strict;
use Getopt::Long;
use Pod::Usage;
use HTTP::Request::Common;
use LWP::UserAgent;

my $help;
my $datastore = $ENV{'MOPS_DATASTORE'};
my $instance_name = $ENV{'MOPS_DBINSTANCE'};

GetOptions(
	'datastore=s' => \$datastore,
	'instance=s' => \$instance_name,
    help => \$help,
) or pod2usage(2);                      # opts parse error
pod2usage(-verbose => 3) if $help;      # user asked for help
die("Must specify instance name or MOPS_DBINSTANCE") unless $instance_name;
die("Must specify data store or MOPS_DATASTORE") unless $datastore;

my @filelist;
if ( @ARGV ) {
	@filelist = @ARGV;
} else {
	@filelist = ('index');
}

my $server_url = "http://$datastore/ds/det/";
my $url;

my $ua = LWP::UserAgent->new;
my $res;

for my $filename (@filelist) {
	$url = $server_url . $instance_name . "/" . $filename;
	print "$url -> $filename ... ";
	$res = $ua->get($url, ':content_file' => $filename);

	if ($res->is_success) {
		print "Success\n";
	} else {
		print "Failed\n";
		print $res->status_line, "\n";
	}
}

__END__

=head1 NAME

detDSFetch - fetch a file from the data store.

=head1 SYNOPSIS

detDSFetch [OPTIONS] [DBFILE [DBFILE [...]]

=head1 OPTIONS

=over 4

=item --instance=INSTANCE

Instance name.  Either the --instance option or the MOPS_DBINSTANCE 
environment variable must be used.  If both are used, the command line
option has priority.

=item --datastore=HOSTNAME[:PORT]

Data store server name.  If the server is listening on a non-standard
port, the port may be specified also.  Either the --datastore option
or the MOPS_DATASTORE environment variable must be used.  If both
are used, the command line option has priority.

=back

=head1 ARGUMENTS

=over 4

=item DBFILE

Data store file name.  If no file names are specified, then the data store
index will be fetched instead. 

=back

=head1 DESCRIPTION

Retrieve a detections file or index from the data store.

=head1 ENVIRONMENT VARIABLES

=over 4

=item MOPS_DBINSTANCE

Simulation instance.

=item MOPS_DATASTORE

Hostname or hostname:port used to specify the data store.

=back

=head1 TO DO

=head1 BUGS

=cut
