#! /usr/pkg/bin/perl
#                                                         -*- 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::Control;
use FreePWING::FPWUtils::FPWUtils;
use Getopt::Long;

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

#
# fpwutils 롣
#
initialize_fpwutils();

#
# 줫Ϥե뤬ǤˤС롣
#
unlink($control_file_name);
unlink($control_ref_file_name);

#
# Ҵե򳫤
#
$control = FreePWING::Control->new();
if (!$control->open($control_file_name, $control_ref_file_name)) {
    die "$PROGRAM_NAME: " . $control->error_message() . "\n";
}

#
# ʸǡȥɲä롣
#
if (-f $text_file_name
    && !$control->add_text_entry($text_file_name)) {
    die "$PROGRAM_NAME: " . $control->error_message() . "\n";
}

#
# Фȥɲä롣
#
if (-f $heading_file_name
    && !$control->add_heading_entry($heading_file_name)) {
    die "$PROGRAM_NAME: " . $control->error_message() . "\n";
}

#
# ץǥåȥɲä롣
#
$i = 0;
@file_names = ();
while (-f "$index_file_name$i") {
    unshift(@file_names, "$index_file_name$i");
    $i++;
}
if (0 < $i && !$control->add_index_entry(@file_names)) {
    die "$PROGRAM_NAME: " . $control->error_message() . "\n";
}

#
# ץǥåȥɲä롣
#
$i = 0;
@file_names = ();
while (-f "$endindex_file_name$i") {
    unshift(@file_names, "$endindex_file_name$i");
    $i++;
}
if (0 < $i && !$control->add_endindex_entry(@file_names)) {
    die "$PROGRAM_NAME: " . $control->error_message() . "\n";
}

#
# ︡ǥåȥɲä롣
#
$i = 0;
@file_names = ();
while (-f "$keyindex_file_name$i") {
    unshift(@file_names, "$keyindex_file_name$i");
    $i++;
}
if (0 < $i && !$control->add_keyindex_entry(@file_names)) {
    die "$PROGRAM_NAME: " . $control->error_message() . "\n";
}

#
# ˥塼ɽѥǡȥɲä롣
#
if (-f $menu_file_name
    && !$control->add_menu_entry($menu_file_name)) {
    die "$PROGRAM_NAME: " . $control->error_message() . "\n";
}

#
# ɽǡȥɲä롣
#
if (-f $copyright_file_name
    && !$control->add_copyright_entry($copyright_file_name)) {
    die "$PROGRAM_NAME: " . $control->error_message() . "\n";
}

#
# 顼ǥȥɲä롣
#
if (-f $color_graphic_file_name
    && !$control->add_color_graphic_entry($color_graphic_file_name)) {
    die "$PROGRAM_NAME: " . $control->error_message() . "\n";
}

#
# ȥɲä롣
#
if (-f $sound_file_name
    && !$control->add_sound_entry($sound_file_name)) {
    die "$PROGRAM_NAME: " . $control->error_message() . "\n";
}

#
# ҴեĤ롣
#
if (!$control->close()) {
    die "$PROGRAM_NAME: " . $control->error_message() . "\n";
}

#
# fpwutils θ򤹤롣
#
finalize_fpwutils();

exit 0;
