#!/bin/sh


DICTDIR=$OOINSTDIR/share/dict/ooo

if (! test -d $DICTDIR); then
    echo "Cannot find $DICTDIR; make sure you installed OOo first"
    exit 1;
fi


# the available dictionaries
DICTS="hyph_cs_CZ
	hyph_de_CH
	hyph_en_CA
	hyph_es_ES
	hyph_fi_FI
	hyph_fr_FR
	hyph_ga_IE
	hyph_hu_HU
	hyph_id_ID
	hyph_is_IS
	hyph_it_IT
	hyph_lt_LT
	hyph_nl_NL
	hyph_pl_PL
	hyph_pt_PT
	hyph_pt_BR
	hyph_sk_SK
	hyph_sv_SE
	hyph_uk_UA
	hyph_fr_BE
	hyph_sl
	hyph_uk
	thes_cs_CZ
	thes_de_DE
	thes_hu_HU
	bg_BG
	ca_ES
	da_DK
	de_AT
	de_CH
	de_DE_comb
	el_GR
	en_AU
	en_CA
	en_NZ
	es_ES
	es_MX
	fr_BE
	fr_FR
	hr_HR
	hu_HU
	la
	nb_NO
	nl_NL
	nn_NO
	pl_PL
	pt_BR
	pt_PT
	ru_RU
	sk_SK
	sl_SI
	sv_SE
	uk_UA"

for DICT in $DICTS ; do
  if test -f $SRCDIR/$DICT.tar.bz2 ; then
    echo "Unpacking $DICT dictionary..."
    mkdir -m 755 -p $DOCDIR/dictionaries
    TMPDIR=`mktemp -d /tmp/ooo-build.dict.XXXXXX`
    tar -xjf $SRCDIR/$DICT.tar.bz2 -C $TMPDIR || exit 1;
    chmod 644 $TMPDIR/*.* || exit 1;
    
    DICTNAME=${DICT/thes/th}

    test -f $TMPDIR/$DICTNAME.dic && mv $TMPDIR/$DICTNAME.dic $DICTDIR/
    test -f $TMPDIR/$DICTNAME.dat && mv $TMPDIR/$DICTNAME.dat $DICTDIR/
    test -f $TMPDIR/$DICTNAME.idx && mv $TMPDIR/$DICTNAME.idx $DICTDIR/
    test -f $TMPDIR/$DICTNAME.aff && mv $TMPDIR/$DICTNAME.aff $DICTDIR/
    mv $TMPDIR/* $DOCDIR/dictionaries || exit 1;
    rmdir $TMPDIR || exit 1; 
  else
    echo "Warning: $DICT dictionary is not available..."
  fi
done

echo "Creating dictionary.lst..."
cd $DICTDIR
> dictionary.lst

# Dictionaries
for i in *.aff; do
    # does any exist?
    if test "z$i" != 'z*.aff' ; then
	LANGUAGE=`echo $i | cut -b1-2`
	COUNTRY=`echo $i | cut -b4-5`
	FULL=`echo $i | cut -b1-5`

	echo "DICT $LANGUAGE $COUNTRY $FULL" >> dictionary.lst
    fi
done

# Dictionary wordbooks
for i in *.dic; do
    # filter out names with "hyph" in them
    if test "z$i" != 'z*.dic' ; then
        TEMP=`echo $i | cut -b1-4`
        if test "z$TEMP" != "zhyph" ; then
            LANGUAGE=`echo $TEMP | cut -b1-2`
            # See if the file is just la.dic for example
            if test "$i" = "$LANGUAGE.dic" ; then
                COUNTRY="  "
                FULL="$LANGUAGE"
            else
                COUNTRY=`echo $i | cut -b4-5`
                FULL=`echo $i | cut -b1-5`
            fi

            echo "DICT $LANGUAGE $COUNTRY $FULL" >> dictionary.lst
        fi
    fi
done

# Hyphenation tables
for i in hyph*.dic; do
    # does any exist?
    if test "z$i" != 'zhyph*.dic' ; then
        LANGUAGE=`echo $i | cut -b6-7`
        # See if the file is just hyph_la.dic for example
        if test "$i" = "hyph_$LANGUAGE.dic" ; then
            COUNTRY="  "
            FULL="$LANGUAGE"
        else
            COUNTRY=`echo $i | cut -b9-10`
            FULL=`echo $i | cut -b6-10`
        fi

        echo "HYPH $LANGUAGE $COUNTRY $FULL" >> dictionary.lst
    fi
done

# Thesauruses
for i in th*.dat; do
    # does any exist?
    if test "z$i" != 'zth*.dat' ; then
	LANGUAGE=`echo $i | cut -b4-5`
	COUNTRY=`echo $i | cut -b7-8`
	FULL=`echo $i | cut -b1-8`

	echo "THES $LANGUAGE $COUNTRY $FULL" >> dictionary.lst
    fi
done
