#!/bin/sh


if test "z$1" != "z"; then
    DICTDIR=$1;
else
    DICTDIR=$OOINSTDIR/share/dict/ooo
fi

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

DICTDIRBASE=`echo $DICTDIR | sed "s|^$OODESTDIR||"`

# the available dictionaries
DICTS="hyph_bg_BG
	hyph_cs_CZ
	hyph_da_DK
	hyph_de_CH
	hyph_de_DE
	hyph_el_GR
	hyph_en_CA
	hyph_en_GB
	hyph_en_US
	hyph_es_ES
	hyph_fi_FI
	hyph_fr_BE
	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_ru_RU
	hyph_sk_SK
	hyph_sl
	hyph_sv_SE
	hyph_uk
	hyph_uk_UA
	thes_bg_BG
	thes_cs_CZ
	thes_de_DE
	thes_en_US
	thes_es_ES
	thes_fr_FR
	thes_hu_HU
	thes_pl_PL
	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"

install_dic()
{
    test -f $1/$2 && mv $1/$2 $DICTDIR/ && echo "$DICTDIRBASE/$2" >>$BUILDDIR/dictionaries
}

install_doc()
{
    if test -f $1/$2 ; then
	# recode to UTF-8 if needed
	coding=`LC_CTYPE=$3 locale -k LC_CTYPE | grep charmap | sed "s|charmap=\"\(.*\)\"\$|\1|"`
	recode utf8..utf16 <$1/$2 >/dev/null 2>&1 || recode $coding..utf8 $1/$2
	# install
	mv $1/$2 $DOCDIR/dictionaries || exit 1;
	echo "$DOCDIRBASE/dictionaries/$2" >>$BUILDDIR/dictionaries
	doc_installed=true
    fi
}

# remove the old file list
rm -f $BUILDDIR/dictionaries

doc_installed=false
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;
    
    # dictionaries
    DICTNAME=${DICT/thes/th}
    install_dic $TMPDIR $DICTNAME.dic
    install_dic $TMPDIR $DICTNAME.dat
    install_dic $TMPDIR $DICTNAME.idx
    install_dic $TMPDIR $DICTNAME.aff

    # documentation
    LOCALE=${DICT/hyph_/}
    LOCALE=${LOCALE/thes_/}
    LOCALE=`echo $LOCALE | cut -d_ -f1-2`
    for file in `ls $TMPDIR` ; do
	install_doc $TMPDIR $file $LOCALE
    done
    rmdir $TMPDIR || exit 1; 
  else
    echo "Warning: $DICT dictionary is not available..."
  fi
done

if $doc_installed ; then
    echo "%dir $DOCDIRBASE"	      		>>$BUILDDIR/dictionaries
    echo "%dir $DOCDIRBASE/dictionaries" 	>>$BUILDDIR/dictionaries
fi

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

test -f $DICTDIR/dictionary.lst && echo "$DICTDIRBASE/dictionary.lst" >>$BUILDDIR/dictionaries
