#! /usr/bin/env perl

# $Id: detFetchFromDS 145 2007-03-07 02:02:44Z 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 $instance_name = $ENV{'MOPS_DBINSTANCE'};
my $mops_home = $ENV{'MOPS_HOME'};
my $var = $ENV{'MOPS_VAR'};
my $datastore_url;

GetOptions(
	'instance=s' => \$instance_name,
	'datastore_url=s' => \$datastore_url,
	'mops_home=s' => \$mops_home,
	'var=s' => \$var,
    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_url ||= $mops_config->{datastore}->{root_url};

pod2usage(2) unless $#ARGV == -1;
die("Must specify instance name or MOPS_DBINSTANCE") unless $instance_name;
die("Must specify data store or MOPS_DATASTORE") unless $datastore_url;
die("Must specify mops home or MOPS_HOME") unless $mops_home;
$var = $mops_home . "/var/" . $instance_name unless $var;

PS::MOPS::DET::htclient::clean($datastore_url, $instance_name, $var);

__END__

=head1 NAME

detDSClean - Search the data store for new files, insert.

=head1 SYNOPSIS

detFindFetchAndInstall [OPTIONS]

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

=item --mops_home=MOPSHOME

Root of the MOPS hierarchy.  Either the --mops_home option or the
MOPS_HOME environment variable must be used.  If both are used, the
command line option has priority.

=item --mops_var=MOPSVAR

Where are temporary files created.  The directory must already
exist.  If this variable is not set, then the MOPS_VAR environment
variable is use.  If neither is set, then MOPSHOME/var/INSTANCE is
used.

=back

=head1 DESCRIPTION

Check the datastore for new files, retrieve any new files, then
insert them into the simulation database.

=head1 ENVIRONMENT VARIABLES

=over 4

=item MOPS_DBINSTANCE

Simulation instance.

=item MOPS_DATASTORE

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

=item MOPS_HOME

Root of the MOPS directory hierachy.

=back

=head1 TO DO

=head1 BUGS

=cut
