#                                                         -*- Perl -*-
# Copyright (C) 1999  Kazuhiko Shiozaki
#
# 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, 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.
#

require 5.005;

use English;
use FileHandle;
use FreePWING::FPWUtils::FPWParser;
use Jcode;

#
# EDICT Υǡե̾
#
$data_file_name = 'edict';

#
# EDICT  README ե̾
#
$readme_file_name = 'edict_doc.txt';

#
# ޥɹ԰ϥե뷲֤줿ǥ쥯ȥ롣
#
if (@ARGV == 0) {
  $source_directory = '.';
} else {
  $source_directory = shift;
}

#
# ƽϥե򳫤
#
initialize_fpwparser('text' => \$fpwtext,
		     'heading' => \$fpwheading,
		     'word2' => \$fpwword2,
		     'menu' => \$fpwmenu,
		     'copyright' => \$fpwcopyright);

#
# ɽʬȥ˥塼ʬϿ롣
# 
#
if (!$fpwmenu->add_reference_start()
    || !$fpwmenu->add_text("About This Dictionary")
    || !$fpwmenu->add_reference_end("readme1")
    || !$fpwmenu->add_newline()
    || !$fpwmenu->add_reference_start()
    || !$fpwmenu->add_text("About This Package")
    || !$fpwmenu->add_reference_end("readme2")
    || !$fpwmenu->add_newline()) {
  die "$PROGRAM_NAME: " . $fpwmenu->error_message() . "\n";
}
if (!$fpwcopyright->add_reference_start()
    || !$fpwcopyright->add_text("About This Dictionary")
    || !$fpwcopyright->add_reference_end("readme1")
    || !$fpwcopyright->add_newline()
    || !$fpwcopyright->add_reference_start()
    || !$fpwcopyright->add_text("About This Package")
    || !$fpwcopyright->add_reference_end("readme2")
    || !$fpwcopyright->add_newline()) {
  die "$PROGRAM_NAME: " . $fpwcopyright->error_message() . "\n";
}
if (!$fpwmenu->new_context()
    || !$fpwmenu->add_tag("readme1")) {
  die "$PROGRAM_NAME: " . $fpwmenu->error_message() . "\n";
}

#
# README ե򳫤
#
$handle = FileHandle->new();
if (!$handle->open("$source_directory/$readme_file_name", 'r')) {
  die "$PROGRAM_NAME: failed to open the file, $ERRNO: $source_directory/$readme_file_name\n";
}

for (;;) {
  $_ = $handle->getline();
  last if !defined($_);
  chomp;
  print "| ", $_, "\n";
  if (!$fpwmenu->add_text($_)
      || !$fpwmenu->add_newline()) {
    die "$PROGRAM_NAME: " . $fpwmenu->error_message() . "\n";
  }
}

#
# README.PKG ե򳫤
#
$handle = FileHandle->new();
if (!$handle->open("README.PKG", 'r')) {
  die "$PROGRAM_NAME: failed to open the file, $ERRNO: README.PKG\n";
}
if (!$fpwmenu->new_context()
    || !$fpwmenu->add_tag("readme2")) {
  die "$PROGRAM_NAME: " . $fpwmenu->error_message() . "\n";
}

for (;;) {
  $_ = $handle->getline();
  last if !defined($_);
  chomp;
  $_ = jcode($_, "sjis")->euc;
  print "| ", $_, "\n";
  if (!$fpwmenu->add_text($_)
      || !$fpwmenu->add_newline()) {
    die "$PROGRAM_NAME: " . $fpwmenu->error_message() . "\n";
  }
}

#
# EDICT Υǡե򳫤
#
$handle = FileHandle->new();
if (!$handle->open("$source_directory/$data_file_name", 'r')) {
  die "$PROGRAM_NAME: failed to open the file, $ERRNO: $source_directory/$data_file_name\n";
}

