#!/bin/sh

# this script finishes the mono installation

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

# check if install path redefined
test -n "$1" && OOINSTDIR="$1"

# continue only when mono has been built
test -f $OOINSTDIR/program/cli_basetypes.dll || exit 0;


echo "Finishing the mono installation..."

if ! test -w "$OODESTDIR/" ; then
    echo "Warning: You do not have rights to finish the mono installation"
    echo "         => skipping it"
    exit 0;	   
fi

# filelist for the GAC stuff
MONOGACFILELIST=$BUILDDIR/mono_gac
rm -f $MONOGACFILELIST

# install the pkgconfig file
# GAC is in /usr/lib even on 64-bit architectures, so the .pc file points to
# the /usr/lib paths; the .pc file itself must be in the arch dependant path
# to be found in PKG_CONFIG_PATH, though
mkdir -p $OODESTDIR${LIBDIRBASE}/pkgconfig/
sed -e "s|@OOOINSTALLDIRNAME@|$OOOINSTALLDIRNAME|g" \
    $SRCDIR/mono-ooo.pc.in >${OODESTDIR}${LIBDIRBASE}/pkgconfig/mono-$OOOINSTALLDIRNAME.pc

# extra dlls config files
for dll in cli_cppuhelper.dll cli_uno_bridge.dll ; do
    sed -e "s|@LIBDIRBASE@|$LIBDIRBASE|g" \
	-e "s|@OOOINSTALLDIRNAME@|$OOOINSTALLDIRNAME|g" \
	$SRCDIR/$dll.config.in >$OOINSTDIR/program/$dll.config
    if test -n "$OODESTDIR" ; then
	file_list=`grep "^$OOINSTBASE/program/$dll$" $OODESTDIR/gid* | head -n 1 | cut -d: -f1`
	echo "$OOINSTBASE/program/$dll.config" >>$file_list
    fi
done

# install versioned assemblies into the system Global Assembly Cache  (GAC)
# to  become  part  of  the assemblies that are available for all applications
# at runtime.
if test -z "$OODESTDIR" -o -n "$MONO_GAC_ROOT" ; then
    test -n "$MONO_GAC_ROOT" && gacutil_root="-root $OODESTDIR$MONO_GAC_ROOT" || gacutil_root=
    for dll in cli_basetypes.dll \
               cli_cppuhelper.dll \
	       cli_types.dll \
	       cli_uno_bridge.dll \
	       cli_ure.dll ; do
	gacutil $gacutil_root -i $OOINSTDIR/program/$dll -package $OOOINSTALLDIRNAME || exit 0

	# the original fixes are not longer needed
	rm -f $OOINSTDIR/program/$dll
	rm -f $OOINSTDIR/program/$dll.config

	# remove the deleted fixes from the filelist
	if test -n "$OODESTDIR" ; then
	    file_list=`grep "^$OOINSTBASE/program/$dll$" $OODESTDIR/gid* | head -n 1 | cut -d: -f1`
	    test -z "$file_list" && echo "Error: \"$OOINSTBASE/program/$dll\" has not found in any filelist" && exit 1;
	    sed -e "s|^$OOINSTBASE/program/$dll$||" \
	        -e "s|^$OOINSTBASE/program/$dll.config$||" \
		$file_list >$file_list.mono
	    mv $file_list.mono $file_list
	fi
    done

    # filelist for the GAC
    if test -n "$OODESTDIR" ; then
        for dir in `find $OODESTDIR/usr/lib/mono -type d -regex ".*/cli_[_a-z]*" -o -regex ".*/ooo-2.0"` ; do
	    find $dir -type d | sed "s|^$OODESTDIR\(.*\)|%dir \1|" >>$MONOGACFILELIST
	    find $dir -type f -o -type l | sed "s|^$OODESTDIR\(.*\)|\1|" >>$MONOGACFILELIST
	done
	sort $MONOGACFILELIST >$MONOGACFILELIST.mono
	mv $MONOGACFILELIST.mono $MONOGACFILELIST
    fi
fi
