#!/usr/bin/perl
#
# Copyright 1999 -- 2001, onShore Development Inc. <URL:http://www.onshore-devel.com/>
#
#
# This program is free software under the terms of the GNU General Public
# License (GPL). A copy of the GPL, "COPYING", should have been made
# available with this software.  If not, a copy may be obtained at 
# http://www.fsf.org/copyleft/gpl.html
#
# $Id: timesheet-load,v 1.9 2001/08/29 22:23:15 adam Exp $
# 

=head1 NAME

timesheet-load - load onShore TimeSheet data from disk into database

=cut

require '../etc/timesheet.conf';
use Getopt::Long;

my $verbose = 0;
my $backupdir = $Conf::BACKUPDIR;
my @load_prog = ('psql', '-q');
my @address = ();
my ($host, $port) = split(':', $Conf::DBADDR);
if ( length($host) > 0 ) {
  push @address, "-h", $host;
}
if ( length($port) > 0 ) {
  push @address, "-p", $port;
}
push @address, $Conf::SQLDB;

=head1 SYNOPSIS

 B<timesheet-load> [ -d I<directory> ] [ -v ]

=head1 DESCRIPTION

B<timesheet-load> extracts data from the onShore TimeSheet database
instance and dumps it onto disk.  This is primarily useful for backing
up data.  The program can be run on a nightly basis.

The data is dumped onto disk using one file per table.

=head1 OPTIONS

=over 4

=item B<-d> I<directory>

Specify the directory where the dumped tables may be found.  By
default, the C<BACKUPDIR> setting from the global configuration file
is used.

=back

=cut

# switch handling
if ( ! GetOptions( "d=s" => \$backupdir,
		   "v" => \$verbose  ) ) {
    die("Usage:\n  $0 [ -d directory ] [ -v ]\n");
}

# sanity
if ( ! -d $backupdir ) {
    die "$0: data storage dir '$backupdir' does not exist\n";
}

my $table;
my @args;

for $table ($Conf::USER_DB, $Conf::CLIENT_DB, $Conf::JOB_DB, $Conf::HOURS_DB)
{
    if ( ! -f "$backupdir/$table" ) {
	warn("file $backupdir/$table doesn't exists, can't import\n");
    }
    else {
	verbose("loading $backupdir/$table");
	@args = (@load_prog, "-f", "$backupdir/$table",
		 @address);
	system(@args) == 0 or
	    die "$0: @args failed: $?\n";
    }
}

# verbose status message
sub verbose {
    print @_, "\n" if $verbose;
}

exit(0);

=head1 CONFIGURATION FILES

B<timesheet-load> reads options from F<'../etc/timesheet.conf'>.
Options read include the PostgreSQL connect string, table names,
default backup directory, etc.

=head1 BUGS

Should also have options to delete * from table or drop the tables.

=head1 SEE ALSO

timesheet-dump(8)

=head1 AUTHOR

Craig Brozefsky, Adam Di Carlo, for onShore, Inc.

=cut
