#!/bin/sh
# Created: Thu Aug 25 21:28:25 CEST 1999 by heinrich@zaphod.wh9.tu-dresden.de
# Copyright (C) 1999  Heinrich Langos
#
# ----------------------- BEGIN LEGAL STUFF ---------------------------
#
#This program is free software; you can redistribute it and/or modify it
#under the terms of the GNU Library 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
#Library General Public License for more details.
#
#You should have received a copy of the GNU Library General Public
#License along with this library; if not, write to the Free
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
# ----------------------- END LEGAL STUFF ---------------------------
#
# This is a script to convert the GERMAN-ENGLISH dictionary files as
# used on http://www.tu-chemnitz.de/urz/netz/forms/dict.html 
# and http://www.tu-chemnitz.de/~fri/ding/ into the dict (www.dict.org) 
# format.
#
# It produces a dictionary that translates english words into german words.
#
# If you want to generate a dictionary with german headwords you'll
# have to change REVERSE to "1". The resulting files will be named
# ding_rev.dict.dz and ding_rev.index so that you can install the 
# reverse one along a normal one.
#
# USAGE:
#
# Copy this script and the ger-eng.txt from the ding package into an empty
# directory. Don't forget to adjust the pathes below. Run the script and see
# how the available space on your filesystem vanishes :-)
# 
# If all goes well you end up with some files named "ding.dict.dz" and
# "ding.index". Move them to /usr/share/dictd/ or wherever you like em 
# to be and add a line like the following to your dictd.conf file. 
#
# database ding {data /usr/share/dictd/ding.dict.dz index /usr/share/dictd/ding.index}
#
# Restart your dictd and have fun.
#
# "dictzip" should have come with your dictd package and "dictfmt" can be 
# found in the dict-misc tarball. 
# Both are also available as staticly linked linux-i386 binarys from this 
# page: http://www.wh9.tu-dresden.de/~heinrich/dict/ or somewhere around
# that area :-)
# 
# have fun
# -heinrich

########### config stuff #################

DEBUG="0"

export REVERSE="0"

dictfmt="../dictfmt"
dictzip="../dictzip"

##########################################

echo >chemnitz
cat ger-eng.txt | perl -w -e '
$i=0;
while (<>) {
  chomp;
  @lin = split / :: /;

  if ($#lin == 1 ) {
    @def = split (/,/,$lin[1]);
    @wrd = split (/,/,$lin[0]);

    for ($i=0;$i<=$#def;$i++) {
      for ($j=0;$j<=$#wrd;$j++) {
        printf "%s -- %s\n",$def[$i],$wrd[$j];
      }
    }

  }else{
#    printf "### %s ###\n",$_;
  }
}' >> chemnitz

cat chemnitz | grep -v "^$" | perl -w -e '
while (<>) {
  chomp;
  @lin = split / -- /;
  if ($#lin == 1 ) {
    while (substr($lin[0],0,1) eq " ") { $lin[0]=substr($lin[0],1);}
    while (substr($lin[1],0,1) eq " ") { $lin[1]=substr($lin[1],1);}

    while (substr($lin[0],-1,1) eq " ") { $lin[0]=substr($lin[0],0,-1);}
    while (substr($lin[1],-1,1) eq " ") { $lin[1]=substr($lin[1],0,-1);}

    if ($ENV{'REVERSE'} eq "0" ) {
      printf "%s -- %s\n",$lin[0],$lin[1];
    }
    else{
      printf "%s -- %s\n",$lin[1],$lin[0];
    }
  }
}' > chemnitz2

if [ $DEBUG == "0" ] ; then 
  rm -f chemnitz
fi


sort -fr chemnitz2  | uniq -i > chemnitz3



if [ $DEBUG == "0" ] ; then 
  rm -f chemnitz2
fi

# sort as if they were all upcase and ignore lines that only differ in case.
# "-r" gives the lowercase version of an english word preference over the
# uppercase word for the following uniq command.

echo '%h 00-database-info' > ding
echo '%d' >> ding
echo -e '\tThis is a conversion of the GERMAN-ENGLISH dictionary file ' >> ding
echo -e '\tfrom the Ding project http://www.tu-chemnitz.de/~fri/ding/.' >> ding
echo -e "\tDing is published under GPL." >> ding
echo >> ding




sort chemnitz3 | perl -w -e ' 
  $olddef=""; 
  $oldind=""; 
  while (<>) { 
    @lin = split / -- /;
    if ($#lin == 1) {
      if ($lin[0] eq $oldind){ 
        printf "\t%s",$lin[1]; 
        $olddef.=" ".$lin[1]; 
      }      
      else {
        printf "\n";  
        $olddef=$lin[1];  
        $oldind=$lin[0]; 
        printf "%%h %s\n",$lin[0];  
        printf "%%d \n";  
        printf "\t%s",$lin[1];
      }
    }
  }
' >> ding

if [ $DEBUG == "0" ] ; then 
  rm -f chemnitz3
fi

if [ $REVERSE == "0" ] ; then 
  $dictfmt -p -u http://www.tu-chemnitz.de/~fri/ding/  -s "Ding dictionary" ding < ding
  $dictzip ding.dict
else
  $dictfmt -p -u http://www.tu-chemnitz.de/~fri/ding/  -s "Ding dictionary (reverse)" ding_rev < ding
  $dictzip ding_rev.dict
fi


if [ $DEBUG == "0" ] ; then 
  rm -f ding
fi


