#!/bin/sh

# this script finishes the mono installation

#
# See setup for user tweakables.
#
if test -n "$OO_TOOLSDIR" ; then
    # split build
    . $OO_TOOLSDIR/setup
    TOOLSDIR=$OO_TOOLSDIR/..
    SRCDIR=$OO_TOOLSDIR/../src
    split_build=yes
else
    . ./setup
    split_build=
fi

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

# continue only when mono has been built
test -f $OOINSTDIR/basis$VERSION/program/cli_oootypes.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
if test "$split_build" = "yes" ; then
    MONOGACFILELIST=files-mono.txt
else
    MONOGACFILELIST=$BUILDDIR/mono_gac
fi
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" \
    $TOOLSDIR/src/mono-ooo.pc.in >${OODESTDIR}${LIBDIRBASE}/pkgconfig/mono-$OOOINSTALLDIRNAME.pc
test "$split_build" = "yes" && echo ${LIBDIRBASE}/pkgconfig/mono-$OOOINSTALLDIRNAME.pc >>$MONOGACFILELIST

# extra dlls config files
for dll in cli_uno_bridge.dll ; do
    sed -e "s|@LIBDIRBASE@|$LIBDIRBASE|g" \
	-e "s|@OOOINSTALLDIRNAME@|$OOOINSTALLDIRNAME|g" \
	$TOOLSDIR/src/$dll.config.in > $OOINSTDIR/ure/lib/$dll.config
    if test -n "$OODESTDIR" ; then
	file_list=`grep "^$OOINSTBASE/ure/lib/$dll$" $OODESTDIR/gid* | head -n 1 | cut -d: -f1`
	echo "$OOINSTBASE/ure/lib/$dll.config" >>$file_list
    fi
done
	perl -pi -e "s,</configuration>,  <dllmap dll=\"cli_uno_glue\" target=\"$LIBDIRBASE/$OOOINSTALLDIRNAME/basis-link/ure-link/lib/libcli_uno_glue.so\"/>\n</configuration>," \
		$OOINSTDIR/ure/lib/cli_cppuhelper.config

# 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_name in ure/lib/cli_basetypes \
               ure/lib/cli_cppuhelper \
	       ure/lib/cli_uretypes \
	       basis$VERSION/program/cli_oootypes \
	       ure/lib/cli_uno_bridge \
	       ure/lib/cli_ure ; do
	# create .config files with correct names
	test -f $OOINSTDIR/$dll_name.config && mv $OOINSTDIR/$dll_name.config $OOINSTDIR/$dll_name.dll.config
	gacutil $gacutil_root -i $OOINSTDIR/$dll_name.dll -package $OOOINSTALLDIRNAME || exit 0

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

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