for (;;) {
  #
  # ιԤɤ߹ࡣʤХ롼פȴ롣
  #
  $_ = $handle->getline();
  if (!defined($_)) {
    last;
  }
  chomp;
  
  if (/^([^\/]*)(.*)$/) {
    $key = $1;
    $key_kana = $1;
    $content = $2;
    $key =~ s/ .*$//;
    $key_kana =~ s/^.* \[//;
    $key_kana =~ s/\] $//;
    $key_kana =~ s/ $//;
    $content =~ s/^\///g;
    $content =~ s/\/$//g;
    $content =~ s/\// \/ /g;
    
    # ΥȥϥȤȸʤ
    if ($key ne "") {
      if ($key ne $key_kana) {
	$item = $key . "" . $key_kana . "";
	#ФΥեޥåȤϹߤʤΤǡŬѹƤ
	#㤨йʤɤƱեޥåȤˤʤ鲼Τ褦ˡ
	#$item = $key_kana . "" . $key . "";
      } else {
	$item = $key;
      }
      register();
      #    s/\s+$//;			# ζʸ
      s/&/&amp;/g;			# ᥿饯ִ
      s/</&lt;/g;
      s/>/&gt;/g;
      s/^([\200-\377]+) +//;	# ФڤФ
      $content = $1;		# Ф줬ʸˤʤ
      while( s/^\[([\200-\377]+)\] +// ){ ; } # 겾̾ΤƤ
      s!^/!!;
      s!/$!!;
      for $head ( split( "/",$_ ) ){
	$STR{$head} .= ">$content";
	#      print "HEAD : ",$head, ", CONTENT : ", $content, "\n";
      }
    }
  } else {
    print "******* ", $_, "\n";
  }
}   
for $head ( keys %STR ){
  for( split( />/,substr($STR{$head},1) ) ){
    $content{$_}++;
  }
  for( keys %content ){
    if( /^([\200-\377]{2})\1/ && $content{sprintf("%s%s",$1,substr($_,4))} ){
      $content{$_}=0;
    }
  }
  $head =~ s/\[/&lp;/g;
  $head =~ s/\]/&rp;/g;
  $str = $head;
  $str =~ tr/A-Z/a-z/;
  $str =~ s/\s+/ /;
  $key = $str;			# $key = ȱʸ/ʸԤʤäʸ
  $str =~ s/^(\([^\)]+\) *)+//;
  $str =~ s/( *\([^\)]+\))+$//;
  $str =~ s/\[[^\]]*\]//g;               
  $str =~ s/^~ //;
  $str =~ s/ ~$//;		# $str = (...)  ~ ʸ
  $str = $key unless $str;
  if( $str eq $key ){
    push( @LINE, sprintf( "%s\x00%s\x00<%s /%s/\n",
			  $str, $head, $head,
			  join("/",grep($content{$_}>0,keys %content)) ));
  } else {
    push( @LINE, sprintf( "%s\x00%s\x00<%s [%s] /%s\n",
			  $str, $head, $head, $str,
			  join("/",grep($content{$_}>0,keys %content)) ));
  }
  undef %content;
  #  print @LINE, "\n";
}
undef %STR;
for( sort @LINE ){
  @f = split( /</,$_,2 );
  #  print $f[1];
  $_ = $f[1];
  chomp;
  
  if (/^([^\/]*)(.*)$/) {
    $key = $1;
    $key_kana = $1;
    $content = $2;
    $key =~ s/ \[.*$//;
    $key_kana =~ s/^.* \[//;
    $key_kana =~ s/\] $//;
    #    $key_kana =~ s/ $//;
    $key =~ s/&amp;/&/g;			# ᥿饯ִ
    $key =~ s/&lt;/</g;
    $key =~ s/&gt;/>/g;
    $key =~ s/&lp;/\[/g;
    $key =~ s/&rp;/\]/g;
    $key_kana =~ s/&amp;/&/g;			# ᥿饯ִ
    $key_kana =~ s/&lt;/</g;
    $key_kana =~ s/&gt;/>/g;
    $key_kana =~ s/&lp;/\[/g;
    $key_kana =~ s/&rp;/\]/g;
    $content =~ s/^\///g;
    $content =~ s/\/$//g;
    $content =~ s/\// \/ /g;
    
    #    if ($key ne $key_kana) {
    #      $item = $key . "" . $key_kana . "";
    #      #ФΥեޥåȤϹߤʤΤǡŬѹƤ
    #      #㤨йʤɤƱեޥåȤˤʤ鲼Τ褦ˡ
    #      #$item = $key_kana . "" . $key . "";
    #    } else {
    #      $item = $key;
    #    }
    $item = $key;
    $key = $key_kana;
    register();
  }
}
#
# ǡեĤ롣
#
$handle->close();

#
# ƽϥեĤ롣
#
finalize_fpwparser('text' => \$fpwtext,
		   'heading' => \$fpwheading,
		   'word2' => \$fpwword2,
		   'menu' => \$fpwmenu,
		   'copyright' => \$fpwcopyright);

sub register() {
  if (!$fpwtext->new_entry()) {
    die "$PROGRAM_NAME: " . $fpwtext->error_message() . "\n";
  }
  if (!$fpwheading->new_entry()) {
    die "$PROGRAM_NAME: " . $fpwheading->error_message() . "\n";
  }
  
  #ν񤭹
  
  $text_position = $fpwtext->entry_position();
  $heading_position = $fpwheading->entry_position();
  if ($key_kana ne $key) {
    if (!$fpwword2->add_entry($key_kana, $heading_position,
			      $heading_file_name, $text_position,
			      $text_file_name)) {
      die "$PROGRAM_NAME: " . $fpwword2->error_message() . "\n";
    }
  }
  if (!$fpwword2->add_entry($key, $heading_position,
			    $heading_file_name, $text_position,
			    $text_file_name)) {
    die "$PROGRAM_NAME: " . $fpwword2->error_message() . "\n";
  }
  
  #Фν񤭹
  if (!$fpwheading->add_text($item)) {
    die "$PROGRAM_NAME: " . $fpwheading->error_message() . "\n";
  }
  
  #ʸν񤭹
#  print $key, ", ", $key_kana, ", ", $item, "\n";
  print $item, "\n";
  if (!$fpwtext->add_keyword_start()
      || !$fpwtext->add_text($item)
      || !$fpwtext->add_keyword_end()
      || !$fpwtext->add_newline()
      || !$fpwtext->add_indent_level(2)
      || !$fpwtext->add_text($content)
      || !$fpwtext->add_newline()) {
    die "$PROGRAM_NAME: " . $fpwtext->error_message() . "\n";
  }
}
