###########################################################################
#
# GAPlugout.pm -- the plugout module for Greenstone 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 GAPlugout;

use strict;
no strict 'refs';
no strict 'subs';

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

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

my $arguments = [];

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

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 $outhandler;
    if ($self->{'debug'}) {
	$outhandler = STDOUT;
	# can we do the xslt and still do debug mode?
    } 
    else {
	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;
	
	$self->process_assoc_files ($doc_obj, $doc_dir, '');
	
	my $output_file = util::filename_cat ($working_dir, "doc.xml");
	
	$self->open_xslt_pipe($output_file, $self->{'xslt_file'});
	
	
	if (defined $self->{'xslt_writer'}){
	    $outhandler = $self->{'xslt_writer'};
	}
	else{
	    $outhandler = $self->get_output_handler($output_file);
	}
    }
    
    $self->output_xml_header($outhandler,"Archive");
    my $section_output = &docprint::get_section_xml($doc_obj, $doc_obj->get_top_section());
    print $outhandler $section_output;
    $self->output_xml_footer($outhandler,"Archive");

    if (!$self->{'debug'}) {
	if (defined $self->{'xslt_writer'}){     
	    $self->close_xslt_pipe(); 
	}
	else {
	    close($outhandler);
	}
	
	$self->{'short_doc_file'} = util::filename_cat ($doc_dir, "doc.xml");  
	
	$self->store_output_info_reference($doc_obj);
    }
}



1;

