#!/bin/bash

piece=$1
ooo_build_tag=$2

source $OO_TOOLSDIR/piece/sys-setup

# this script saves the architecture independent files back into solver
# they could be installed later in the noarch RPMs

# the file lists are needed
test -n "$DESTDIR" || exit 0;

# you should use --enable-build-noarch configure switch to enable this feature
test "$OOO_BUILD_NOARCH" = 'YES' || exit 0;

DEST="$DESTDIR$OO_SOLVERDIR/noarch"

# help need not be saved; it is built directly in noarch package
# will be moved to /usr/share in file-list-help
test "$piece" = "help" && exit 0;

# HACK: the lyt-* layout tamplates are available at
# http://extensions.services.openoffice.org/project/TemplatePack_II
# they are not really localized
# we do not want them copied into all l10n packageswe,definitely
# For example, they are already included in the separate package
# OpenOffice_org-templates-presentation-layouts on OpenSUSE
# FIXME: make me it optional?; allow move them to the common dir?
for list in $DESTDIR/gid_Module_Langpack_Basis_* ; do
    test -f $list || continue;
    for file in `sort -u $list | grep "$OO_INSTDIR/basis$VERSION/share/template/[-_[:alnum:]]*/layout/lyt-.*$"` ; do
	echo "Warning: removing unwanted: $file"
	rm $DESTDIR$file
    done
    sed "s|$OO_INSTDIR/basis$VERSION/share/template/[-_[:alnum:]]*/layout/lyt-.*$||" $list >$list.new
    mv $list.new $list
done

echo "Saving noarch stuff back into solver ..."

# always create the top dir, so we could put it into the package even when
# just en-US is being built
mkdir -p $DEST

# lang packs are noarch
for list in  $DESTDIR/gid_Module_Langpack* $DESTDIR/gid_Module_Helppack* ; do
    test -f $list || continue;
    # we want to keep the en_US localization in the package
    echo $list | grep "en_US" && continue;
    for source in `grep -v "^%dir" $list | sort -ru` ; do
	if test "$OOO_BUILD_NOARCH" = 'YES' ; then
	    target=`echo "$source" | sed "s|$OO_INSTDIR|$OO_INSTDIR_SHARE|"`
	else
	    target="$source"
	fi
	source_dir=`dirname $source`
	target_dir=`dirname $target`
	mkdir -p "$DEST$target_dir"
	mv "$DESTDIR$source" "$DEST$target_dir"
	rmdir --ignore-fail-on-non-empty "$DESTDIR$source_dir"
    done
    # we need to save the list of files with the piece-specific name
    listname_piece=`echo $list | sed -e "s|\(gid_Module_Langpack\)|\1_$piece|" -e "s|\(gid_Module_Helppack\)|\1_$piece|"`
    listname_piece=`basename $listname_piece`
    if test "$OOO_BUILD_NOARCH" = 'YES' ; then
	sed "s|$OO_INSTDIR|$OO_INSTDIR_SHARE|" $list >$DEST/$listname_piece
	rm $list
    else	
	mv $list $DEST/$listname_piece
    fi
done

# icon themes are noarch but they could be keep as is in the separate package
# and just repacked later, see $OO_TOOLSDIR/piece/file-list-postprocess
