#!/usr/bin/perl -w

BEGIN 
{
    if (!defined $ENV{'GSDLHOME'}) {
	print STDERR "Environment variable GSDLHOME not set.\n";
	print STDERR "  Have you sourced Greenstone's 'setup.bash' file?\n";
	exit 1;
    }

    if (!defined $ENV{'JAVA_HOME'}) {
	print STDERR "Environment variable JAVA_HOME not set.\n";
	print STDERR "Needed by Fedora command line scripts.\n";
	exit 1;
    }

    $ENV{'FEDORA_HOSTNAME'} = "localhost" if (!defined $ENV{'FEDORA_HOSTNAME'});
    $ENV{'FEDORA_SERVER_PORT'} = "8080" if (!defined $ENV{'FEDORA_SERVER_PORT'});
    $ENV{'FEDORA_USER'}     = "fedoraAdmin" if (!defined $ENV{'FEDORA_USER'});
    $ENV{'FEDORA_PASS'}     = "fedoraAdmin" if (!defined $ENV{'FEDORA_PASS'});
    $ENV{'FEDORA_PROTOCOL'} = "http" if (!defined $ENV{'FEDORA_PROTOCOL'});
    $ENV{'FEDORA_PID_NAMESPACE'} = "greenstone" if (!defined $ENV{'FEDORA_PID_NAMESPACE'});

    unshift (@INC, "$ENV{'GSDLHOME'}/perllib/");

}


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

use util;
use gsprintf 'gsprintf';
use printusage;
use parse2;

use g2futil;


my $arguments = 
    [ 
      { 'name' => "verbosity",
	'desc' => "Level of verbosity generated",
	'type' => "string",
	'deft' => '1',
	'reqd' => "no",
        'hiddengli' => "no" },
      { 'name' => "hostname",
	'desc' => "Domain hostname of Fedora server",
	'type' => "string",
	'deft' => $ENV{'FEDORA_HOSTNAME'},
	'reqd' => "no",
        'hiddengli' => "no" },
      { 'name' => "port",
	'desc' => "Port that the Fedora server is running on.",
	'type' => "string",
	'deft' => $ENV{'FEDORA_SERVER_PORT'},
	'reqd' => "no",
        'hiddengli' => "no" },
      { 'name' => "username",
	'desc' => "Fedora admin username",
	'type' => "string",
	'deft' => $ENV{'FEDORA_USER'},
	'reqd' => "no",
        'hiddengli' => "no" },
      { 'name' => "password",
	'desc' => "Fedora admin password",
	'type' => "string",
	'deft' => $ENV{'FEDORA_PASS'},
	'reqd' => "no",
        'hiddengli' => "no" },
      { 'name' => "protocol",
	'desc' => "Fedora protocol, e.g. 'http' or 'https'",
	'type' => "string",
	'deft' => $ENV{'FEDORA_PROTOCOL'},
	'reqd' => "no",
        'hiddengli' => "no" },
      { 'name' => "pidnamespace",
	'desc' => "Fedora prefix for PIDs",
	'type' => "string",
	'deft' => $ENV{'FEDORA_PID_NAMESPACE'},
	'reqd' => "no",
        'hiddengli' => "no" },
      { 'name' => "gli",
	'desc' => "",
	'type' => "flag",
	'reqd' => "no",
	'hiddengli' => "yes" },
      { 'name' => "xml",
	'desc' => "{scripts.xml}",
	'type' => "flag",
	'reqd' => "no",
	'hiddengli' => "yes" },
      { 'name' => "removeold",
	'desc' => "{import.removeold}",
	'type' => "flag",
	'reqd' => "no",
	'modegli' => "3" },
      { 'name' => "language",
	'desc' => "{scripts.language}",
	'type' => "string",
	'reqd' => "no",
	'modegli' => "4" },
      { 'name' => "collectdir",
	'desc' => "{import.collectdir}",
	'type' => "string",
	'deft' => "",
	'reqd' => "no",
	'hiddengli' => "yes" }
      ];

my $prog_options 
    = { 'name' => "g2fbuildcol.pl",
	'desc' => "Ingest Greenstone directory of FedoraMETS documents into Fedora",
	'args' => $arguments };


sub main
{
    my (@ARGV) = @_;

    my $GSDLHOME = $ENV{'GSDLHOME'};


    my $options = {};
    # general options available to all plugins
    my $intArgLeftinAfterParsing = parse2::parse(\@ARGV,$arguments,$options,"allow_extra_options");

    # Something went wrong with parsing
    if ($intArgLeftinAfterParsing ==-1)
    {
	&PrintUsage::print_txt_usage($prog_options, "[options] greenstone-col");
	die "\n";
    }

    my $xml = $options->{'xml'};
    my $gli = $options->{'gli'};

    if ($intArgLeftinAfterParsing != 1)
    {
	if ($xml) {
	    &PrintUsage::print_xml_usage($prog_options);
	    print "\n";
	    return;
	}
	else {
	    &PrintUsage::print_txt_usage($prog_options, "[options] greenstone-col");
	    print "\n";
	    return;
	}
	    
    }

    my $gs_col = $ARGV[0];

    my $verbosity = $options->{'verbosity'};
    my $hostname  = $options->{'hostname'};
    my $port      = $options->{'port'};
    my $username  = $options->{'username'};
    my $password  = $options->{'password'};
    my $protocol  = $options->{'protocol'};
    my $pid_namespace = $options->{'pidnamespace'};

    # The following are needed in the FedoraMETS plugout
    $ENV{'FEDORA_HOSTNAME'} = $hostname;
    $ENV{'FEDORA_SERVER_PORT'} = $port;

    my $collectdir = $options->{'collectdir'};

    if (!$collectdir) {
	##    my $collect_dir = util::filename_cat($ENV{'GREENSTONE3_HOME'},
	##					 "web","sites","localsite","collect");


	$collectdir = util::filename_cat($ENV{'GSDLHOME'},"collect");
    }

    my $full_gs_col = util::filename_cat($collectdir,$gs_col);


    if (!-e $full_gs_col ) {
	print STDERR "Unable to find Greenstone collection $full_gs_col\n";
	exit 1;
    }

##    my $archives_dir = &util::filename_cat($full_gs_col,"archives");
    my $export_dir = &util::filename_cat($full_gs_col,"export");


    print "***\n";
    print "* Ingesting Greenstone processed files into Fedora $pid_namespace\n";
    print "***\n";

    # Following falls foul of Schematron rule checking
    my $fd_add_prog = "fedora-ingest";
#    my $fd_add_cmd;
#    $fd_add_args  = "dir $export_dir O metslikefedora1 $hostname:$port $username $password \\\n";
#    $fd_add_args .= "  \"Automated_ingest_by_gs2fed.pl\"";

#    &g2futil::run_cmd($fd_add_prog,$fd_add_args,$options);


    # => Ingest individually!

    if (opendir(DIR, $export_dir)) {
	closedir DIR;
	## my @hash_dirs = grep { /\.dir$/ } readdir(DIR);
	my @hash_dirs = &g2futil::get_all_hash_dirs($export_dir);


	# for each hash dir, purge its respective PID
	foreach my $hd (@hash_dirs) {

	    my $docmets_filename 
		= &util::filename_cat($hd,"docmets.xml");

	    print STDERR "<Build>\n" if $gli;

	    print "Ingesting $docmets_filename\n";

	    &g2futil::run_ingest($docmets_filename,$options);
	    print STDERR "</Build>\n" if $gli;

	}	    
    }
    else {
	print STDERR "Error: Unable to open directory $export_dir: $!\n";
	exit;
    }


}

&main(@ARGV);



