#!/bin/bash

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

if test "z$1" = 'z--help'; then
	echo "build-ooo <BRANCH> [--checkout[--clean]|--help]";
	echo " --checkout: updates CVS tree";
	echo " --clean:    cleans OO build tree";
	echo " --nopatch:  doesn't re-patch the tree";
	exit 0;
fi

# Ensure dirs
echo "Creating environment"
mkdir -p $BUILDDIR

# misc install brokenness
mkdir -p $BUILDDIR/bin
mkdir -p $BUILDDIR/share
mkdir -p $BUILDDIR/share/aclocal
mkdir -p $BUILDDIR/share/autoconf
mkdir -p $BUILDDIR/lib
mkdir -p $BUILDDIR/man
mkdir -p $BUILDDIR/man/man1
mkdir -p $OOBUILDDIR

# Versions
GCC_VER=gcc-3.2.2
GCC_TARBALL=gcc-3.2.2.tar.bz2
GCC_UNTAR_OPTIONS=jxf

BINUTILS_VER=binutils-2.13.2.1
BINUTILS_TARBALL=binutils-2.13.2.1.tar.bz2
BINUTILS_UNTAR_OPTIONS=jxf
BINUTILS_PATCH=$TOOLSDIR/patches/ld-2.13.2.1.diff

if test "z$ENABLE_JAVA" = "zyes"; then
    # If the user didn't specify the location of the JDK,
    # use builtin/downloaded one
    if test "z$JDK_HOME" = "z"; then
        JDK_VER=j2sdk1.4.1
        JDK_TARBALL=j2sdk-1.4.1-01-linux-i586-gcc3.2.tar.bz2
        JDK_UNTAR_OPTIONS=jxf
    fi
fi

#OOO_ICONS_VER is sourced from "setup"
OOO_ICONS_TARBALL=$OOO_ICONS_VER.tar.gz
OOO_ICONS_UNTAR_OPTIONS=xzf

OOO_SCALED_ICONS_VER=ooo-scaled-icons
OOO_SCALED_ICONS_TARBALL=$OOO_SCALED_ICONS_VER.tar.gz
OOO_SCALED_ICONS_UNTAR_OPTIONS=xzf

OOO_TARBALL="$CVSTAG.tar.bz2"
OOO_UNTAR_OPTIONS=jxf

check-tarball ()
{
    echo -n "Looking for $1 ... ";
    if test ! -f $1; then
	echo "missing the $1 archive; run './download'"
	exit 1;
    else
	echo "ok"
    fi
}

check-file ()
{
    echo -n "Looking for $1 ... ";
    if test ! -f $1; then
	echo "missing required archive; run ./download'";
	exit 1;
    else
	echo "ok"
    fi
}

if test "z$WITH_SRC" = "z"; then
    mkdir -p $SRCDIR
    cd $SRCDIR

    echo "Checking for source packages in $SRCDIR";
    if test "z$SYSTEM_GCC" = "z"; then
        check-tarball $GCC_TARBALL
        check-tarball $BINUTILS_TARBALL
    fi
    if test "z$ENABLE_JAVA" = "zyes" &&
       test "z$JDK_HOME" = "z"; then
        check-tarball $JDK_TARBALL
    fi
    check-tarball $OOO_TARBALL
    check-tarball $OOO_ICONS_TARBALL

    if test "z$SYSTEM_GCC" = "z"; then
      if test ! -f $GCC_TARBALL; then
	   echo "You need to put the relevant source packages into $SRCDIR before commencing the build";
	   exit 1
      fi
    fi

    if test "z$BUILD_WIN32" != "z"; then
	check-tarball $OOO_SCALED_ICONS_TARBALL
    fi

elif test "z$SYSTEM_GCC" = "z"; then
    if test "z$ENABLE_JAVA" = "zyes" &&
       test ! -d "$BUILDDIR/$JDK_VER" &&
       test "z$JDK_HOME" = "z"; then
        echo "Missing the JDK";
    fi

    if test ! -d "$BUILDDIR/$GCC_VER" ||
       test ! -d "$BUILDDIR/$OOO_ICONS_VER" ||
       test ! -d "$BUILDDIR/$BINUTILS_VER"; then
        echo "Missing some of the helper source";
        exit 1;
    fi
fi

cd $BUILDDIR

UNICOWS_SRC=$SRCDIR/unicows.exe;
DBGINST_SRC=$SRCDIR/dbghinst.exe;
if test "z$BUILD_WIN32" != "z"; then
    check-file $UNICOWS_SRC
    check-file $DBGINST_SRC
fi

if test "z$ENABLE_JAVA" = "zyes" &&
   test "z$JDK_HOME" = "z"; then
    # unpack the Jdk
    PKG_VER=$JDK_VER

    if test -f $BUILDDIR/$PKG_VER/$STAMP; then
	echo "Skipping $PKG_VER";
	ln -sfn $PKG_VER $BUILDDIR/jdk
    else
	echo "Unpacking $PKG_VER";
	cd $BUILDDIR
	if test "z$WITH_SRC" = "z"; then
	    tar $JDK_UNTAR_OPTIONS $SRCDIR/$JDK_TARBALL || exit 1;
	fi
