#!/usr/bin/env perl
# xdict2sdict - Creates a StarDict dictionary.
# Revision: 070124

#---------------------------------------------------------------------
#                            module setup
#---------------------------------------------------------------------

require 5.6.1;
use strict;
use Carp;
use warnings;
                                # Trap warnings
$SIG {__WARN__} = sub { die @_; };

#---------------------------------------------------------------------
#                           basic constants
#---------------------------------------------------------------------

use constant ZERO  => 0;        # Zero
use constant ONE   => 1;        # One

use constant FALSE => 0;        # Boolean FALSE
use constant TRUE  => 1;        # Boolean TRUE

#---------------------------------------------------------------------
#                            main routine
#---------------------------------------------------------------------

sub Main
{
    undef $/;
    my $data;
    my @data;
    my $iebase = 'xdict2sdict: Internal error';

    my ($ifname) = shift (@ARGV);

    if (!defined ($ifname) || !length ($ifname) || (!-T $ifname))
    {
        die "Usage: xdict2sdict foo.txt\n";
    }

    open (IFD, "<$ifname") ||
        die "Error: Can't open file for reading: $!\n$ifname\n";
    undef $/;
    binmode (IFD);
    $data = <IFD>;
    $data = "" if !defined $data;
    close IFD;

    die "$iebase #1\n" if $data =~ m@\.equals\b@i;

    $data =~ s@(\.item\b[^\012=]+)(\n)@$1 .equals $2@g;
    $data =~ s@(\.item\b[^\012=]+)=@$1 .equals @g;
    $data =~ s@\s+@ @gs;
    $data =~ s@^\s+@@;
    $data =~ s@\s+\z@@;

    @data =  sort split (/\.item\b/i, $data);
    open (OFD, ">newdict");
    my $num = 0;

    for my $item (@data)
    {
        $item =~ s@^\s+@@;
        $item =~ s@\s+\z@@;
        next unless length ($item);
        my ($term, $def) = $item =~ m@^(.+)\.equals\b(.+)@;
        die "$iebase #2: $item\n" if !defined $def;

        $def =~ s@^\s+@@;
        $def =~ s@\s+\z@@;
        $term =~ s@^\s+@@;
        $term =~ s@\s+\z@@;
        die "err2 $item\n" if !length ($def) || !length ($term);

        die "$iebase #3 $term\n" if $term =~ m@\\@;
        print OFD "$term\t$def\n";
        ++$num;
    }

    close OFD;
    unlink "newdict.idx";
    system "sdtabfile newdict";
    my $size = (stat "newdict.idx") [7];
    print "size is $size\n";
    open (OFD, ">newdict.ifo");
    print OFD << "END";
StarDict's dict ifo file
version=2.4.2
wordcount=$num
idxfilesize=$size
bookname=FCOCL
author=Wikipedia and similar sources
email=no-one\@nodomain.unknown
website=www.nodomain.unknown
description=A Stardict-compatible dictionary
date=2006.04.25
sametypesequence=m
END
    close OFD;

    unlink 'newdict';
    print << "END";

Created "newdict.*".  Note:  You can rename these files.  However, the
files should be named consistently, and the filename extensions should
be preserved.
END
    undef;
}

#---------------------------------------------------------------------
#                            main program
#---------------------------------------------------------------------

&Main();                        # Call the main routine
exit ZERO;                      # Normal exit
