#                                                         -*- Perl -*-
# Copyright (C) 1999  Motoyuki Kasahara
#
# 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;

require 'jcode.pl';

#
# gene Υǡե̾
#
$data_file_name = 'gene.txt';

#
# gene  README ե̾
#
$readme_file_name = 'readme.txt';

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

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

#
# PDIC Υǡե򳫤
#
$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 (;;) {
    #
    #  2 Ԥɤ߹ࡣʤХ롼פȴ롣
    #
    $heading_line = $handle->getline();
    $text_line = $handle->getline();
    if (!defined($heading_line) || !defined($text_line)) {
	last;
    }
    $heading_line =~ s/\r\n//;
    $text_line =~ s/\r\n//;
    $heading_line = jcode::euc($heading_line, 'sjis', 'z');
    $text_line = jcode::euc($text_line, 'sjis', 'z');

    #
    # ǻϤޤ븫ФԤ̵뤹롣
    #
    if ($heading_line =~ /^ /) {
	next;
    }

    #
    #  3 İʾΥ줿Фʬ䤹롣
    #
    @headings = ();
    @words = ();
    $word_count = 0;
    foreach $heading (split(/ {3,}/, $heading_line)) {
	$word = $heading;
	$word =~ s/[\-\']//g;
	$word =~ s/ \+\d+$//;
	if ($word !~ /^ *$/) {
	    push(@headings, $heading);
	    push(@words, $word);
	    $word_count++;
	}
    }
    if ($word_count == 0) {
	next;
    }

    #
    # ʸΰ֤롣
    #
    if (!$fpwtext->new_entry()) {
	die "$PROGRAM_NAME: " . $fpwtext->error_message() . "\n";
    }
    $text_position = $fpwtext->entry_position();

    #
    # ƸФȸϿ롣
    #
    for ($i = 0; $i < $word_count; $i++) {
	$heading = $headings[$i];
	$word = $words[$i];
	print $word, "\n";

	if (!$fpwheading->new_entry()) {
	    die "$PROGRAM_NAME: " . $fpwheading->error_message() . "\n";
	}
	$heading_position = $fpwheading->entry_position();

	if (!$fpwheading->add_text($heading)) {
	    die "$PROGRAM_NAME: " . $fpwheading->error_message() . "\n";
	}

	if (!$fpwword2->add_entry($word, $heading_position,
				  $heading_file_name, $text_position,
				  $text_file_name)) {
	    die "$PROGRAM_NAME: " . $fpwword2->error_message() . "\n";
	}

	if (!$fpwtext->add_keyword_start()
	    || !$fpwtext->add_text($heading)
	    || !$fpwtext->add_keyword_end()
	    || !$fpwtext->add_newline()) {
	    die "$PROGRAM_NAME: " . $fpwtext->error_message() . "\n";
	}
    }

    #
    # ʸʸȤɲä롣
    #
    if (!$fpwtext->add_indent_level(2)
	|| !$fpwtext->add_text($text_line)
	|| !$fpwtext->add_newline()) {
	die "$PROGRAM_NAME: " . $fpwtext->error_message() . "\n";
    }
}

#
# ǡեĤ롣
#
$handle->close();

#
# 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();
    if (!defined($_)) {
	die "$PROGRAM_NAME: unexpected EOF: $source_directory/$readme_file_name\n";
    }
    s/\r\n//;
    $_ = jcode::euc($_, 'sjis', 'z');
    if ($_ eq 'ۤʤ') {
	if (!$fpwcopyright->add_text($_)
	    || !$fpwcopyright->add_newline()) {
	    die "$PROGRAM_NAME: " . $fpwcopyright->error_message() . "\n";
	}
	last;
    }
}

#
# ɽʬȤϿ롣
#
for (;;) {
    $_ = $handle->getline();
    if (!defined($_)) {
	last;
    }
    s/\r\n//;
    $_ = jcode::euc($_, 'sjis', 'z');
    if ($_ eq "\x1a") {
	last;
    } elsif (/^$/) {
	if (!$fpwcopyright->add_newline()) {
	    die "$PROGRAM_NAME: " . $fpwcopyright->error_message() . "\n";
	}
    } else {
	if (!$fpwcopyright->add_text($_)) {
	    die "$PROGRAM_NAME: " . $fpwcopyright->error_message() . "\n";
	}
    }
}

if (!$fpwcopyright->add_newline()) {
    die "$PROGRAM_NAME: " . $fpwcopyright->error_message() . "\n";
}

#
# ᥤեĤ롣
#
$handle->close();

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

