#!/bin/bash

#
# See setup for user tweakables.
#
. ./setup
. $OOBUILDDIR/*.[sS]et.sh
. ./setup

export LC_ALL='C';

if ! which localize 2>/dev/null 2>&1 ; then
    echo 'Building localize stuff at first...'
    cd $OOBUILDDIR/transex3
    perl $SOLARENV/bin/build.pl --all $EXTRA_BUILD_FLAGS $EXTRA_DMAKE_FLAGS || exit 1;
    perl $SOLARENV/bin/deliver.pl || exit 1;
    cd -

    echo "Fixing localize.sdf permissions..."
    find $OOBUILDDIR -type f -name "localize.sdf" -exec chmod 644 {} \;
fi

# What extra localizations are avilable as $SRCDIR/GSI_*.sdf and are not
# already merged?
extra_trans_stamp=$TOOLSDIR/stamp/build.extra.translations
extra_trans=`find $SRCDIR -name "GSI_*.sdf"`
extra_trans_langs=
for file in $extra_trans ; do
    # skip this localization if it is already merged
    grep -q "^$file$" $extra_trans_stamp 2>/dev/null && continue;
    # find if this localization is required
    trans_lang=`echo $file | sed "s|^$SRCDIR/GSI_\(.*\).sdf\$|\1|"`
    for lang in $OOO_LANGS_LIST ; do
	if echo "$lang" | grep -q "$trans_lang" ; then
	    extra_trans_langs="$trans_lang $extra_trans_langs"
	    break;
	fi
    done
done	    

for langcode in $extra_trans_langs ; do
    # merge the available localization
    echo "Merging extra \"$langcode\" localization..."
    $SOLARVERSION/$INPATH/bin/localize -m -x -l $langcode -f $SRCDIR/GSI_$langcode.sdf || exit 1;
    echo ""
    DEB_GSIFILE=$DEB_GSIDIR/GSI_$langcode.sdf
    if test "z$DEB_GSIDIR" != "z" && test -f $DEB_GSIFILE; then
	echo "Merging imported \"$langcode\" localization..."
	$SOLARVERSION/$INPATH/bin/localize -m -x -l $langcode -f $DEB_GSIFILE || exit 1;
	echo ""
    else
	DEB_GSIFILE=
    fi

    # workaround for i#56622, n#210797
    if echo "$langcode" | grep -q "hu" ; then
        echo "Fixing Hungarian strings: OOo vs. SO"
        for file in `find $OOBUILDDIR -type f -name "localize.sdf"` ; do
    	    $TOOLSDIR/bin/a2az.pl $file >$file.a2az
	    mv $file.a2az $file
	done
    fi

    # copy help auxiliary files if they are missing but the localized strings are available
    if grep -q "^helpcontent2" $SRCDIR/GSI_$langcode.sdf $DEB_GSIFILE &&
       test ! -d $OOBUILDDIR/helpcontent2/source/auxiliary/$langcode ; then
        echo "Copying English help auxiliary files for \"$langcode\"..."
	cd $OOBUILDDIR/helpcontent2/source/auxiliary
	cp -r en-US $langcode
	perl -pi -e "s|Language=en-US|Language=$langcode|" $langcode/*.cfg
    fi
    # make stamp for this localization
    echo $SRCDIR/GSI_$langcode.sdf >>$extra_trans_stamp
done

# apply all extra string fixes and updates
for fn in $SRCDIR/sdf/*.sdf; do
    [ -f "$fn" ] || continue
    grep -q "^$fn$" $extra_trans_stamp 2>/dev/null && continue;
    echo "Merging extra localization $(basename $fn) ..."
    $SOLARVERSION/$INPATH/bin/localize -m -x -l all -f $fn || exit 1;
    echo "$fn" >>$extra_trans_stamp
done

if test "z$DEB_GSIDIR" != "z"; then
    for fn in $DEB_GSIDIR/*all*.sdf; do
	[ -f "$fn" ] || continue
	echo "Merging extra localization $(basename $fn) ..."
	$SOLARVERSION/$INPATH/bin/localize -m -x -l all -f $fn || exit 1;
    done
fi

# FIXME: add all missing en-GB strings; it is a workaround for i#66919, n#231678
if ! grep -q "^GSI_en-GB-en-US.sdf$" $extra_trans_stamp 2>/dev/null ; then
    for lang in $OOO_LANGS_LIST ; do
        if test "$lang" = "en-GB" ; then
	    gsi_temp=`mktemp /tmp/build-ooo.XXXXXX`
	    echo "Fixing en-GB localization..."
	    localize -e -l en-US,en-GB=en-US -f $gsi_temp
	    localize -m -x -l en-GB -f $gsi_temp
	    echo "GSI_en-GB-en-US.sdf" >>$extra_trans_stamp
	    rm -f $gsi_temp
	    break;
	fi
    done
fi

echo "Localizations updated ...!"

exit 0;