# Link it into where we expect it in the path
	ln -sfn $PKG_VER $BUILDDIR/jdk
	touch $BUILDDIR/$PKG_VER/$STAMP || exit 1;
    fi
fi

if test "z$SYSTEM_GCC" = "z"; then
	# build binutils
	PKG_VER=$BINUTILS_VER

	# -- evil cut --
	if test -f $BUILDDIR/$PKG_VER/$STAMP; then
		echo "Skipping $PKG_VER";
	else
		echo "Building $PKG_VER";
		cd $BUILDDIR
		if test "z$WITH_SRC" = "z"; then
			tar $BINUTILS_UNTAR_OPTIONS $SRCDIR/$BINUTILS_TARBALL || exit 1;
			patch -p0 < $BINUTILS_PATCH || exit 1;
		fi
		cd $PKG_VER
		./configure --prefix=$BUILDDIR || exit 1;
		make && make install || exit 1;
		touch $BUILDDIR/$PKG_VER/$STAMP || exit 1;
	fi
	# -- cut --

	# build gcc
	PKG_VER=$GCC_VER

	# -- evil cut --
	if test -f $BUILDDIR/$PKG_VER/$STAMP; then
		echo "Skipping $PKG_VER";
	else
		echo "Building $PKG_VER";
		cd $BUILDDIR
		if test "z$WITH_SRC" = "z"; then
			tar $GCC_UNTAR_OPTIONS $SRCDIR/$GCC_TARBALL || exit 1;
		fi
		cd $PKG_VER
		./configure --prefix=$BUILDDIR --enable-languages=c++ || exit 1;
		make && make install || exit 1;
		touch $BUILDDIR/$PKG_VER/$STAMP
	fi
	# -- cut --
fi

PKG_VER=$OOO_ICONS_VER

# -- evil cut --
if test -f $BUILDDIR/$PKG_VER/$STAMP; then
	echo "Skipping $PKG_VER";
else
	echo "Building $PKG_VER";
	cd $BUILDDIR
	if test "z$WITH_SRC" = "z"; then
	    tar $OOO_ICONS_UNTAR_OPTIONS $SRCDIR/$OOO_ICONS_TARBALL || exit 1;
	fi
	touch $BUILDDIR/$PKG_VER/$STAMP || exit 1;
fi
# -- cut --

PKG_VER=$OOO_SCALED_ICONS_VER

# -- evil cut --
if test "z$BUILD_WIN32" != "z"; then
    if test -f $BUILDDIR/$PKG_VER/$STAMP; then
	echo "Skipping $PKG_VER";
    else
	echo "Building $PKG_VER";
	cd $BUILDDIR
	if test "z$WITH_SRC" = "z"; then
	    tar $OOO_SCALED_ICONS_UNTAR_OPTIONS $SRCDIR/$OOO_SCALED_ICONS_TARBALL || exit 1;
	fi
	touch $BUILDDIR/$PKG_VER/$STAMP || exit 1;
    fi
fi
# -- cut --

# For RH distributions, if we have the splash & about screens,
# copy them into the source dir
if test "z$VENDORNAME" = "zRedHat"; then
	mkdir -p $OOBUILDDIR/offmgr/res
	cp $SRCDIR/openabout_redhat.bmp $OOBUILDDIR/offmgr/res/
	cp $SRCDIR/openintro_redhat.bmp $OOBUILDDIR/offmgr/res/
fi

