#
# -*- Perl -*-
# $Id:$
# Copyright (C) 2004 Tadamasa Teranishi All rights reserved.
# Copyright (C) 2000-2004 Namazu Project All rights reserved.
#     This is free software with ABSOLUTELY NO WARRANTY.
#
#  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 versions 2, 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., 59 Temple Place - Suite 330, Boston, MA
#  02111-1307, USA
#
#  This file must be encoded in EUC-JP encoding
#

package swf;
use strict;
require 'util.pl';
require 'gfilter.pl';

my $swfconvpath  = undef;
my @swfconvopts  = undef;
my $utfconvpath = undef;

sub mediatype() {
    return ('application/x-shockwave-flash');
}

sub status() {
    #
    # swf2html
    # http://www.macromedia.com/software/flash/download/search_engine/index.html
    #
    # Support:
    #   Flash Player 3, 4, 5 and 6.
    #   Windows NT4, Windows 2000 Professional, and Linux RedHat 7.1 systems.
    #

    $swfconvpath = util::checkcmd('swf2html');
    if (defined $swfconvpath) {
	@swfconvopts = ();
        if (!util::islang("ja")) {
            return 'yes';
        } else {
            $utfconvpath = util::checkcmd('lv');
            if (defined $utfconvpath) {
                return 'yes';
            } else {
                return 'no';
            }
        }
    }
    else {
        return 'no';
    }
}

sub recursive() {
    return 0;
}

sub pre_codeconv() {
    return 0;
}

sub post_codeconv () {
    return 0;
}

sub add_magic ($) {
    my ($magic) = @_;

    $magic->addFileExts('\\.swfl?$', 'application/x-shockwave-flash');

    $magic->addMagicEntry("0\tstring\tSWS\tapplication/x-shockwave-flash");
    $magic->addMagicEntry("0\tstring\tCWS\tapplication/x-shockwave-flash");

    return;
}

sub filter ($$$$$) {
    my ($orig_cfile, $cont, $weighted_str, $headings, $fields)
	= @_;
    my $err = undef;

    $err = filter_swf2html($orig_cfile, $cont, $weighted_str, $headings, $fields);
 
    return $err;
}   

sub filter_swf2html ($$$$$) {
    my ($orig_cfile, $cont, $weighted_str, $headings, $fields)
      = @_;
    my $cfile = defined $orig_cfile ? $$orig_cfile : '';

    util::vprint("Processing swf file ... (using '$swfconvpath')\n");

    my $tmpfile = util::tmpnam('NMZ.swf');
    {
	my $fh = util::efopen("> $tmpfile");
	print $fh $$cont;
        $fh->close();
    }
    {
	my @cmd = ($swfconvpath, @swfconvopts, $tmpfile);
	my ($status, $fh_out, $fh_err) = util::systemcmd(@cmd);
	my $size = util::filesize($fh_out);
	if ($size == 0) {
	    return "Unable to convert file ($swfconvpath error occurred).";
	}
	if ($size > $conf::TEXT_SIZE_MAX) {
	    return 'Too large swf file.';
	}
        $$cont = util::readfile($fh_out);
    }
    unlink $tmpfile;

    if (util::islang("ja")) {
        my $encoding = "s"; # Shift_JIS

        if ($$cont =~ m!<meta http-equiv="content-type" content="text/html; charset=utf-8">!si)
        {
            # Flush Version >= 6
            $encoding = "u8"; # UTF-8
        }
        {
            my $fh = util::efopen("> $tmpfile");
            print $fh $$cont;
            $fh->close();
        }
        {
            my @cmd = ($utfconvpath, "-I$encoding", "-Oej", $tmpfile);
            my ($status, $fh_out, $fh_err) = util::systemcmd(@cmd);
            my $size = util::filesize($fh_out);
            if ($size == 0) {
                return "Unable to convert file ($utfconvpath error occurred).";
            }
            if ($size > $conf::TEXT_SIZE_MAX) {
                return 'Too large swf file';
            }
            $$cont = util::readfile($fh_out);
            codeconv::normalize_eucjp($cont);
        }
    }
    unlink $tmpfile;

    codeconv::toeuc($cont);

    # Title shoud be removed.
    $$cont =~ s!<TITLE.*?>.*?</TITLE>!!is;

    html::html_filter($cont, $weighted_str, $fields, $headings);

    gfilter::line_adjust_filter($cont);
    gfilter::line_adjust_filter($weighted_str);
    gfilter::white_space_adjust_filter($cont);
    $fields->{'title'} = gfilter::filename_to_title($cfile, $weighted_str)
	unless $fields->{'title'};
    gfilter::show_filter_debug_info($cont, $weighted_str,
				    $fields, $headings);
    return undef;
}

1;
