#! /usr/bin/perl -w

# 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 of the License, 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

use utf8;
use encoding 'utf8';
use open ':utf8'; # input and output default layer will be UTF-8
use POSIX;
my $prefix="th-dict";
mkdir $prefix or die "Could not create Thwab directory";
mkdir $prefix."/+" or die "Could not create Thwab control + directory";
chdir $prefix;
open I ,">+/0" or die "Could not create Thwab info +/0 file";
open TOC ,">+/1" or die "Could not create Thwab TOC +/1 file";
print I "thwab = 2250\n",
	"version = 1\n",
	"charset = UTF-8\n",
	"format = plain\n",
	"digits = 4\n",
	"title = Dictionary Title\n",
	"subtitle = Dictionary Subtitle\n",
	"lang = en\n",
	"classification = Lexical:Dictionaries\n",
	"key = headword\n",
	"author = Author:short:full\n_\n";
my $mode=0;
my $empty=0;
my $ch_n=0;
my $sec_n=0;
my $itm_n=0;
my $ch="";
my $sec="";
my $itm="";
my $old_ch="";
my $fn;
while (<>) {
chomp;
  if (/^00-database/) {
    $mode=1;
    print I "$_\n";
# 
#   } elsif (/^(\S\S)/) {
#     $mode=0;
#     chdir $prefix.$ch;
  } elsif (/^(\S)/) {
  # start at column zero
  $itm=$_;
  $ch=uc($1);
  $mode=0;
#    if ($itm=~/\W*(\w)/) { # 1st alphanum
#       $ch="$1"; $ch=uc $ch;
#       if ($ch=~/[^\p{alpha}]/) {$ch="0"} # all non alpha to ch 0
#     } else {$ch="0"}
   if ($ch ne $old_ch) {
      printf "(%s)(%s)(%d)\n",$ch,$itm,$ch_n;

      $ch_n=$ch_n+1;
      $fn=sprintf "%04d",$ch_n;
      mkdir $fn;
      $old_ch=$ch;
      printf TOC "%s\t%s\t%s\n",$fn,5,$ch;
      $itm_n=0;
    }
    $fn=sprintf "%04d/%04d",$ch_n,$itm_n;
    $itm_n=$itm_n+1;
    if (F) {close F}
    open F ,">".$fn or die "Could not create Thwab TOC +/1 file";
    print F "$itm\n\n";
    printf TOC "%s\t%s\t%s\n",$fn,2,$itm;
} elsif (/^\s*$/) {
    if ($empty!=0) {$empty=0; next}
    if ($mode==1) {print I "\n"}
    else  {print F "\n"}
    $empty=1;
  } else {
   print "[/$_/]\n";
    if ($mode==1) {print I "$_\n"} else  {print F "$_\n"}
  }
}

if (F) {close F}
close TOC;
close I;
