#!/usr/bin/perl

use lib qw(/opt/swish-e/swish-filters);

use SWISH::Filter;
# Requires MIME::Types

my @docs = (
   'tests/textplain.txt',
   'tests/msexcel.xls',
   'tests/msword.doc',
   'tests/test.mp3'
);

my $filter = SWISH::Filter->new;

for my $file ( @docs ) {
    
    my $converted = $filter->filter("document", $file);
    my $swish_type_ref = $filter->content_type;
    my $swish_path_name = $file;
    my $swish_size = length(${$filter->fetch_doc}) + 1;
 
    if ( $converted ) {
        
        #print "Converted $file\n", substr(
        print "Document-Type: " . $swish_type_ref . "\n";
        print "Path-Name: " . $swish_path_name . "\n";
        print "Content-Length: " . $swish_size . "\n";
        # print "Last-Mtime: " . $swish_mtime . "\n";
        print "\n"; # Blank newline marks end of headers
        # print substr( ${$filter->fetch_doc} ,0,512) . "\n";
        print ${$filter->fetch_doc};
    } else { 
        print STDERR "WARN: not converted $file\n";
                open FH, $file or die "Failed to open $file: $!";
        local $/ = undef;
        my $content = <FH>;
        $swish_size = length($content) + 1;
        print "Document-Type: " . $swish_type_ref . "\n";
        print "Path-Name: " . $swish_path_name . "\n";
        print "Content-Length: " . $swish_size . "\n";
        # print "Last-Mtime: " . $swish_mtime . "\n";
        print "\n"; # Blank newline marks end of headers
        print substr( $content ,0,512) . "\n";

    }
}
