#! /usr/local/bin/perl5
#                                                         -*- Perl -*-
# Copyright (c) 1999, 2000  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 FreePWING::Sort;
use FreePWING::FPWUtils::FPWUtils;
use Getopt::Long;

#
# ޥɹԤϤ롣
#
if (!GetOptions('workdir=s' => \$work_directory)) {
    exit 1;
}

#
# Ķѿ TMPDIR ꤹ롣
#
$ENV{'TMPDIR'} = $work_directory;

#
# fpwutils 롣
#
initialize_fpwutils();

#
# 줫Ϥե뤬ǤˤС롣
#
unlink($sort_file_name);
unlink($endsort_file_name);
unlink($keysort_file_name);

#
# sort ޥɤǥȤԤ
#
if (-f $word_file_name) {
    $sort = FreePWING::Sort->new();
    if (!$sort->open($sort_file_name)) {
        die "$PROGRAM_NAME: " . $sort->error_message() . "\n";
    }
    if (!$sort->add_entries_in_file($word_file_name)) {
        die "$PROGRAM_NAME: " . $sort->error_message() . "\n";
    }
    if (!$sort->close()) {
        die "$PROGRAM_NAME: " . $sort->error_message() . "\n";
    }
}

if (-f $endword_file_name) {
    $endsort = FreePWING::Sort->new();
    if (!$endsort->open($endsort_file_name)) {
        die "$PROGRAM_NAME: " . $endsort->error_message() . "\n";
    }
    if (!$endsort->add_entries_in_file($endword_file_name)) {
        die "$PROGRAM_NAME: " . $endsort->error_message() . "\n";
    }
    if (!$endsort->close()) {
        die "$PROGRAM_NAME: " . $endsort->error_message() . "\n";
    }
}

if (-f $keyword_file_name) {
    $keysort = FreePWING::Sort->new();
    if (!$keysort->open($keysort_file_name)) {
        die "$PROGRAM_NAME: " . $keysort->error_message() . "\n";
    }
    if (!$keysort->add_entries_in_file($keyword_file_name)) {
        die "$PROGRAM_NAME: " . $keysort->error_message() . "\n";
    }
    if (!$keysort->close()) {
        die "$PROGRAM_NAME: " . $keysort->error_message() . "\n";
    }
}

#
# fpwutils θ򤹤롣
#
finalize_fpwutils();

exit 0;
