#!/usr/local/bin/perl
# Copyright(C) 2004-2009, 2310. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the
#    distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

use strict;
use File::Basename;

my $command_cdbmake = "/usr/local/bin/cdbmake";

my @contents;
my @contents_converted;

&mainsub;

sub mainsub () {
    #----- variable
    my $input_file = $ARGV[0];
    my $output_file;
    my $output_tmp_file;
    my $line;

    #----- file check
    if (! $input_file) {
	print_help_message("input skk dictionary filename");
	exit 1;
    } else {
	$output_file = basename($input_file).".cdb";
	$output_tmp_file = "$output_file.tmp";
    }

    # ----- read file
    if (!open(FILE, "<$input_file")) {
	print_help_message("can't read $input_file");
	exit 1;
    }
    @contents = <FILE>;
    close(FILE);


    # ----- convert
    convert_contents();

    # ---- cdbmake
    open(OUTPUT, "|$command_cdbmake $output_file $output_tmp_file") || die "command error";
    print OUTPUT join("\n", @contents_converted);
    print OUTPUT "\n\n";
    close(OUTPUT);
    exit 0;
}

# ----------------------------------------

sub print_help_message () {
    my $script_name = basename($0);
    my $message = @_[0];
    print "\t$script_name is small utility to make cdb file from skk-dictionary files\n";
    print "\tUsage: $script_name <input_file_name>\n";
    if ($message) {
	print "***** $message *****\n";
    }
}

sub convert_contents () {
    my $key;
    my $key_length;
    my $value;
    my $value_length;
    foreach (@contents) {
	if ($_ =~ /^\s*;/) {
	    next;
	}
	if ($_ =~ /^(\S+) +(\S.*)$/) {
	    $key = $1;
	    $value = $2;
	    $key_length = length($key);
	    $value_length = length($value);
	}
	push(@contents_converted, "+${key_length},${value_length}:${key}->${value}");
    }
}
