#!/bin/bash

piece=$1
ooo_build_tag=$2

source $OO_TOOLSDIR/piece/sys-setup

custom_install="$OO_TOOLSDIR/piece/inst-$piece"
if test -f $custom_install; then
    $custom_install $piece $ooo_build_tag $DISTRO
fi

# ----- monster make_installer section -----

# can't just source $SOLARENV/inc/minor.mk [ sadly ] - missing quotes.
export BUILD=`grep "BUILD=" "$OO_INSTDIR/solenv/inc/minor.mk" | cut -d= -f2`
export LAST_MINOR=`grep "LAST_MINOR=" "$OO_INSTDIR/solenv/inc/minor.mk" | cut -d= -f2`
export OUT="`pwd`/solver/install"
export LOCAL_OUT=$OUT;
export LOCAL_COMMON_OUT=$OUT;
# install from the copy in the solver - to the system
export SOLARVERSION="`pwd`/solver"

if test "z$DESTDIR" == "z"; then
    echo "install-generic requires a destdir to be set"
    exit 1
fi

export LANGS=
for lang in $OO_LANGS ; do
    if test -z "$LANGS" ; then
	LANGS="$lang"
    else
	LANGS="$LANGS,$lang"
    fi
done

if test "$piece" = "sdk" ; then
    PRODUCT=LibreOffice_SDK
else
    PRODUCT=LibreOffice
fi

perl -w $SOLARENV/bin/make_installer.pl \
     -f $OO_INSTDIR/solver/instsetoo_native/util/openoffice.lst \
     -l $LANGS -p $PRODUCT -buildid $BUILD -destdir $DESTDIR \
     -dontstrip -simple $OO_INSTDIR

# ----- end monster make_installer section -----

# Now try to copy the bits we didn't install into a -devel RPM ...

SRCDIR="$SOLARPIECEVERSION/$INPATH"
DEST="$DESTDIR$OO_SOLVERDIR"
echo "Copy / install remaining devel pieces ..."
filelist="$DESTDIR/all_filelist.txt"
touch $filelist
$OO_TOOLSDIR/piece/copyexcept -x $filelist $SRCDIR/* $DEST
rm -f $filelist

# FIXME: we need to provide external files also in other pieces (e.g. prebuild mono.dlls)
#        external is automatically taken into build dependencies even when removed from
#        the dirs list in build-generic
#        => the build.lst and deliver.log must be deleted in other pieces to avoid conflict of devel packages
if test "$piece" != "libs-extern" -a "$piece" != "libs_extern" ; then
    rm -f $DEST/inc/external/build.lst
    rm -f $DEST/inc/external/deliver.log
fi

# hotfix post upstream install
# allows to modify the install process from the piece package sources
hotfix_post_upstream_install="ooo-build/bin/post-upstream-install"
if test -f "$hotfix_post_upstream_install" ; then
    "$hotfix_post_upstream_install" $piece $ooo_build_tag $DISTRO || exit 1;
fi


# hack to install the noarch files later in the noarch RPMs
if test -n "$DESTDIR" -a "$OOO_BUILD_NOARCH" = 'YES' ; then
    $OO_TOOLSDIR/piece/save-noarch $piece $ooo_build_tag || exit 1;
fi

# file lists
if test -n "$DESTDIR" -a "$OO_BUILD_FILE_LISTS_ENABLE" != "NO" ; then
    custom_file_list="$OO_TOOLSDIR/piece/file-list-$piece"
    if test -f $custom_file_list; then
	$custom_file_list $piece $ooo_build_tag $DISTRO || exit 1;
    else
	$OO_TOOLSDIR/piece/merge-file-lists "files-$piece.txt" $DESTDIR/gid_*
    fi
fi

# add executable rights to libraries
echo "Fixing permissions of libraries..."
for libdir in $OO_INSTDIR/ure/lib \
	      $OO_INSTDIR/program \
	      $OO_INSTDIR/basis$VERSION/program ; do
    test -d $DESTDIR$libdir && \
	find $DESTDIR$libdir -name '*.so*' -type f -exec chmod a+x {} \;
done

# clean up solver
echo "Cleaning up solver..."
find $DEST -depth -name ".svn" -type d -exec rm -rf {} \;
find $DEST -name "*.orig" -exec rm -rf {} \;
find $DEST -type f -exec chmod go-w {} \;
find $DEST -name "*.h" -exec chmod a-x {} \;
find $DEST -name "*.hxx" -exec chmod a-x {} \;

# custom post install stuff
custom_post_install="$OO_TOOLSDIR/piece/post-inst-$piece"
if test -f $custom_post_install -a "$OO_BUILD_POST_PROCESS_ENABLE" != "NO" ; then
    $custom_post_install $piece $ooo_build_tag $DISTRO || exit 1;
fi

# hotfix post install; allows to modify the install process from the piece package sources
hotfix_post_install="ooo-build/bin/post-install"
if test -f "$hotfix_post_install" ; then
    "$hotfix_post_install" $piece $ooo_build_tag $DISTRO || exit 1;
fi
