###########################################################################
#
# docsave.pm
# 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 document processor saves a document in the 
# archives directory of a collection


package docsave;

use arcinfo;
use docproc;
use util;


sub BEGIN {
    @ISA = ('docproc');
}

sub new {
    my ($class, $collection, $archive_info, $verbosity, $gzip) = @_;
    my $self = new docproc ();

    $self->{'collection'} = $collection;
    $self->{'archive_info'} = $archive_info;
    $self->{'verbosity'} = $verbosity;
    $self->{'gzip'} = $gzip;

    # set a default for the archive directory
    $self->{'archive_dir'} = "$ENV{'GSDLHOME'}/collect/$self->{'collection'}/archives";

    return bless $self, $class;
}

sub setarchivedir {
    my $self = shift (@_);
    my ($archive_dir) = @_;

    $self->{'archive_dir'} = $archive_dir;
}

sub process {
    my $self = shift (@_);
    my ($doc_obj) = @_;

    my $archive_dir = $self->{'archive_dir'};
    my $OID = $doc_obj->get_OID();
    $OID = "NULL" unless defined $OID;

    # get the document's directory.
    my $doc_info = $self->{'archive_info'}->get_info($OID);
    my $doc_dir = "";
    if (defined $doc_info && scalar(@$doc_info) >= 1) {
	# this OID already has an assigned directory, use the
	# same one.
	$doc_dir = $doc_info->[0];
	$doc_dir =~ s/\/?doc\.gml(\.gz)?$//;

    } else {
	# have to get a new document directory
	my $doc_dir_rest = $OID;
	my $doc_dir_num = 0;
	do {
	    $doc_dir .= "/" if $doc_dir_num > 0;
	    if ($doc_dir_rest =~ s/^(.{1,8})//) {
		$doc_dir .= $1;
		$doc_dir_num++;
	    }
	} while ($doc_dir_rest ne "" && 
		 ((-d &util::filename_cat ($archive_dir, "$doc_dir.dir")) || 
		  ($self->{'archive_info'}->size() >= 1024 && $doc_dir_num < 2)));
	$doc_dir .= ".dir";
    }

    &util::mk_all_dir ("$archive_dir/$doc_dir");

    # copy all the associated files, add this information as metadata
    # to the document
    my @assoc_files = ();
    foreach $assoc_file (@{$doc_obj->get_assoc_files()}) {
	if (-e $assoc_file->[0]) {
	    my $afile = &util::filename_cat($archive_dir, $doc_dir, $assoc_file->[1]);
	    &util::cp ($assoc_file->[0], $afile);
	    $doc_obj->add_metadata ($doc_obj->get_top_section(), 
				    "gsdlassocfile", 
				    "$assoc_file->[1]:$assoc_file->[2]");
	} else {
	    print STDERR "docsave::process couldn't copy the associated file " .
		"$assoc_file->[0] to $afile\n";
	}
    }

    # save this document
    my $doc_file = &util::filename_cat ($archive_dir, $doc_dir, "doc.gml");
    my $short_doc_file = &util::filename_cat ($doc_dir, "doc.gml");

    if (!open (OUTDOC, ">$doc_file")) {
	print STDERR "docsave::process could not write to file $docfile\n";
	return;
    }
    $doc_obj->output_section('docsave::OUTDOC', $doc_obj->get_top_section());
    close OUTDOC;

    if ($self->{'gzip'}) {
	`gzip $doc_file`;
	$doc_file .= ".gz";
	$short_doc_file .= ".gz";
	if (!-e $doc_file) {
	    print STDERR "error while gzipping: $doc_file doesn't exist\n";
	    return;
	}
    }

    # store reference in the archive_info
    $self->{'archive_info'}->add_info($OID, $short_doc_file);
    
}


1;
