#!/usr/bin/perl -w

###########################################################################
#
# import.pl --
# A component of the Greenstone digital library software
# from the New Zealand Digital Library Project at the 
# University of Waikato, New Zealand.
#
# Copyright (C) 1999 New Zealand Digital Library Project
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
###########################################################################


# This program will import a number of files into a particular collection

BEGIN {
    die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
    die "GSDLOS not set\n" unless defined $ENV{'GSDLOS'};
    unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
    unshift (@INC, "$ENV{'GSDLHOME'}/perllib/plugins");
    unshift (@INC, "$ENV{'GSDLHOME'}/perllib/classify");
}

use strict;
use arcinfo;
use colcfg;
use plugin;
use docprint;
use util;
use parsargv;

sub print_usage {
    print STDERR "\n  usage: $0 [options] collection-name\n\n";
    print STDERR "  options:\n";
    print STDERR "   -verbosity number      0=none, 3=lots\n";
    print STDERR "   -importdir directory   Where the original material lives\n";
    print STDERR "   -archivedir directory  Where the converted material ends up\n";
    print STDERR "   -keepold               Will not destroy the current contents of the\n";
    print STDERR "                          archives directory (the default)\n";
    print STDERR "   -removeold             Will remove the old contents of the archives\n";
    print STDERR "                          directory -- use with care\n";
    print STDERR "   -gzip                  Use gzip to compress resulting gml documents\n";
    print STDERR "                          (don't forget to include ZIPPlug in your plugin\n";
    print STDERR "                          list when building from compressed documents)\n";
    print STDERR "   -maxdocs number        Maximum number of documents to import\n";
    print STDERR "   -groupsize number      Number of GML documents to group into one file\n";
    print STDERR "   -debug                 Print imported text to STDOUT\n\n";
}


&main ();

sub main {
    my ($verbosity, $importdir, $archivedir, $keepold, 
	$removeold, $gzip, $groupsize, $debug, $maxdocs, $collection,
	$configfilename, $collectcfg, $pluginfo, 
	$archive_info_filename, $archive_info, $processor);
    if (!parsargv::parse(\@ARGV, 
			 'verbosity/\d+/2', \$verbosity,
			 'importdir/.*/', \$importdir,
			 'archivedir/.*/', \$archivedir,
			 'keepold', \$keepold,
			 'removeold', \$removeold,
			 'gzip', \$gzip,
			 'groupsize/\d+/1', \$groupsize,
			 'debug', \$debug,
			 'maxdocs/^\-?\d+/-1', \$maxdocs)) {
	&print_usage();
	die "\n";
    }

    # set removeold to false if it has been defined
    $removeold = 0 if ($keepold);

    # get and check the collection name
    if (($collection = &util::use_collection(@ARGV)) eq "") {
	&print_usage();
	die "\n";
    }

    # dynamically load 'docsave' module so it can pick up on a collection
    # specific docsave.pm is specified.

    unshift (@INC, "$ENV{'GSDLCOLLECTDIR'}/perllib");
    require docsave;


    # get the list of plugins for this collection
    my $plugins = [];
    $configfilename = &util::filename_cat ($ENV{'GSDLCOLLECTDIR'}, "etc/collect.cfg");
    if (-e $configfilename) {
	$collectcfg = &colcfg::read_collect_cfg ($configfilename);
	if (defined $collectcfg->{'plugin'}) {
	    $plugins = $collectcfg->{'plugin'};
	}
	if (defined $collectcfg->{'importdir'} && $importdir eq "") {
	    $importdir = $collectcfg->{'importdir'};
	}
	if (defined $collectcfg->{'archivedir'} && $archivedir eq "") {
	    $archivedir = $collectcfg->{'archivedir'};
	}
	if (defined $collectcfg->{'removeold'}) {
	    if ($collectcfg->{'removeold'} =~ /^true$/i && !$keepold) {
		$removeold = 1;
	    }
	    if ($collectcfg->{'removeold'} =~ /^false$/i && !$removeold) {
		$removeold = 0;
	    }
	}
    } else {
	die "Couldn't find the configuration file $configfilename\n";
    }
    
    # fill in the default import and archives directories if none
    # were supplied, turn all \ into / and remove trailing /
    $importdir = "$ENV{'GSDLCOLLECTDIR'}/import" if $importdir eq "";
    $importdir =~ s/[\\\/]+/\//g;
    $importdir =~ s/\/$//;
    $archivedir = "$ENV{'GSDLCOLLECTDIR'}/archives" if $archivedir eq "";
    $archivedir =~ s/[\\\/]+/\//g;
    $archivedir =~ s/\/$//;

    # load all the plugins
    $pluginfo = &plugin::load_plugins ($plugins, $verbosity);
    if (scalar(@$pluginfo) == 0) {
	print STDERR "No plugins were loaded.\n";
	die "\n";
    }
	
    # remove the old contents of the archives directory if needed
    if ($removeold && -e $archivedir) {
	print STDERR "Warning - removing current contents of the archives directory\n";
	print STDERR "          in preparation for the import\n";
	sleep(5); # just in case...
	&util::rm_r ($archivedir);
    }
    
    # read the archive information file
    if (!$debug) {
	$archive_info_filename = &util::filename_cat ($archivedir, "archives.inf");
	$archive_info = new arcinfo ();
	$archive_info->load_info ($archive_info_filename);

	# create a docsave object to process the documents
	$processor = new docsave ($collection, $archive_info, $verbosity, $gzip, $groupsize);
	$processor->setarchivedir ($archivedir);
    } else {
	$processor = new docprint ();
    }

    &plugin::begin($pluginfo, $importdir, $processor, $maxdocs);

    # process the import directory
    &plugin::read ($pluginfo, $importdir, "", {}, $processor, $maxdocs);
    
    &plugin::end($pluginfo);
    
    # write out the archive information file
    if (!$debug) {
	$processor->close_file_output();
	$archive_info->save_info($archive_info_filename);
    }
}





