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

if (! pkg-config --version >& /dev/null); then
    echo "Can't find pkg-config";
    exit 1;
fi

if (! `pkg-config --exists gtk+-2.0 libxml-2.0 gnome-vfs-2.0 >& /dev/null`); then
    echo "Failed to find required packages";
    exit 1;
fi

if (! pkg-config --modversion 'libgnomecups-1.0 >= 0.1.0'); then
    echo "Can't find required packages";
    exit 1;
fi

if (! pkg-config --modversion 'fontconfig >= 1.0.1'); then
    echo "Can't find required packages";
    exit 1;
fi

if (! test -f /usr/include/png.h); then
    echo "Don't have libpng installed (!)";
    exit 1;
fi

if (! test -f /usr/include/security/pam_appl.h); then
    echo "Don't have PAM installed - possible problem";
fi

if (! `convert >& /dev/null`); then
    echo "Don't have ImageMagick installed (!)";
    exit 1;
fi

if (! `autoconf --version >& /dev/null`); then
    echo "Don't have autoconf installed (!)";
    exit 1;
fi

if (! `flex --version >& /dev/null`); then
    echo "Don't have flex installed (!)";
    exit 1;
fi

if (! `bison --version >& /dev/null`); then
    echo "Don't have bison installed (!)";
    exit 1;
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
# if test "z$CVSTAG" = "zOOO_1_0_2"; then
    GCC_VER=gcc-3.2.2
    GCC_TARBALL=gcc-3.2.2.tar.bz2
    GCC_UNTAR_OPTIONS=jxf

if test ${CVSTAG:0:4} != 'HEAD'; then
    JDK_VER=j2sdk1.3.1
    JDK_TARBALL=j2sdk-1.3.1-FCS-linux-i386.tar.bz2
    JDK_REPO_NAME=j2sdk-1.3.1-FCS-linux-i386.tar.bz2-1
    JDK_UNTAR_OPTIONS=jxf
else
    JDK_VER=j2sdk1.4.1
    JDK_TARBALL=j2sdk-1.4.1-01-linux-i586-gcc3.2.tar.bz2
    JDK_REPO_NAME=j2sdk-1.4.1-01-linux-i586-gcc3.2.tar.bz2-2
    JDK_UNTAR_OPTIONS=jxf
fi

    BINUTILS_VER=binutils-2.13.2.1
    BINUTILS_TARBALL=binutils-2.13.2.1.tar.bz2
    BINUTILS_UNTAR_OPTIONS=jxf

    OOO_TARBALL="$CVSTAG.tar.bz2"
    OOO_REPO_NAME="$CVSTAG.tar.bz2-1"
    OOO_UNTAR_OPTIONS=jxf

    CONFIGURE_OPTIONS="--with-lang=$LANGS"
# fi

# get-tarball TARBALLFILENAME 
# Sees if a tarball exists and if not gets it from build-buddy
get-tarball ()
{
    tarball=$1
    reponame=$2
    if test "z$reponame" = "z"; then
	reponame="$tarball"-1
    fi
    echo -n "Looking for $tarball... "
    if test ! -f $tarball; then
	echo "no --- Getting $reponame from build-buddy"
	bb_get $reponame || exit 1
    else
	echo "ok"
    fi
}

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

    echo "Checking for source packages in $SRCDIR";
    get-tarball $GCC_TARBALL
    get-tarball $BINUTILS_TARBALL
    get-tarball $JDK_TARBALL $JDK_REPO_NAME
    get-tarball $OOO_TARBALL $OOO_REPO_NAME

    if test ! -f $GCC_TARBALL; then
	echo "You need to put the relevant source packages into $SRCDIR before commencing the build";
	exit 1
    fi
else
    if test ! -d "$BUILDDIR/$JDK_VER" ||
       test ! -d "$BUILDDIR/$GCC_VER" ||
       test ! -d "$BUILDDIR/$BINUTILS_VER"; then
       echo "Missing some of the helper source";
       exit 1;
    fi
fi

cd $BUILDDIR

# 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$BUILD_VERSION" = "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

