#!/bin/sh

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

# configure

#  dmake security patch
cd $OOBUILDDIR/dmake
autoconf || exit 1;

#  main configure
cd $OOBUILDDIR/config_office
rm -f config.cache
echo "configuring ...";
autoconf || exit 1;

./configure $CONFIGURE_OPTIONS || exit 1;

# Final build preparation

# tcsh sucks great rocks, and refuses to re-direct it's output otherwise
export TERM=
# Many Java files have 8bit char-set comments, javac barfs on them in utf8 locales
export LANG="C";

if test "z$ENABLE_JAVA" = "zyes"; then
	# Many Java's can't cope with the NPTL on Linux.
	LD_ASSUME_KERNEL=2.2.5 /bin/true 2> /dev/null || LD_ASSUME_KERNEL=2.4.10 /bin/true 2> /dev/null || LD_ASSUME_KERNEL=2.6.0 2> /dev/null || unset LD_ASSUME_KERNEL
	export LD_ASSUME_KERNEL
fi
# Don't do a massive painful install
export BUILD_MINIMAL="01";
# Embedded python dies without Home set
if test "z$HOME" = "z"; then
    export HOME="";
fi
cd $OOBUILDDIR
# Import all of the OO.o environment
if test "z$DISTRO" = "zWin32"; then
    . $OOBUILDDIR/*.set.sh
else
    . $OOBUILDDIR/*.Set.sh
fi
# Copy in missing libraries if we just built them
if test "z$SYSTEM_GCC" = "z"; then
    echo "Copying libraries to $SOLARVER/$UPD/$INPATH/lib";
    mkdir -p $SOLARVER/$UPD/$INPATH/lib || exit 1;
    cp -avf $BUILDDIR/lib/libgcc* $BUILDDIR/lib/libstdc* $SOLARVER/$UPD/$INPATH/lib || exit 1;
    cp -vf $BUILDDIR/lib/libstdc++* $SOLARVER/$UPD/$INPATH/lib/ || exit 1;
fi
echo 'Verifying environment'
echo "Path:  \'$PATH\'"
echo "Shell: \'$SHELL\'"
echo "Lang:  \'$LANGS\'"
if test "z$DISTRO" != "zWin32"; then
    echo "Gcc: "
    gcc -dumpversion
fi
bison --version
flex --version
echo 'Bootstrapping'
./bootstrap || ./bootstrap || ./bootstrap || exit 1;
echo 'Commencing main build'
cd instsetoo || exit 1;
EXTRA_BUILD_FLAGS=
if test $BUILD_NCPUS -gt 1; then
    EXTRA_BUILD_FLAGS="-P$BUILD_NCPUS"
fi
if test "z$BUILD_WIN32" == "z"; then
    EXTRA_BUILD_FLAGS="--dlv_switch link $EXTRA_BUILD_FLAGS"
fi

# Automake exports MAKEFLAGS= --unix or something
# similar that gives child nmake's pain.
unset MAKEFLAGS

# Accelerate straight-through compiles by not building / using dependencies
export nodep=1
echo "Env:"
set
perl $SOLARENV/bin/build.pl --all $EXTRA_BUILD_FLAGS || exit 1;


# build vcl module for the non-default widget sets if any
if test "${OOO_WIDGET_SETS/*,*/comma}" = "comma" ; then

    echo 'Builiding support for the non-default native widget sets'

    # save the old build if any
    vcl_build_old=""
    if test -d $OOBUILDDIR/vcl/$INPATH ; then
      vcl_build_old=`mktemp -d $OOBUILDDIR/vcl/$INPATH.XXXXXX`
      mv $OOBUILDDIR/vcl/$INPATH $vcl_build_old || exit 1;
    fi
    
    # build modules if needed
    IFS_saved="$IFS"
    IFS=","
    for widgetset in ${OOO_WIDGET_SETS} ; do
        if ! test -f $OOBUILDDIR/vcl/$INPATH.$widgetset/lib/libvcl* ; then
	    # configure
	    IFS="$IFS_saved"
	    cd $OOBUILDDIR/config_office
	    ./configure $CONFIGURE_OPTIONS --with-widgetset="${widgetset/original/}" || exit 1;

	    # import the updated OO.o environment
	    if test "z$DISTRO" = "zWin32"; then
		. $OOBUILDDIR/*.set.sh
	    else
		. $OOBUILDDIR/*.Set.sh
	    fi

	    # build the vcl module
	    cd $OOBUILDDIR/vcl
	    build || exit 1;

	    # save the new build
	    rm -rf $INPATH.$widgetset || exit 1;
	    mv $INPATH $INPATH.$widgetset || exit 1;
	    IFS=","
	fi
    done
    IFS="$IFS_saved"

    # restore the original configuration
    cd $OOBUILDDIR/config_office
    ./configure $CONFIGURE_OPTIONS || exit 1;
    # import the updated OO.o environment
    if test "z$DISTRO" = "zWin32"; then
        . $OOBUILDDIR/*.set.sh
    else
        . $OOBUILDDIR/*.Set.sh
    fi

    # restore the saved build if any
    if test "z$vcl_build_old" != "z" ; then
      mv $vcl_build_old/$INPATH $OOBUILDDIR/vcl/ || exit 1;
      rmdir $vcl_build_old/
    fi
fi
    
echo "Build succeeded ...!"
exit 0;
