###########################################################################
#
# 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" },
      { 'name' => "description_tags",
	'desc' => "{HTMLPlug.description_tags}",
	'type' => "flag" }
      ];

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' => "metadata_fields",
			 'type' => "string",
			 'deft' => "Title" },
		       { 'name' => "level1_header",
			 'desc' => "{StructuredHTMLPlug.level1_header}",
			 'type' => "regexp",
			 'reqd' => "no",
			 'deft' => "" },
		       { 'name' => "level2_header",
			 'desc' => "{StructuredHTMLPlug.level2_header}",
			 'type' => "regexp",
			 'reqd' => "no",
			 'deft' => "" },
		       { 'name' => "level3_header",
			 'desc' => "{StructuredHTMLPlug.level3_header}",
			 'type' => "regexp",
			 'reqd' => "no",
			 'deft' => "" },
		       { 'name' => "title_header",
			 'desc' => "{StructuredHTMLPlug.title_header}",
			 'type' => "regexp",
			 'reqd' => "no",
			 'deft' => "" },
		       { 'name' => "delete_toc",
			 'desc' => "{StructuredHTMLPlug.delete_toc}",
			 'type' => "flag",
			 'reqd' => "no",
			 # set the mode so this doesn't show up unless 
			 # all the following ones do
			 'modegli' => "3"},
		       { 'name' => "toc_header",
			 'desc' => "{StructuredHTMLPlug.toc_header}",
			 'type' => "regexp",
			 'reqd' => "no",
			 'deft' => "" }
		       ];
	
	push(@$arguments,@$ws_arg);
    }

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

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

    if ($self->{'info_only'}) {
	# don't worry about any options etc
	return bless $self, $class;
    }

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

    # we always save as utf-8
    if ($self->{'input_encoding'} eq "auto") {
	$self->{'input_encoding'} = "utf8";
    }

    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'};
	    
	    # 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,"-extract_language") if $self->{'extract_language'};
	    push (@$structhtml_options, "-delete_toc") if $self->{'delete_toc'};
	    push (@$structhtml_options, "-toc_header", $self->{'toc_header'}) if $self->{'toc_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, "-metadata_fields", $self->{'metadata_fields'}) if $self->{'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'};
    my $structhtml_options = $secondary_plugin_options->{'StructuredHTMLPlug'};    
    # wvWare will always produce html files encoded as utf-8, so make sure the secondary HTMLPlug knows this
    push(@$html_options,"-input_encoding", "utf8");
    push(@$html_options,"-extract_language") if $self->{'extract_language'};
    push(@$html_options, "-description_tags") if $self->{'description_tags'};

    # 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+)?');

    my $associate_tail_re = $self->{'associate_tail_re'};
    if ((defined $associate_tail_re) && ($associate_tail_re ne "")) {
	push(@$html_options, "-associate_tail_re", $associate_tail_re);
	push(@$text_options, "-associate_tail_re", $associate_tail_re);
	push(@$structhtml_options, "-associate_tail_re", $associate_tail_re);
    }
       
    $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;