# 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$BUILD_VERSION" = "z"; then
	    tar $BINUTILS_UNTAR_OPTIONS $SRCDIR/$BINUTILS_TARBALL || 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$BUILD_VERSION" = "z"; then
	    tar $GCC_UNTAR_OPTIONS $SRCDIR/$GCC_TARBALL || exit 1;
	fi
	cd $PKG_VER
	./configure --prefix=$BUILDDIR || exit 1;
	make && make install || exit 1;
	touch $BUILDDIR/$PKG_VER/$STAMP
fi
# -- cut --

# build png2bmp
cd $TOOLSDIR/util
make || exit 1;

# Checkout / Update source:
if (test "z$2" = "z--checkout") || !(test -f $OOBUILDDIR/$STAMP); then
	if test "z$BUILD_VERSION" = "z"; then
		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
		else
			echo "Reverting patches ..."
			$TOOLSDIR/patches/apply.pl $TOOLSDIR/patches/$CVSTAG $OOBUILDDIR -R $QUIET
		fi

		echo "Removing custom icons";
		$TOOLSDIR/bin/scale-icons $OOBUILDDIR $QUIET --remove || exit 1;
		if test -f $TOOLSDIR/icons/ChangeLog; then
			$TOOLSDIR/bin/install-icons $CVSTAG $OOBUILDDIR $TOOLSDIR $QUIET --remove || exit 1;
		fi

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

		echo "Updating source ..."
		cd $OOBUILDDIR

		if test -f $SRCDIR/$CVSTAG.tar.bz2; then
			cd ..
			tar xjf $SRCDIR/$CVSTAG.tar.bz2;
		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

	cd $OOBUILDDIR

	# (re)apply patches
	if test "z$2" = "z--nopatch"; then
		echo "Skipping patching";
	else
		echo "Installing link maps"
		$TOOLSDIR/bin/install-maps $OOBUILDDIR $TOOLSDIR gcc3 || exit 1;

		echo "Re-applying patches"
		$TOOLSDIR/patches/apply.pl $TOOLSDIR/patches/$CVSTAG $OOBUILDDIR -f $QUIET || exit 1;
	fi

	echo "Installing / scaling icons";
	$TOOLSDIR/bin/scale-icons $OOBUILDDIR $QUIET || echo "Error: scaling failed";
	echo "done icon scaling";
	if test -f $TOOLSDIR/icons/ChangeLog; then
	    $TOOLSDIR/bin/install-icons $CVSTAG $OOBUILDDIR $TOOLSDIR $QUIET || exit 1;
	else
	    cp -avf $TOOLSDIR/icons/* $OOBUILDDIR || echo "Icon copy failed";
	fi
	
	find $OOBUILDDIR -name '*.src' -exec touch {} \;;

	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
cd $OOBUILDDIR/config_office
rm -f config.cache
echo "configuring ...";
autoconf || exit 1;

./configure $CONFIGURE_OPTIONS || exit 1;

echo "Starting tsch source builds"
cat > /tmp/build-ooo-tcsh <<"EOF"
#!/bin/tcsh
# Warning - do not edit, this is a temporary file, see build-ooo
setenv LANG "C";
setenv LD_ASSUME_KERNEL "2.2.5";
cd $OOBUILDDIR
source $OOBUILDDIR/*.Set || exit 1;
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++.so.3 $SOLARVER/$UPD/$INPATH/lib/libstdc++.so.3.0.1 || exit 1;
cp -vf $BUILDDIR/lib/libstdc++* $SOLARVER/$UPD/$INPATH/lib/ || exit 1;
echo 'Verifying environment'
echo "Path:  '\$PATH'";
echo "Shell: '\$SHELL'"
echo "Gcc: "
gcc -dumpversion
echo 'Bootstrapping'
./bootstrap || ./bootstrap || ./bootstrap || exit 1;
echo 'Commencing main build'
$SOLARENV/$OUTPATH/bin/dmake || $SOLARENV/$OUTPATH/bin/dmake || $SOLARENV/$OUTPATH/bin/dmake || exit 1;
EOF

chmod 755 /tmp/build-ooo-tcsh
# tcsh sucks great rocks, and refuses to re-direct it's output otherwise
export TERM=
/tmp/build-ooo-tcsh || exit 1;

touch $OOBUILDDIR/$STAMP || exit 1;

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