#!/bin/bash

#
# See setup for user tweakables.
#
if test -n "$OO_TOOLSDIR" ; then
    # split build
    . $OO_TOOLSDIR/piece/sys-setup
    . $OO_TOOLSDIR/setup
    OOBUILDDIR=`pwd`
    SRCDIR=$OO_TOOLSDIR/../src
    SRCDIR_PIECE=$OOBUILDDIR/ooo-build/sdf
    TOOLSDIR=$OO_TOOLSDIR/..
    OOO_LANGS_LIST=`echo $OO_LANGS | sed -e "s|ALL|$ALL_LANGS|g"`
    split_build=yes
else
    . ./setup
    . $OOBUILDDIR/*.[sS]et.sh
    . ./setup
    SRCDIR_PIECE=
    split_build=no
fi

export LC_ALL='C';

# do nothing for en-US only build
test "$OOO_LANGS_LIST" = "en-US" && exit 0;

merge_localization()
{
    sdf_file="$1"
    langcode="$2"

    # we do not want en-US strings
    sdf_file_filtered=`mktemp /tmp/localize-ooo-sdf-filtered-XXXXXX`
    awk -F "\t" '{ if ($10 != "en-US") print }' "$sdf_file" >"$sdf_file_filtered"

    # merge the localization
    if test `wc -l "$sdf_file_filtered" | cut -d " " -f 1` -eq 0 ; then
	echo "Warning: Ignoring empty extra localization $(basename $sdf_file)..."
    else
	echo "Merging extra localization $(basename $sdf_file)..."
	localize -m -x -o -l "$langcode" -f "$sdf_file_filtered" || exit 1;
	echo ""
    fi

    rm "$sdf_file_filtered"
}

# fix broken image in help, i#99165
for lang in $OOO_LANGS_LIST; do
    if [ -d $OOBUILDDIR/default_images/res/commandimagelist/$lang/ ]
    then
        if [ ! -f $OOBUILDDIR/default_images/res/commandimagelist/$lang/sc_label.png ]
        then
            cp $OOBUILDDIR/default_images/res/commandimagelist/sc_label.png $OOBUILDDIR/default_images/res/commandimagelist/$lang/
        fi
    fi
done

# build the localize tool if needed (only in the non-split build)
if test "$split_build" != "yes" && ! localize -h >/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..."
    if [ -d $OOBUILDDIR/l10n ]; then
	find $OOBUILDDIR/l10n -type f -name "localize.sdf" -exec chmod 644 {} \;
    fi
fi

# stamp files
if test "$split_build" = "yes" ; then
    stamp_dir=$OOBUILDDIR/ooo-build/stamp
    mkdir -p $stamp_dir
else
    stamp_dir=$TOOLSDIR/stamp
fi
# already applied sdf files
extra_trans_stamp=$stamp_dir/build.extra.translations
# lang-specific sdf files
extra_trans_lang_list=$stamp_dir/build.extra.translations.lang.list

test "$VENDORNAME" = "OxygenOffice" && rm -rf "$extra_trans_stamp" "$extra_trans_lang_list" && echo "$VENDORNAME distro rebuilds translations every time."

# call grep only once to speed up the search from 22s to 2s
all_langs_pattern=`mktemp /tmp/localize-ooo-XXXXXX`
for lang in $ALL_LANGS ; do
    echo "$lang.sdf\$" >>$all_langs_pattern
done

# lang-specific sdf files
# it looks for files "any-name-<lang>.sdf", e.g. "gallery-hu.sdf"
# it ignores files below $SRCDIR/clone (upstream sources available when --with-git is used)
hungarian_updated=
for sdf_file in `find $SRCDIR $DEB_GSIDIR $TOOLSDIR/po $SRCDIR_PIECE -path $SRCDIR/clone -prune -o -name "*.sdf"` ; do

    # check if it is lang-specific file
    echo "$sdf_file" | grep -q -f "$all_langs_pattern" || continue;

    # mention it in specific sdf file list
    grep -q "^$sdf_file$" $extra_trans_lang_list 2>/dev/null || echo $sdf_file >>$extra_trans_lang_list

    # skip this localization if it is already merged
    grep -q "^$sdf_file$" $extra_trans_stamp 2>/dev/null && continue;

    # find if this localization is required
    langcode=
    for lang in $OOO_LANGS_LIST ; do
	echo "$sdf_file" | grep -q "$lang.sdf$" && langcode=$lang
    done
    test -z "$langcode" && continue;

    merge_localization "$sdf_file" "$langcode"

    # workaround for i#56622, n#210797
    test "$langcode" = "hu" && hungarian_updated=yes

    # copy help auxiliary files if they are missing but the localized strings are available
    if test -d "$OOBUILDDIR/helpcontent2" ; then
	if grep -q "^helpcontent2" $sdf_file &&
	    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
    fi

    # make stamp for this localization
    echo "$sdf_file" >>$extra_trans_stamp
done

sdf_langpack=`mktemp /tmp/langpack-ooo-sdf-XXXXXX`
echo -n "Generating langpack description from translation: "
echo -n "" > langpack.sdf
for sdf_file in `find $SRCDIR/clone/l10n/l10n/source -name "*.sdf" | sort` ; do
    echo -n "."
    grep -E "STR_...._MODULE_LANGPACK_" $sdf_file >> $sdf_langpack
done
sed -e 's/source\\ooo\\module_langpack\.ulf/source\\accessories\\module_samples_accessories\.ulf/g' $sdf_langpack > $SRCDIR/accessories-samples-langpack.sdf
sed -e 's/source\\ooo\\module_langpack\.ulf/source\\accessories\\module_templates_accessories\.ulf/g' $sdf_langpack > $SRCDIR/accessories-templates-langpack.sdf
sed -e 's/source\\ooo\\module_langpack\.ulf/source\\extensions\\module_extensions_sun_templates\.ulf/g' $sdf_langpack > $SRCDIR/extensions-sun-templates-langpack.sdf
sed -e 's/source\\ooo\\module_langpack\.ulf/source\\extensions\\module_extensions_lightproof\.ulf/g' $sdf_langpack > $SRCDIR/extensions-lightproof-langpack.sdf
rm $sdf_langpack
echo "done!"

echo "Processing non language specific files:"
# apply lang-non-specific sdf files
# the files "any-name-<lang>.sdf" are ignored because they are already processed above
# it ignores files below $SRCDIR/clone (upstream sources available when --with-git is used)
for fn in `find $SRCDIR $DEB_GSIDIR $TOOLSDIR/po $SRCDIR_PIECE -path $SRCDIR/clone -prune -o -name "*.sdf"` ; do
    [ -f "$fn" ] || continue
    # skip if already processed as lang-specific files
    grep -q "^$fn$" $extra_trans_lang_list 2>/dev/null && continue;
    # skip if already merged
    grep -q "^$fn$" $extra_trans_stamp 2>/dev/null && continue;
    # skip sdf-templates
    echo "$fn" | grep -q "^$TOOLSDIR/po/sdf-templates" && continue;
    echo "$fn" | grep -q "^$TOOLSDIR/po/ooo-build.sdf" && continue;
	# skip transex3/workbench files, they are not real sdf files
	echo "$fn" | grep -q "^$SRCDIR/clone/libs-gui/transex3/workbench/" && continue;
	echo "$fn" | grep -q "^$SRCDIR/clone/libs-gui/transex3/layout/" && continue;
	# skip cloned files
	echo "$fn" | grep -q "^$SRCDIR/clone/l10n/l10n/source/" && continue;
    # skip exotic localizations that are not in $ALL_LANGS and thus not detected
    echo "$fn" | grep -q "$SRCDIR/GSI" && ! echo $OOO_LANGS | grep -q "$(echo $(basename $fn) | cut -d'_' -f2)" && continue;
    echo "$fn" | grep -q "$TOOLSDIR/po/ooo-build" && ! echo $OOO_LANGS | grep -q "$(echo $(basename $fn) | cut -d'-' -f3)" && continue;
    merge_localization "$fn" "all" || 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 additional localization $(basename $fn) ..."
	merge_localization "$fn" "all" || 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
	    merge_localization "$gsi_temp" "en-GB"
	    echo "GSI_en-GB-en-US.sdf" >>$extra_trans_stamp
	    rm -f $gsi_temp
	    break;
	fi
    done
fi

# remove temporary files
rm $all_langs_pattern

echo "Localizations updated ...!"

exit 0;
