#!/bin/sh

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

if test "z$BUILD_WIN32" = "z"; then
    if ! test -f /proc/cpuinfo; then
        echo "Looks like proc isn't mounted - this means almost certain"
        echo "Java related weird build failure: please check /proc"
        exit 1;
    fi
fi

# configure

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

echo "Env for configure:"
set
echo "Env for configure ends"

eval ./configure "$CONFIGURE_OPTIONS" "$PROPAGATED_ARGS" || 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 LC_ALL='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
# Embedded python dies without Home set
if test "z$HOME" = "z"; then
    export HOME="";
fi
cd $OOBUILDDIR

echo 'Importing Environment'
# Import all of the OO.o environment
if test "z$BUILD_WIN32" = "zyes"; then
    . $OOBUILDDIR/*.set.sh
else
    . $OOBUILDDIR/*.Set.sh
fi

# Check to see if dmake was not there - if not, this is a from-pristine build
echo "Build: z$BUILD_DMAKE == zYES -a ! -f $SOLARENV/$OUTPATH/bin/dmake";
if test "z$BUILD_DMAKE" == "zYES" -a ! -f "$SOLARENV/$OUTPATH/bin/dmake"; then
    echo "A very clean straight-through build - deferring dep generation"
    if test "z$BUILD_WIN32" != "zyes"; then
	# Accelerate straight-through compiles by not building / using dependencies
	export nodep=1
    else
	echo "Building with full dependencies - for win32"
    fi
else
    echo "Building with full dependencies"
fi

echo 'Bootstrapping'
./bootstrap || ./bootstrap || ./bootstrap || exit 1;

echo 'Verifying environment'
echo "Path:  \'$PATH\'"
echo "Shell: \'$SHELL\'"
echo "Lang:  \'$OOO_LANGS\'"
if test "z$BUILD_WIN32" != "zyes"; then
    echo "Gcc: "
    gcc -dumpversion
fi
bison --version
flex --version
# parallel build setting
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

EXTRA_DMAKE_FLAGS=
if test "$ENABLE_ICECREAM" = "yes" -a "$ICECREAM_JOBS" -gt 1; then
    EXTRA_DMAKE_FLAGS="-- -P$ICECREAM_JOBS"
fi

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

echo "Env:"
set

# update localizations from external sources
cd $TOOLSDIR/bin
./localize-ooo
cd -

echo 'Commencing main build'

cd $OOBUILDDIR/instsetoo_native || exit 1;
perl $SOLARENV/bin/build.pl --all $EXTRA_BUILD_FLAGS $EXTRA_DMAKE_FLAGS || exit 1;
OOO_REBUILD_NEEDED="no"

if [ "$VENDORNAME" = "Debian" ]; then
	cd $OOBUILDDIR/instsetoo_native/util || exit 1;
	dmake ure_en-US
fi

echo "Build succeeded ...!"

exit 0;
