#! /usr/bin/env perl

# $Id: detDSSubmit 165 2007-03-08 02:08:02Z yamada $

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

use warnings;
use strict;
use Getopt::Long;
use Pod::Usage;

use PS::MOPS::DC::Instance;
use PS::MOPS::DET::htclient;

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

my $inst = PS::MOPS::DC::Instance->new(DBNAME => $instance_name);
my $mops_config = $inst->getConfig;
$datastore ||= $mops_config->{dtctl}->{datastore};

die("Must specify instance name or MOPS_DBINSTANCE") unless $instance_name;
die("Must specify data store or MOPS_DATASTORE") unless $datastore;

if ( $#ARGV < 0 ) {
	pod2usage(2);
	exit(1);
}

my $server_url = "http://$datastore/";

for my $filename (@ARGV) {
	PS::MOPS::DET::htclient::file_submit($server_url, $instance_name,
		$filename);
}

__END__

=head1 NAME

detDSSubmit - post a file to the data store

=head1 SYNOPSIS

detDSSubmit [OPTIONS] LOCALFILE [LOCALFILE [...]]

=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 LOCALFILE

File to be submitted.

=back

=head1 DESCRIPTION

Submit a file to the data store as part of the specified simulation
instance.  If the instance does not already exist, it is created.
The file is created with an automatically incremented filename.

=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
