#!/usr/bin/perl -w

###########################################################################
#
# export.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) 2004 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 export a particular collection into a specific Format (e.g. METS or DSpace) by importing then saving as a different format.

package export;

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/cpan");
#    unshift (@INC, "$ENV{'GSDLHOME'}/perllib/cpan/perl-5.8");
    unshift (@INC, "$ENV{'GSDLHOME'}/perllib/plugins");
    unshift (@INC, "$ENV{'GSDLHOME'}/perllib/plugouts");

    if (defined $ENV{'GSDLEXTS'}) {
	my @extensions = split(/:/,$ENV{'GSDLEXTS'});
	foreach my $e (@extensions) {
	    my $ext_prefix = "$ENV{'GSDLHOME'}/ext/$e";

	    unshift (@INC, "$ext_prefix/perllib");
	    unshift (@INC, "$ext_prefix/perllib/cpan");
	    unshift (@INC, "$ext_prefix/perllib/plugins");
	    unshift (@INC, "$ext_prefix/perllib/plugouts");
	}
    }
    if (defined $ENV{'GSDL3EXTS'}) {
	my @extensions = split(/:/,$ENV{'GSDL3EXTS'});
	foreach my $e (@extensions) {
	    my $ext_prefix = "$ENV{'GSDL3SRCHOME'}/ext/$e";

	    unshift (@INC, "$ext_prefix/perllib");
	    unshift (@INC, "$ext_prefix/perllib/cpan");
	    unshift (@INC, "$ext_prefix/perllib/plugins");
	    unshift (@INC, "$ext_prefix/perllib/plugouts");
	}
    }

}

use strict;
#no strict 'refs'; # allow filehandles to be variables and vice versa
#no strict 'subs'; # allow barewords (eg STDERR) as function arguments
use inexport;

my $oidtype_list = 
    [ { 'name' => "hash",
        'desc' => "{import.OIDtype.hash}" },
      { 'name' => "assigned",
        'desc' => "{import.OIDtype.assigned}" },
      { 'name' => "incremental",
        'desc' => "{import.OIDtype.incremental}" },
      { 'name' => "dirname",
        'desc' => "{import.OIDtype.dirname}" } ];

# what format to export as
my $saveas_list = 
    [ { 'name' => "GreenstoneMETS",
        'desc' => "{export.saveas.GreenstoneMETS}"},
      { 'name' => "FedoraMETS",
        'desc' => "{export.saveas.FedoraMETS}"},
      { 'name' => "MARCXML",
        'desc' => "{export.saveas.MARCXML}"},
      { 'name' => "DSpace",
        'desc' => "{export.saveas.DSpace}" }
     ];


# Possible attributes for each argument
# name: The name of the argument
# desc: A description (or more likely a reference to a description) for this argument
# type: The type of control used to represent the argument. Options include: string, int, flag, regexp, metadata, language, enum etc
# reqd: Is this argument required?
# hiddengli: Is this argument hidden in GLI?
# modegli: The lowest detail mode this argument is visible at in GLI

my $saveas_argument =
      { 'name' => "saveas",
	'desc' => "{export.saveas}",
	'type' => "enum",
	'list' => $saveas_list,
	'deft' => "GreenstoneMETS",
	'reqd' => "no",
	'modegli' => "3" };


