#!/bin/sh

# this script finishes the mono installation

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

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

if test -d $OOINSTDIR/basis$VERSION/sdk ; then

    echo "SDK installation clean up"

    # bin potential .orig files
    find $OOINSTDIR/basis$VERSION/sdk -name "*.orig" -exec rm -f {} \;

    # move some SDK directories to the right place according to FHS
    # note that examples must stay in $OOINSTDIR/basis$VERSION/sdk because there are used
    # relative paths to $OOINSTDIR/basis$VERSION/sdk/setting and it does not work via
    # a symlink
    mkdir -p $PREFIX/include
    mkdir -p $DATADIR/idl
    mkdir -p $DATADIR/$OOOINSTALLDIRNAME/sdk
    mkdir -p $DOCDIR/sdk
    mv $OOINSTDIR/basis$VERSION/sdk/include      $PREFIX/include/$OOOINSTALLDIRNAME
    if [ -d $OOINSTDIR/basis$VERSION/sdk/classes ]; then
        mv $OOINSTDIR/basis$VERSION/sdk/classes      $DATADIR/$OOOINSTALLDIRNAME/sdk/classes
    fi
    mv $OOINSTDIR/basis$VERSION/sdk/idl          $DATADIR/idl/$OOOINSTALLDIRNAME
    mv $OOINSTDIR/basis$VERSION/sdk/docs         $DOCDIR/sdk
    mv $OOINSTDIR/basis$VERSION/sdk/share/readme $DOCDIR/sdk/readme
    mv $OOINSTDIR/basis$VERSION/sdk/index.html   $DOCDIR/sdk

    # compat symlinks
    ln -sf $PREFIXBASE/include/$OOOINSTALLDIRNAME      $OOINSTDIR/basis$VERSION/sdk/include
    ln -sf $DATADIRBASE/$OOOINSTALLDIRNAME/sdk/classes $OOINSTDIR/basis$VERSION/sdk/classes
    ln -sf $DATADIRBASE/idl/$OOOINSTALLDIRNAME         $OOINSTDIR/basis$VERSION/sdk/idl
    ln -sf $DOCDIRBASE/sdk/docs                        $OOINSTDIR/basis$VERSION/sdk/
    ln -sf $DOCDIRBASE/sdk/index.html                  $OOINSTDIR/basis$VERSION/sdk/index.html
    ln -sf $OOINSTBASE/basis$VERSION/sdk/examples                    $DOCDIR/sdk/examples

    # fix file list
    sed -e "s|^\(%dir \)\?$OOINSTBASE/basis$VERSION/sdk/include|\1$PREFIXBASE/include/$OOOINSTALLDIRNAME|" \
	-e "s|^\(%dir \)\?$OOINSTBASE/basis$VERSION/sdk/classes|\1$DATADIRBASE/$OOOINSTALLDIRNAME/sdk/classes|" \
	-e "s|^\(%dir \)\?$OOINSTBASE/basis$VERSION/sdk/idl|\1$DATADIRBASE/idl/$OOOINSTALLDIRNAME|" \
	-e "s|^\(%dir \)\?$OOINSTBASE/basis$VERSION/sdk/docs|\1$DOCDIRBASE/sdk/docs|" \
	-e "s|^\(%dir \)\?$OOINSTBASE/basis$VERSION/sdk/share/readme|\1$DOCDIRBASE/sdk/readme|" \
	-e "s|^$OOINSTBASE/basis$VERSION/sdk/index.html$|$DOCDIRBASE/sdk/index.html|" \
	-e "s|^\(%dir \)\?$OOINSTBASE/basis$VERSION/sdk/share.*$||" \
	-e "/\.orig$/D" \
	-e "/^$/D" \
	$OODESTDIR/gid_Module_Root_SDK | sort -u \
	>$OODESTDIR/gid_Module_Root_SDK.new
    mv $OODESTDIR/gid_Module_Root_SDK.new $OODESTDIR/gid_Module_Root_SDK
    #
    echo "%dir $DATADIRBASE/$OOOINSTALLDIRNAME/sdk"    >>$OODESTDIR/gid_Module_Root_SDK
    echo "%dir $DATADIRBASE/$OOOINSTALLDIRNAME"        >>$OODESTDIR/gid_Module_Root_SDK
    echo "%dir $DATADIRBASE/idl"                       >>$OODESTDIR/gid_Module_Root_SDK
    echo "%dir $DOCDIRBASE/sdk/docs"                   >>$OODESTDIR/gid_Module_Root_SDK
    echo "%dir $DOCDIRBASE/sdk"                        >>$OODESTDIR/gid_Module_Root_SDK
    echo "%dir $DOCDIRBASE"                            >>$OODESTDIR/gid_Module_Root_SDK
    echo "$OOINSTBASE/basis$VERSION/sdk/include"     >>$OODESTDIR/gid_Module_Root_SDK
    echo "$OOINSTBASE/basis$VERSION/sdk/classes"     >>$OODESTDIR/gid_Module_Root_SDK
    echo "$OOINSTBASE/basis$VERSION/sdk/idl"         >>$OODESTDIR/gid_Module_Root_SDK
    echo "$OOINSTBASE/basis$VERSION/sdk/docs"        >>$OODESTDIR/gid_Module_Root_SDK
    echo "$OOINSTBASE/basis$VERSION/sdk/index.html"  >>$OODESTDIR/gid_Module_Root_SDK
    echo "$DOCDIRBASE/sdk/examples"    >>$OODESTDIR/gid_Module_Root_SDK

    # generate default profiles
    for file in setsdkenv_unix.csh setsdkenv_unix.sh ; do
        sed -e "s,@OO_SDK_NAME@,openoffice.org${VERSION}_sdk," \
	    -e "s,@OO_SDK_HOME@,$OOINSTBASE/basis$VERSION/sdk," \
	    -e "s,@OFFICE_HOME@,$OOINSTBASE," \
	    -e "s,@OFFICE_BASE_HOME@,$OOINSTBASE/basis$VERSION," \
	    -e "s,@OO_SDK_URE_HOME@,$OOINSTBASE/basis$VERSION/ure-link," \
	    -e "s,@OO_SDK_MAKE_HOME@,/usr/bin," \
	    -e "s,@OO_SDK_ZIP_HOME@,/usr/bin," \
	    -e "s,@OO_SDK_CPP_HOME@,/usr/bin," \
	    -e "s,@OO_SDK_CC_55_OR_HIGHER@,," \
	    -e "s,@OO_SDK_JAVA_HOME@,$JAVA_HOME," \
	    -e "s,@OO_SDK_OUTPUT_DIR@,\$HOME," \
	    -e "s,@SDK_AUTO_DEPLOYMENT@,NO," \
            $OOINSTDIR/basis$VERSION/sdk/$file.in \
	    > $OOINSTDIR/basis$VERSION/sdk/$file
	chmod 755 $OOINSTDIR/basis$VERSION/sdk/$file
	echo $OOINSTBASE/basis$VERSION/sdk/$file >>$OODESTDIR/gid_Module_Root_SDK
    done
    
    # FIXME: I rather set this file to be non-world-writttable for now, i#64812
    chmod go-w $OOINSTDIR/basis$VERSION/sdk/settings/component.uno.map
fi
