#!/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

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

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

# If we are forcing gcc33, make sure configure knows
if test "z$FORCE_GCC33" = "zYES"; then
CONFIGURE_OPTIONS="$CONFIGURE_OPTIONS CFLAGS= CXXFLAGS= CC=gcc33 CXX=g++33"
fi

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

eval ./configure "$CONFIGURE_OPTIONS" "$PROPAGATED_ARGS" || exit 1;

# Unfortunately, on when forcing gcc33 on x86, RPM attempts to
# inject its %{optflags} everywhere.  Since gcc33 doesn't support
# -mtune, override that here
if test "z$FORCE_GCC33" = "zYES"; then
	if test "z$DISTRO" = "zRHFedora"; then
		# Set up x86 optimization flags that gcc33 can deal with
		ARCH_FLAGS=""
		if test "z$INPATH" = "zunxlngi4.pro"; then
			ARCH_FLAGS="-march=i386 -mcpu=i686"
		fi
		export ARCH_FLAGS
		FOOBAR=`ls $OOBUILDDIR/*.Set.sh`
		echo 'ARCH_FLAGS="$ARCH_FLAGS"' >> $FOOBAR
	fi
fi

# 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
# Embedded python dies without Home set
if test "z$HOME" = "z"; then
    export HOME="";
fi
cd $OOBUILDDIR
if ! test "z$BUILD_DMAKE" == "zNO" -o -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
    fi
fi
echo 'Bootstrapping'
./bootstrap || ./bootstrap || ./bootstrap || exit 1;
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
# 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:  \'$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

# What extra localizations are avilable as $SRCDIR/GSI_*.sdf and are not
# already merged?
extra_trans_stamp=$TOOLSDIR/stamp/build.extra.translations
extra_trans=`find $SRCDIR -name "GSI_*.sdf"`
extra_trans_langs=
for file in $extra_trans ; do
    # skip this localization if it is already merged
    grep -q "^$file$" $extra_trans_stamp 2>/dev/null && continue;
    # find if this localization is required
    trans_lang=`echo $file | sed "s|^$SRCDIR/GSI_\(.*\).sdf\$|\1|"`
    for lang in $OOO_LANGS_LIST ; do
        if test "z$lang" = "z$trans_lang" ; then
	    extra_trans_langs="$trans_lang $extra_trans_langs"
	    break;
	fi
    done
done	    

if test "z$extra_trans_langs" != "z" ; then
    echo 'Building localize stuff at first...'
    cd $OOBUILDDIR/transex3
    perl $SOLARENV/bin/build.pl --all $EXTRA_BUILD_FLAGS $EXTRA_DMAKE_FLAGS || exit 1;
    perl $SOLARENV/bin/deliver.pl || exit 1;
   
    echo "Fixing localize.sdf permissions..."
    find $OOBUILDDIR -type f -name "localize.sdf" -exec chmod 644 {} \;

    for langcode in $extra_trans_langs ; do
	# merge the available localization
        echo "Merging extra \"$langcode\" localization..."
	$SOLARVERSION/$INPATH/bin/localize -m -x -l $langcode -f $SRCDIR/GSI_$langcode.sdf || exit 1;
	echo ""
	DEB_GSIFILE=$DEB_GSIDIR/GSI_$langcode.sdf
	if test "z$DEB_GSIDIR" != "z" && test -f $DEB_GSIFILE; then
	    echo "Merging imported \"$langcode\" localization..."
	    $SOLARVERSION/$INPATH/bin/localize -m -x -l $langcode -f $DEB_GSIFILE || exit 1;
	    echo ""
	else
	    DEB_GSIFILE=
	fi
	
	# copy help auxiliary files if they are missing but the localized strings are available
	if grep -q "^helpcontent2" $SRCDIR/GSI_$langcode.sdf $DEB_GSIFILE &&
	   test ! -d $OOBUILDDIR/helpcontent2/source/auxiliary/$langcode ; then
	    echo "Copying English help auxiliary files for \"$langcode\"..."
	    cd $OOBUILDDIR/helpcontent2/source/auxiliary
	    cp -r en-US $langcode
	    perl -pi -e "s|Language=en-US|Language=$langcode|" $langcode/*.cfg
	fi
	# make stamp for this localization
	echo $SRCDIR/GSI_$langcode.sdf >>$extra_trans_stamp
    done

    if test "z$DEB_GSIDIR" != "z"; then
	for fn in $DEB_GSIDIR/*all*.sdf; do
	    [ -f "$fn" ] || continue
	    echo "Merging extra localization $(basename $fn) ..."
	    $SOLARVERSION/$INPATH/bin/localize -m -x -l all -f $fn || exit 1;
	done
    fi
fi

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"
    
echo "Build succeeded ...!"

exit 0;