# Checkout / Update source:
if (test "z$2" = "z--checkout") || !(test -f $OOBUILDDIR/$STAMP); then

	FONTMUNGE_FILE="$OOBUILDDIR/officecfg/registry/data/org/openoffice/VCL.xcu";

	if test "z$WITH_SRC" = "z"; then
		PRISTINE_SOURCE=0
		if test ! -d $OOBUILDDIR/solenv; then
			echo "OOo build tree not found; unpacking it... [go make some tea]"
			cd $OOBUILDDIR
			cd ..
			tar $OOO_UNTAR_OPTIONS $SRCDIR/$OOO_TARBALL || exit 1
			PRISTINE_SOURCE=1
		else
			if test -f "$FONTMUNGE_FILE.bak"; then
			    echo "Revert font mapping munging ..."
			    mv -f "$FONTMUNGE_FILE.bak" $FONTMUNGE_FILE;
			fi

			echo "Reverting patches ..."
			$TOOLSDIR/patches/apply.pl $TOOLSDIR/patches/$CVSTAG $OOBUILDDIR --distro=$DISTRO -R || exit 1;
		fi

		if test "z$BUILD_WIN32" = "z"; then
			echo "Removing custom icons";
			$TOOLSDIR/bin/scale-icons $OOBUILDDIR --remove || exit 1;
		fi

		echo "Poking resource builds";
		find $OOBUILDDIR/*/*.pro -name '*.dpr' -exec rm {} \;
		find $OOBUILDDIR/*/*.pro -name '*.don' -exec rm {} \;
		find $OOBUILDDIR/*/*.pro -name '*.srs' -exec rm {} \;

		echo "Updating source ..."
		cd $OOBUILDDIR

		if test -f $SRCDIR/$CVSTAG.tar.bz2; then
			# Don't re-untar the sources if you've just untarred a pristine copy above
			if test "$OOO_TARBALL" != "$CVSTAG.tar.bz2" -o $PRISTINE_SOURCE -eq 0; then
				cd ..
				tar xjf $SRCDIR/$CVSTAG.tar.bz2;
			else
				echo "Actually, not updating because sources are pristine."
			fi
		else
			echo "Unreliably broken CVS branch...";
			if test "z`hostname`" = "zchampignon.ximian.com"; then
				cvs -d /cvsroot update -dP
			else
				cvs -z3 -q -d ':pserver:anoncvs@ftp.stardiv.de:/cvs' checkout -r $CVSTAG OpenOffice || exit 1;
#				cvs -z3 -q -d ':pserver:mmeeks@localhost:/cvs' checkout -r $CVSTAG OpenOffice || echo "Some error updating [?]";
			fi
		fi
	else
		# We expect a pristine copy to be there already ...

		if test ! -d "$BUILDDIR/$CVSTAG"; then
			echo "Missing the main OO.o source tree";
		fi
	fi

	# Win32 prerequisites ...
	cd $BUILDDIR;
	if test "z$BUILD_WIN32" != "z"; then
	    # dbghelp
	    echo -n "Extracting dbghelp "
	    cabextract -q $DBGINST_SRC
	    TARGET=dbghelp.exe
	    if test ! -f $TARGET; then
		echo "failed";
		exit 1;
	    fi
	    unzip -o $TARGET
	    cp -af x86/* $OOBUILDDIR/external/dbghelp
	    echo "done";

	    # unicows
	    echo -n "Extracting unicows "
	    cabextract -q $UNICOWS_SRC
	    TARGET=unicows.dll
	    if test ! -f $TARGET; then
		echo "failed";
		exit 1;
	    fi
	    cp -af $TARGET $OOBUILDDIR/external/unicows

	    # vcp bits (?)
	    mkdir -p $OOBUILDDIR/external/msvcp71
	fi

	cd $OOBUILDDIR

	# (re)apply patches
	if test "z$2" = "z--nopatch"; then
		echo "Skipping patching";
	else
		echo "Re-applying patches"
		$TOOLSDIR/patches/apply.pl $TOOLSDIR/patches/$CVSTAG $OOBUILDDIR --distro=$DISTRO -f || exit 1;
	fi

	if test "z$BUILD_WIN32" = "z"; then
		echo "Installing / scaling icons";
		$TOOLSDIR/bin/scale-icons $OOBUILDDIR || echo "Error: scaling failed";
		echo "done icon scaling";
	else
	    cp -Rvf $BUILDDIR/$OOO_SCALED_ICONS_VER/* $OOBUILDDIR || echo "Scaled icon copy failed"
	fi

	cp -Rvf $BUILDDIR/$OOO_ICONS_VER/* $OOBUILDDIR || echo "Icon copy failed";
	echo "Copied new artwork into the tree";

	cp -vf $TOOLSDIR/fonts/opens___.ttf $OOBUILDDIR/extras/source/truetype/symbol || echo "font copy failed";
	echo "Copied new opensymbol font into the tree";
	
	find $OOBUILDDIR -name '*.src' -exec touch {} \;;

	echo "Munging font mappings ..."
	$TOOLSDIR/bin/font-munge $FONTMUNGE_FILE;
	echo "done munging fonts."

	touch $OOBUILDDIR/$STAMP;
else
	echo "Skipping checkout, use --checkout to update";
fi

if test "z$3" = "z--clean"; then
	cd $OOBUILDDIR
	echo "Cleaning ..."
	find -name 'unxlngi4.pro' -exec rm -Rf {} \;
	exit 0;
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;

./configure $CONFIGURE_OPTIONS || exit 1;

# Final build preparation

# tcsh sucks great rocks, and refuses to re-direct it's output otherwise
export TERM=
# Warning - do not edit, this is a temporary file, see build-ooo
# Many Java files have 8bit char-set comments, javac barfs on them in utf8 locales
export LANG="C";
# Many Java's can't cope with the NPTL on Linux.
export LD_ASSUME_KERNEL="2.2.5";
# Don't do a massive painful install
export BUILD_MINIMAL="01";
# Embedded python dies without Home set
export HOME="";
cd $OOBUILDDIR
# Import all of the OO.o environment
if test "z$DISTRO" = "zWin32"; then
    source $OOBUILDDIR/*.set.sh
else
    source $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\'"
if test "z$DISTRO" != "zWin32"; then
    echo "Gcc: "
    gcc -dumpversion
fi
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
# Accelerate straight-through compiles by not building / using dependencies
export nodep=1
perl $SOLARENV/bin/build.pl --all $EXTRA_BUILD_FLAGS --dlv_switch link || exit 1;

touch $OOBUILDDIR/$STAMP || exit 1;

echo "Build succeeded ...!"
exit 0;