my $arguments = 
    [ 
      $saveas_argument,
      { 'name' => "exportdir",
	'desc' => "{export.exportdir}",
	'type' => "string",
	'reqd' => "no",
        'hiddengli' => "yes" },
      { 'name' => "importdir",
	'desc' => "{import.importdir}",
	'type' => "string",
	'reqd' => "no",
        'hiddengli' => "yes" },
      { 'name' => "collectdir",
	'desc' => "{export.collectdir}",
	'type' => "string",
	# parsearg left "" as default
	#'deft' => &util::filename_cat ($ENV{'GSDLHOME'}, "collect"),
	'deft' => "",
	'reqd' => "no",
	'hiddengli' => "yes" },
      { 'name' => "site",
	'desc' => "{import.site}",
	'type' => "string",
	'deft' => "",
	'reqd' => "no",
        'hiddengli' => "yes" },
     { 'name' => "manifest",
	'desc' => "{import.manifest}",
	'type' => "string",
	'deft' => "",
	'reqd' => "no",
        'hiddengli' => "yes" },
      { 'name' => "debug",
	'desc' => "{export.debug}",
	'type' => "flag",
	'reqd' => "no",
        'hiddengli' => "yes" },
      { 'name' => "faillog",
	'desc' => "{export.faillog}",
	'type' => "string",
	'deft' => "",
	'reqd' => "no",
        'modegli' => "3" },
      # does this make sense?
      { 'name' => "incremental",
	'desc' => "{import.incremental}",
	'type' => "flag",
	'hiddengli' => "yes" },
      { 'name' => "keepold",
	'desc' => "{export.keepold}",
	'type' => "flag",
	'reqd' => "no",
	'hiddengli' => "yes" },
      { 'name' => "removeold",
	'desc' => "{export.removeold}",
	'type' => "flag",
	'reqd' => "no",
	'hiddengli' => "yes" },
      { 'name' => "language",
	'desc' => "{scripts.language}",
	'type' => "string",
	'reqd' => "no",
	'hiddengli' => "yes" },
      { 'name' => "maxdocs",
	'desc' => "{export.maxdocs}",
	'type' => "int",
	'reqd' => "no",
	'range' => "1,",
	'modegli' => "1" },
      { 'name' => "OIDtype",
	'desc' => "{import.OIDtype}",
	'type' => "enum",
	'list' => $oidtype_list,
	# parsearg left "" as default
	#'deft' => "hash",
	'reqd' => "no",
	'modegli' => "2" },
      { 'name' => "OIDmetadata",
	'desc' => "{import.OIDmetadata}",
	'type' => "string",
	#'type' => "metadata", #doesn't work properly in GLI
	'deft' => "dc.Identifier",
	'reqd' => "no",
	'modegli' => "2" },
      { 'name' => "out",
	'desc' => "{export.out}",
	'type' => "string",
	'deft' => "STDERR",
	'reqd' => "no",
        'hiddengli' => "yes" },
      { 'name' => "statsfile",
	'desc' => "{export.statsfile}",
	'type' => "string",
	'deft' => "STDERR",
	'reqd' => "no",
        'hiddengli' => "yes" },
      { 'name' => "xsltfile",
	'desc' => "{BasPlugout.xslt_file}",
	'type' => "string",
	'reqd' => "no",
        'hiddengli' => "yes" },
      { 'name' => "xslt_txt",
	'desc' => "{METSPlugout.xslt_txt}",
	'type' => "string",
	'reqd' => "no",
        'hiddengli' => "no" },
      { 'name' => "xslt_mets",
	'desc' => "{METSPlugout.xslt_mets}",
	'type' => "string",
	'reqd' => "no",
        'hiddengli' => "no" },
      { 'name' => "fedora_namespace",
	'desc' => "{FedoraMETSPlugout.fedora_namespace} (-saveas FedoraMETS)",
	'type' => "string",
	'deft' => "greenstone",
	'reqd' => "no",
        'hiddengli' => "no" },
      { 'name' => "mapping_file",
	'desc' => "{MARCXMLPlugout.mapping_file} (-saveas MARCXML)",
	'type' => "string",
	'reqd' => "no",
        'hiddengli' => "no" },
      { 'name' => "group_marc",
	'desc' => "{MARCXMLPlugout.group} (-saveas MARCXML)",
	'type' => "flag",
	'reqd' => "no",
        'hiddengli' => "no" },
      { 'name' => "metadata_prefix",
	'desc' => "{DSpacePlugout.metadata_prefix} (-saveas DSpace)}",
	'type' => "string",
	'reqd' => "no",
        'hiddengli' => "no" },
      { 'name' => "verbosity",
	'desc' => "{export.verbosity}",
	'type' => "int",
	'range' => "0,3",
	'deft' => "2",
	'reqd' => "no",
	'modegli' => "3" },
      { 'name' => "gli",
	'desc' => "{scripts.gli}",
	'type' => "flag",
	'reqd' => "no",
	'hiddengli' => "yes" },
      { 'name' => "listall",
	'desc' => "{export.listall}",
	'type' => "flag",
	'reqd' => "no" },
      { 'name' => "xml",
	'desc' => "{scripts.xml}",
	'type' => "flag",
	'reqd' => "no",
	'hiddengli' => "yes" }
      ];

my $options = { 'name' => "export.pl",
		'desc' => "{export.desc}",
		'args' => $arguments };

my $listall_options = { 'name' => "export.pl",
		        'desc' => "{export.desc}",
		        'args' => [ $saveas_argument ] };



sub main 
{
    my $inexport = new inexport("export",\@ARGV,$options,$listall_options);
    
    my $collection = $inexport->get_collection();

    if (defined $collection) {
	my ($config_filename,$collect_cfg) 
	    = $inexport->read_collection_cfg($collection,$options);    

	$inexport->set_collection_options($collect_cfg);
    
	my $pluginfo = $inexport->process_files($config_filename,$collect_cfg);
	
	$inexport->generate_statistics($pluginfo);
    }
}


&main();


