###########################################################################
#
# WordPlug.pm -- plugin for importing Microsoft Word documents
# 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.
#
###########################################################################
# 12/05/02 Added usage datastructure - John Thompson

package WordPlug;

use ConvertToPlug;
use strict;
no strict 'refs'; # allow filehandles to be variables and viceversa

sub BEGIN {
    @WordPlug::ISA = ('ConvertToPlug');
}

my $arguments =
    [ { 'name' => "process_exp",
	'desc' => "{BasPlug.process_exp}",
	'type' => "regexp",
	'deft' => &get_default_process_exp(),
	'reqd' => "no" }
      ];

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

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

    if ($ENV{'GSDLOS'} =~ m/^windows$/i) {
	my $ws_arg = [ { 'name' => "windows_scripting",
			 'desc' => "{WordPlug.windows_scripting}",
			 'type' => "flag",
		         'reqd' => "no" },
		       { 'name' => "delete_toc",
			 'desc' => "{WordPlug.delete_toc}",
			 'type' => "flag",
			 'reqd' => "no"},
		       { 'name' => "level1_header",
			 'desc' => "{WordPlug.level1_header}",
			 'type' => "regexp",
			 'reqd' => "no",
			 'deft' => "" },
		       { 'name' => "level2_header",
			 'desc' => "{WordPlug.level2_header}",
			 'type' => "regexp",
			 'reqd' => "no",
			 'deft' => "" },
		       { 'name' => "level3_header",
			 'desc' => "{WordPlug.level3_header}",
			 'type' => "regexp",
			 'reqd' => "no",
			 'deft' => "" },
		       { 'name' => "title_header",
			 'desc' => "{WordPlug.title_header}",
			 'type' => "regexp",
			 'reqd' => "no",
			 'deft' => "" },
		       { 'name' => "toc_header",
			 'desc' => "{WordPlug.toc_header}",
			 'type' => "regexp",
			 'reqd' => "no",
			 'deft' => "" },
		       { 'name' => "tof_header",
			 'desc' => "{WordPlug.tof_header}",
			 'type' => "regexp",
			 'reqd' => "no",
			 'deft' => "" },
		       { 'name' => "extracted_word_metadata_fields",
			 'type' => "string",
			 'deft' => "Title"}];
	
	push(@$arguments,@$ws_arg);
    }

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

    my $self = (defined $hashArgOptLists)? new ConvertToPlug($pluginlist,$inputargs,$hashArgOptLists): new ConvertToPlug($pluginlist,$inputargs);

    #this is passed through to gsConvert.pl by ConvertToPlug.pm
    $self->{'convert_options'} = "-windows_scripting" if $self->{'windows_scripting'};

    my $secondary_plugin_options = $self->{'secondary_plugin_options'};
    if (defined $self->{'windows_scripting'}) { 
	if (!defined $secondary_plugin_options->{'StructuredHTMLPlug'}){
	    $secondary_plugin_options->{'StructuredHTMLPlug'} = [];
	    my $structhtml_options = $secondary_plugin_options->{'StructuredHTMLPlug'};
	    if ($self->{'input_encoding'} eq "auto") {
		$self->{'input_encoding'} = "utf8";
		$self->{'extract_language'} = 1;
		push(@$structhtml_options,"-input_encoding", "utf8");
		push(@$structhtml_options,"-extract_language");
	    }
				
	    # Instruct HTMLPlug (when eventually accessed through read_into_doc_obj)
	    # to extract these metadata fields from the HEAD META fields
	    push (@$structhtml_options,"-metadata_fields","Title,GENERATOR,date,author<Creator>");
	    push (@$structhtml_options , "-title_sub", '^(Page\s+\d+)?(\s*1\s+)?');
	    push (@$structhtml_options, "-description_tags") if $self->{'windows_scripting'};
	    push (@$structhtml_options, "-delete_toc") if $self->{'delete_toc'};
	    push (@$structhtml_options, "-toc_header", $self->{'toc_header'}) if $self->{'toc_header'};
	    push (@$structhtml_options, "-tof_header", $self->{'tof_header'}) if $self->{'tof_header'};
	    push (@$structhtml_options, "-title_header", $self->{'title_header'}) if $self->{'title_header'};
	    push (@$structhtml_options, "-level1_header", $self->{'level1_header'}) if $self->{'level1_header'};
	    push (@$structhtml_options, "-level2_header", $self->{'level2_header'})if $self->{'level2_header'};
	    push (@$structhtml_options, "-level3_header", $self->{'level3_header'}) if $self->{'level3_header'};
	    push (@$structhtml_options, "-extracted_word_metadata_fields", $self->{'extracted_word_metadata_fields'}) if $self->{'extracted_word_metadata_fields'};
	}
    }
    if (!defined $secondary_plugin_options->{'HTMLPlug'}) {
	$secondary_plugin_options->{'HTMLPlug'} = [];
    }
    if (!defined $secondary_plugin_options->{'TEXTPlug'}) {
	$secondary_plugin_options->{'TEXTPlug'} = [];
    }

    my $html_options = $secondary_plugin_options->{'HTMLPlug'};
    my $text_options = $secondary_plugin_options->{'TextPlug'};
    
    # wvWare will always produce html files encoded as utf-8
    if ($self->{'input_encoding'} eq "auto") {
	$self->{'input_encoding'} = "utf8";
	$self->{'extract_language'} = 1;
	push(@$html_options,"-input_encoding", "utf8");
	push(@$html_options,"-extract_language");
    }

    # Instruct HTMLPlug (when eventually accessed through read_into_doc_obj)
    # to extract these metadata fields from the HEAD META fields
    push(@$html_options,"-metadata_fields","Title,GENERATOR,date,author<Creator>");
    push(@$html_options , "-title_sub", '^(Page\s+\d+)?(\s*1\s+)?');
       
    $self = bless $self, $class;
    $self->load_secondary_plugins($class,$secondary_plugin_options,$hashArgOptLists);

    return bless $self;
}

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

    return q^(?i)\.(doc|dot)$^;
}

sub convert_post_process
{
    my $self = shift (@_);
    my ($conv_filename) = @_;

    my $outhandle=$self->{'outhandle'};
     
    my ($language, $encoding) = $self->textcat_get_language_encoding ($conv_filename);

    # read in file ($text will be in utf8)
    my $text = "";
    $self->read_file ($conv_filename, $encoding, $language, \$text);

    # turn any high bytes that aren't valid utf-8 into utf-8.
    #unicode::ensure_utf8(\$text);
    
    # Write it out again!
    #$self->utf8_write_file (\$text, $conv_filename);
}

sub get_file_type {
    my $self = shift (@_);
    my $file_type = "Word";
    return $file_type;
}

# Modified to cache HTML files for efficieny reasons rather
# than delete all.  HTML is modified not to use IE's VML.
# VML uses WML files, so these can be deleted.
sub cleanup_tmp_area {
    my ($self) = @_;
    if (defined $self->{'files_dir'}) {
	my $html_files_dir = $self->{'files_dir'};

	if (opendir(DIN,$html_files_dir)) {
	    my @wmz_files = grep( /\.wmz$/, readdir(DIN));
	    foreach my $f (@wmz_files) {
		my $full_f = &util::filename_cat($html_files_dir,$f);
		&util::rm($full_f);
	    }
	    closedir(DIN);
	}
	else {
	    # if HTML file has no supporting images, then no _files dir made
	    # => do nothing
	}
    }
}

# do plugin specific processing of doc_obj for HTML type
sub process {
    my $self = shift (@_);
    my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;

    return $self->process_type("doc", $base_dir, $file, $doc_obj);
}

1;

