#!/bin/sh

#
# See setup for user tweakables.
#
. ./setup

if test "z$1" != "z"; then
    DICTDIR=$1;
else
    DICTDIR=$OOINSTDIR/basis$VERSION/share/dictionaries
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_AU
	hyph_en_CA
	hyph_en_GB
	hyph_en_NZ
	hyph_en_US
	hyph_es_ES
	hyph_es_MX
	hyph_et_EE
	hyph_fi_FI
	hyph_fr_BE
	hyph_fr_FR
	hyph_ga_IE
	hyph_hu_HU
	hyph_hr_HR
	hyph_id_ID
	hyph_is_IS
	hyph_it_IT
	hyph_lt_LT
	hyph_lv_LV
	hyph_nb_NO
	hyph_nl_NL
	hyph_nn_NO
	hyph_pl_PL
	hyph_pt_BR
	hyph_pt_PT
	hyph_ro_RO
	hyph_ru_RU
	hyph_sk_SK
	hyph_sl
	hyph_sl_SI
	hyph_sv_SE
	hyph_uk
	hyph_uk_UA
	thes_bg_BG
	thes_bg_BG_v2
	thes_cs_CZ
	thes_cs_CZ_v2
	thes_de_DE
	thes_de_DE_v2
	thes_en_US
	thes_en_US_v2
	thes_es_ES
	thes_es_ES_v2
	thes_fr_FR
	thes_fr_FR_v2
	thes_it_IT
	thes_hu_HU
	thes_nb_NO_v2
	thes_pl_PL
	thes_pl_PL_v2
	thes_pt_PT_v2
	thes_ru_RU
	thes_sk_SK
	thes_sk_SK_v2
	af_ZA
	bg_BG
	ca_ES
	cs_CZ
	da_DK
	de_AT
	de_CH
	de_DE_comb
	el_GR
	en_AU
	en_CA
	en_GB
	en_NZ
	en_US
	eo_EO
	es_ES
	es_MX
	et_EE
	fi_FI
	fo_FO
	fr_BE
	fr_FR
	ga_IE
	gd_GB
	gl_ES
	he_IL
	hr_HR
	hu_HU
	it_IT
	ku_TR
	la
	lt_LT
	lv_LV
	mg_MG
	mi_NZ
	ms_MY
	nb_NO
	nl_NL
	nn_NO
	ny_MW
	pl_PL
	pt_BR
	pt_PT
	ro_RO
	ru_RU
	rw_RW
	sk_SK
	sl_SI
	sv_SE
	sw_KE
	tl_PH
	tn_ZA
	uk_UA
	zu_ZA"

install_dic()
{
    test -f $1/$2 && mv $1/$2 $DICTDIR/ && echo "$DICTDIRBASE/$2" >>$DICTFILELIST && dic_installed=true 
}

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/$3 || exit 1;
	echo "$DOCDIRBASE/dictionaries/$3/$2" >>$DICTFILELIST
	doc_installed=true
	locale_doc_installed=true
    fi
}

#file list
if test "z$OODESTDIR" != "z" ; then
    DICTFILELIST="$BUILDDIR/dictionaries"
    rm -f $DICTFILELIST
else
    DICTFILELIST=/dev/null
fi

dic_installed=false
doc_installed=false
for DICT in $DICTS ; do
  if test -f $SRCDIR/$DICT.tar.bz2 ; then
    echo "Unpacking $DICT dictionary..."
    mkdir -m 755 -p $DICTDIR
    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=$(echo $DICT | sed s/thes/th/)
    install_dic $TMPDIR $DICTNAME.dic
    install_dic $TMPDIR $DICTNAME.dat
    install_dic $TMPDIR $DICTNAME.idx
    install_dic $TMPDIR $DICTNAME.aff

    # documentation
    LOCALE=$(echo $DICT | sed 's/hyph_//;s/thes_//' | cut -d_ -f1-2)
    mkdir -m 755 -p $DOCDIR/dictionaries/$LOCALE
    locale_doc_installed=false
    for file in `ls $TMPDIR` ; do
	install_doc $TMPDIR $file $LOCALE
    done
    if $locale_doc_installed ; then
	echo "%dir $DOCDIRBASE/dictionaries/$LOCALE" 	>>$DICTFILELIST
    fi
    rmdir $TMPDIR || exit 1; 
  else
    echo "Warning: $DICT dictionary is not available..."
  fi
done

if $dic_installed ; then
    echo "%dir $DICTDIRBASE"	      		>>$DICTFILELIST
fi

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

#echo "Creating dictionary.lst..."
#$TOOLSDIR/bin/install-dict.in $DICTDIR
#test -f $DICTDIR/dictionary.lst && echo "$DICTDIRBASE/dictionary.lst" >>$DICTFILELIST
