###########################################################################
#
# METSPlugout.pm -- the plugout module for METS archives
# A component of the Greenstone digital library software
# from the New Zealand Digital Library Project at the 
# University of Waikato, New Zealand.
#
# Copyright (C) 2006 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.
#
###########################################################################

package METSPlugout;

use strict;
no strict 'refs';

eval {require bytes};
use util;
use BasPlugout;

sub BEGIN {
    @METSPlugout::ISA = ('BasPlugout');
}

my $arguments = [
      { 'name' => "saveas_version", 
	'desc' => "{METSPlugout.version}",
	'type' => "string",
        'deft' => 'greenstone',
	'reqd' => "yes",    
	'hiddengli' => "no"},
      { '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"}
      ];

my $options = { 'name'     => "METSPlugout",
		'desc'     => "{METSPlugout.desc}",
		'abstract' => "no",
		'inherits' => "yes", 
	        'args'     => $arguments
                };

sub new {
    my ($class) = shift (@_);
    my ($plugoutlist, $inputargs,$hashArgOptLists) = @_;
    push(@$plugoutlist, $class);

      
    if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
    if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};

    my $self = (defined $hashArgOptLists)? new BasPlugout($plugoutlist,$inputargs,$hashArgOptLists): new BasPlugout($plugoutlist,$inputargs); 

 
   
   return bless $self, $class;
}

sub saveas {
    my $self = shift (@_);
    my ($doc_obj,$doc_dir) = @_;
    my $version = $self->{'saveas_version'};

    $self->process_assoc_files ($doc_obj, $doc_dir, '');

    my $output_dir = $self->get_output_dir();
    &util::mk_all_dir ($output_dir) unless -e $output_dir;
 
    my $working_dir = &util::filename_cat ($output_dir, $doc_dir);              
   
    &util::mk_all_dir ($working_dir) unless -e $working_dir;

    #########################
    # save the text file
    #########################
    my $doc_txt_file = &util::filename_cat ($working_dir,"doctxt.xml");	
	
    $self->open_xslt_pipe($doc_txt_file,$self->{'xslt_txt'});

    my $outhandler; 

    if (defined $self->{'xslt_writer'}){
	$outhandler = $self->{'xslt_writer'};
    }
    else{
	$outhandler = $self->get_output_handler($doc_txt_file);
    } 

    $self->output_xml_header($outhandler);
    $doc_obj->output_txt_section($outhandler,$doc_obj->get_top_section());
    $self->output_xml_footer($outhandler);
    

    if (defined $self->{'xslt_writer'}){     
	$self->close_xslt_pipe(); 
    }
    else{
	close($outhandler);
    }
    
    #########################
    # save the mets file
    #########################
    my $doc_mets_file = &util::filename_cat ($working_dir, "docmets.xml");
    
    my $doc_title = $doc_obj->get_metadata_element($doc_obj->get_top_section(),"dc.Title");
    if (!defined $doc_title) {
	$doc_title = $doc_obj->get_metadata_element($doc_obj->get_top_section(),"Title");
    }
 
    $self->open_xslt_pipe($doc_mets_file,$self->{'xslt_mets'});

    if (defined $self->{'xslt_writer'}){
       $outhandler = $self->{'xslt_writer'};
    }
    else{
       $outhandler = $self->get_output_handler($doc_mets_file);
     }   
 
      
    $self->output_mets_xml_header($outhandler, $doc_obj->get_OID(), $doc_title);
    $doc_obj->output_mets_section($outhandler,$doc_obj->get_top_section(),$version,$working_dir);
    $self->output_mets_xml_footer($outhandler);
	
     if (defined $self->{'xslt_writer'}){     
	$self->close_xslt_pipe(); 
    }
    else{
	close($outhandler);
    }
     
    $self->{'short_doc_file'} =  &util::filename_cat ($doc_dir, "docmets.xml");
  
    $self->store_output_info_reference($doc_obj);
      
}


sub output_mets_xml_header(){
    my $self = shift(@_);
    my ($handle, $OID, $doc_title) = @_;

    my $version = $self->{'saveas_version'};

    my $extra_attr = "";
    if ($version eq "fedora") {	
	my $fnamespace = $ENV{'FEDORA_PID_NAMESPACE'};
	my $oid_namespace = (defined $fnamespace) ? $fnamespace : "test";

	$extra_attr = "OBJID=\"$oid_namespace:$OID\" TYPE=\"FedoraObject\" LABEL=\"$doc_title\"";
    }
    else {
	# Greenstone METS profile
	$extra_attr = "OBJID=\"$OID:2\"";
    }


    print $handle '<?xml version="1.0" encoding="UTF-8" standalone="no"?>' . "\n";
    print $handle '<mets:mets xmlns:mets="http://www.loc.gov/METS/"' . "\n";
    print $handle '           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"' . "\n";
    print $handle '           xmlns:gsdl3="http://www.greenstone.org/namespace/gsdlmetadata/1.0/"' . "\n";
    print $handle '           xmlns:xlink="http://www.w3.org/TR/xlink"' ."\n";
    print $handle '           xsi:schemaLocation="http://www.loc.gov/METS/' . "\n";
    print $handle '           http://www.loc.gov/standards/mets/mets.xsd' . "\n";
    print $handle '           http://www.greenstone.org/namespace/gsdlmetadata/1.0/' . "\n";
    print $handle '           http://www.greenstone.org/namespace/gsdlmetadata/1.0/gsdl_metadata.xsd"' . "\n";
    print $handle "  $extra_attr>\n";

    if ($version eq "fedora") {
	print $handle '<mets:metsHdr RECORDSTATUS="A"/>'. "\n"; # A = active
    }

}

sub output_mets_xml_footer() {
    my $self = shift(@_);
    my ($handle) = @_;
    print $handle '</mets:mets>' . "\n";
}

1;
