The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/perl -w
#========================================================================
#
# repo_export
#
# DESCRIPTION
#
#   Administrative tool for exporting sets of documents from a
#   document repository managed by Document::Repository.
#
# AUTHOR
#   Bryce W. Harrington <bryce at bryceharrington dot com>
#
# COPYRIGHT
#   Copyright (C) 2004 Bryce W. Harrington  
#   All Rights Reserved.
#
#   This module is free software; you can redistribute it and/or
#   modify it under the same terms as Perl itself.
#
#------------------------------------------------------------------------
#
# Last Modified:  $Date: 2005/06/11 05:36:59 $
#
# $Id: repo_export,v 1.1.1.1 2005/06/11 05:36:59 bryce Exp $
#
# $Log: repo_export,v $
# Revision 1.1.1.1  2005/06/11 05:36:59  bryce
# Initial import
#
# Revision 1.2  2005/02/06 07:06:14  bryce
# Address change
#
# Revision 1.1  2004/08/15 22:39:34  bryce
# Adding some new commands to init a repository, put revisions of existing
# documents, and export documents from the repository.
#
#
#========================================================================

use strict;                             # Forces variable decl's
use Carp;                               # Improved error/warning prints
use Pod::Usage;                         # To report program usage
use Getopt::Long;                       # Basic cmdline arg handling
use Document::Repository;

#------------------------------------------------------------------------
# Commandline option processing
#------------------------------------------------------------------------

our $opt_help            = 0;    # Prints a brief help message
our $opt_debug           = 0;    # Prints debug messages
our $opt_repository_dir  = '';   # Location of the repository
our $opt_destination     = '.';

Getopt::Long::Configure ("bundling", "no_ignore_case");  
GetOptions(
           "help|h",             # Prints a brief help message
           "debug|D=i",          # Prints debug messages
	   "repository_dir|R=s", # Location of the repository
	   "destination|d=s",    # Directory to put the files
            ) || pod2usage(1);

pod2usage(-verbose => 1, -exitstatus => 0) if $opt_help;

my $repo = new Document::Repository( repository_dir => $opt_repository_dir,
				     debug          => $opt_debug );

my $copy_f = undef;
my $select_f = undef;

foreach my $doc_id (@ARGV || sort { $a <=> $b } $repo->documents()) {
    warn "Retrieving files for doc id '$doc_id' to $opt_destination\n" if $opt_debug>2; 
    my @files = $repo->get($doc_id, undef, $opt_destination, $copy_f, $select_f);
    if (! @files ) {
	if ($repo->get_error()) {
	    warn $repo->get_error()."\n";
	}
    } else {
	print join("\n", @files), "\n";
    }
}


__END__

=head1 NAME

repo_export - Administrative tool to export files from documents stored
in the document repository.

If two or more documents contain files with the same name, the one with
the highest document id will overwrite the others.

=head1 SYNOPSIS

repo_export [options] [ doc_id [doc_id ...] ]

 Options:
   -h, --help                    Prints a brief help message
   -D, --debug=integer           Prints debug messages
   -R, --repository_dir          Location of the repository
   -d, --destination             Directory to put the files

=head1 DESCRIPTION

B<repo_export> - This exports documents from the document repository.
By default it returns all documents unless specific doc_id's are 
indicated.  Note that if two or more documents contain files with the
same name, the files for the document with the larger id number will
overwrite the smaller ones.

The user executing this script must have direct read/write/execute
permisison into the document repository.

=head1 AUTHOR

Bryce W. Harrington E<lt>bryce at bryceharrington dot comE<gt>

L<http://www.bryceharrington.org>

=head1 COPYRIGHT

Copyright (C) 2004 Bryce W. Harrington.
All Rights Reserved.

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

=head1 REVISION

Revision: $Revision: 1.1.1.1 $

=cut