#!/bin/sh

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

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
}

mkdir -p $SRCDIR
cd $SRCDIR

echo "Checking for source packages in $SRCDIR";
if test "z$SYSTEM_GCC" = "z"; then
    check_tarball $GCC_TARBALL
    if test "z$BINUTILS_TARBALL" != "z"; then
	check_tarball $BINUTILS_TARBALL
    fi
fi
check_tarball $OOO_TARBALL

mkdir -p $BUILDDIR
cd $BUILDDIR

if test "z$BUILD_WIN32" != "z"; then
    check_file $UNICOWS_SRC
    check_file $DBGINST_SRC
#    check_file $ODMA_SRC
    check_file $SRCDIR/$BUILD_MOZILLA_SOURCE
    check_file $SRCDIR/glib-1.2.10.tar.gz
    check_file $SRCDIR/glib-1.2.10-ooo.patch
    check_file $SRCDIR/libIDL-0.6.8.tar.gz
    check_file $SRCDIR/libIDL-0.6.8-ooo.patch
    check_file $SRCDIR/wintools.zip
fi

if test "z$SYSTEM_GCC" = "z"; then
    # unpack binutils
    cd $BUILDDIR
    echo "Unpacking compiler bits"

    if test "z$BINUTILS_TARBALL" != "z"; then
	tar $BINUTILS_UNTAR_OPTIONS $SRCDIR/$BINUTILS_TARBALL || exit 1;
	patch -p0 < $BINUTILS_PATCH || exit 1;
    fi
    tar $GCC_UNTAR_OPTIONS $SRCDIR/$GCC_TARBALL || exit 1;
    for P in $GCC_PATCHES ; do
	patch -d "$GCC_VER" -p1 -s < "$SRCDIR/$P"
    done
fi

echo "Unpacking OO.o build tree - [ go make some tea ] ..."
tar $OOO_UNTAR_OPTIONS $SRCDIR/$OOO_TARBALL || exit 1
echo "Linking rc3 to 2.0.0"
if ! test -d OOO_2_0_0; then
    ln -sf ooo680-m3 OOO_2_0_0
fi

echo "Fixing unfortunate snafus"
INDUSTRIAL_DIR=$OOBUILDDIR/ooo_custom_images/industrial/res/commandimagelist
cd $INDUSTRIAL_DIR
mv -f lc_zoomplus.png lc_zoomin.png
mv -f sc_zoomplus.png sc_zoomin.png
rm -f $OOBUILDDIR/extras/source/autotext/lang/en-US/acor_en-GB.*
cd $BUILDDIR

# copy in misc Mono related dlls if we need to
# we can't build them on Unix.
CLI_TYPES=cli_types.dll
if test -f "$SRCDIR/$CLI_TYPES"; then
    mkdir -p $OOBUILDDIR/external/cli
    cp -af $SRCDIR/$CLI_TYPES $SRCDIR/cli_types_bridgetest.dll $OOBUILDDIR/external/cli
fi

# copy oowintool into the tree
cp -af $TOOLSDIR/bin/oowintool $OOBUILDDIR/config_office
chmod a+x $OOBUILDDIR/config_office/oowintool

# -system source
if test -f $SRCDIR/$OOO_SYSTEM_TARBALL; then
    # System is optional, but parts of it are useful
    tar $OOO_UNTAR_OPTIONS $SRCDIR/$OOO_SYSTEM_TARBALL
fi
# -binfilter source
if test -f $SRCDIR/$OOO_BINFILTER_TARBALL; then
    tar $OOO_UNTAR_OPTIONS $SRCDIR/$OOO_BINFILTER_TARBALL
fi
# -lang source
if test -f $SRCDIR/$OOO_LANG_TARBALL; then
    tar $OOO_UNTAR_OPTIONS $SRCDIR/$OOO_LANG_TARBALL
fi

# Win32 prerequisites ...
if test "z$BUILD_WIN32" != "z"; then
# FIXME: add check for system bits ...

    # ODMA
	echo "ODMA disabled for now."
#    cd $BUILDDIR;
#    DEST=$OOBUILDDIR/ucb/source/ucp/odma
#    if test ! -f $DEST/ODMA.H; then
#	echo -n "Extracting ODMA SDK "
#	unzip -o $ODMA_SRC
#	TARGET=Odma20.exe
#	if test ! -f $TARGET; then
#	    echo "failed";
#	    exit 1;
#	fi
#	mkdir -p $DEST
#	cd $DEST
#	unzip -o $BUILDDIR/$TARGET
#	echo "done";
#    else
#	echo "Already have ODMA SDK";
#    fi

    # dbghelp
    cd $BUILDDIR;
    if test ! -f $OOBUILDDIR/external/dbghelp/DbgHelp.Dll; then
	echo -n "Extracting dbghelp "
	cabextract -q $DBGINST_SRC
	TARGET=dbghelp.exe
	if test ! -f $TARGET; then
	    echo "failed";
	    exit 1;
	fi
	unzip -o $TARGET
	echo "done";
    else
	echo "Already have dbghelp";
    fi
    
    # unicows
    cd $BUILDDIR;
    if test ! -f $OOBUILDDIR/external/dbghelp/unicows.dll; then
	echo -n "Extracting unicows "
	cabextract -q $UNICOWS_SRC
	TARGET=unicows.dll
	if test ! -f $TARGET; then
	    echo "failed";
	    exit 1;
	fi
	echo "done";
    else
	echo "Already have unicows";
    fi

    cd $BUILDDIR;
    APACHE_ANT='apache-ant-1.6.5'
    APACHE_ANT_TARBALL=$APACHE_ANT-bin.tar.gz
    if test -f $TOOLSDIR/src/$APACHE_ANT_TARBALL; then
	echo -n "Extracting ant "
	tar xzf $TOOLSDIR/src/$APACHE_ANT_TARBALL || exit 1;
	echo "done";
    else
	echo "Don't need to unpack ant";
    fi

    cd $BUILDDIR;
    if test -f $TOOLSDIR/src/$BUILD_MOZILLA_SOURCE;  then
        echo -n "Installing mozilla source "
	cp -f $TOOLSDIR/src/$BUILD_MOZILLA_SOURCE $OOBUILDDIR/moz/download || exit 1;
	echo "done";
    else
	echo "Don't need to unpack mozilla source";
    fi
    MOZILLA_IDL_SRC='libIDL-0.6.8.tar.gz'
    MOZILLA_IDL_PATCH='libIDL-0.6.8-ooo.patch'
    MOZILLA_GLIB_SRC='glib-1.2.10.tar.gz'
    MOZILLA_GLIB_PATCH='glib-1.2.10-ooo.patch'
    MOZILLA_WINTOOLS_BIN='wintools.zip'
    if test -f $TOOLSDIR/src/$MOZILLA_IDL_SRC; then
        echo -n "Installing mozilla IDL source "
	cp -f $TOOLSDIR/src/$MOZILLA_IDL_SRC $OOBUILDDIR/moz/download || exit 1;
	cp -f $TOOLSDIR/src/$MOZILLA_IDL_PATCH $OOBUILDDIR/moz/download || exit 1;
	echo "done";
    else
	echo "Don't need to unpack mozilla IDL source";
    fi
    if test -f $TOOLSDIR/src/$MOZILLA_GLIB_SRC; then
        echo -n "Installing mozilla glib source "
	cp -f $TOOLSDIR/src/$MOZILLA_GLIB_SRC $OOBUILDDIR/moz/download || exit 1;
	cp -f $TOOLSDIR/src/$MOZILLA_GLIB_PATCH $OOBUILDDIR/moz/download || exit 1;
	echo "done";
    else
	echo "Don't need to unpack mozilla glib source";
    fi
    if test -f $TOOLSDIR/src/$MOZILLA_WINTOOLS_BIN; then
        echo -n "Installing mozilla wintools"
	cp -f $TOOLSDIR/src/$MOZILLA_WINTOOLS_BIN $OOBUILDDIR/moz/download || exit 1;
	echo "done";
    else
	echo "Already have mozilla wintools";
    fi
fi

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

# Construct bits that should be in the up-stream tar.bz2
cd $OOBUILDDIR
CUSTOM_DIR=$OOBUILDDIR/ooo_custom_images
if test "z${WITH_ICONS#*industrial}" != "z$WITH_ICONS" ; then
# disable for now
#    tar xjf $SRCDIR/$OOO_CUSTOM_IMAGES || exit 1;
# fix a checkbox bug - in the up-stream icon set.
    rm -f $CUSTOM_DIR/industrial/vcl/source/src/check*; 
fi
if test "z${WITH_ICONS#*crystal}" != "z$WITH_ICONS" ; then
    tar xjf $SRCDIR/$OOO_CRYSTAL_IMAGES -C $CUSTOM_DIR || exit 1;
fi

if test "x$OOO_EXTRA_ARTWORK" != "x"; then
# Html export rulers etc.
    cd $OOBUILDDIR
    tar xjf $SRCDIR/$OOO_EXTRA_ARTWORK || exit 1;
fi

if test "x$OPENCLIPART_VER" != "x"; then
    echo "Unpacking open clipart"
    check_tarball $SRCDIR/openclipart-$OPENCLIPART_VER.tar.bz2
    cd $BUILDDIR
    tar xjf $SRCDIR/openclipart-$OPENCLIPART_VER.tar.bz2 || exit 1;
fi

if test "x$MDBTOOLS_SRC" != "x"; then
    echo "Copying mdbtools into the tree"
    MDBDEST=$OOBUILDDIR/mdbtools/download
    mkdir -p $MDBDEST || exit 1;
    cp -a $SRCDIR/$MDBTOOLS_SRC $MDBDEST || exit 1;
fi

echo "Copying default evolution database into tree"
cp -af $TOOLSDIR/src/evolocal.odb $OOBUILDDIR/extras/source/database || exit 1;

if test "$CAIRO_ENABLED" = "TRUE" -a "$SYSTEM_CAIRO" != "TRUE"; then
    echo "Making symbolic links of libpixman, cairo and glitz at build location"

    mkdir -p $OOBUILDDIR/glitz
    check_tarball $SRCDIR/glitz-${GLITZ_VER}.tar.gz
    mkdir -p $OOBUILDDIR/glitz/download
    cp -a $SRCDIR/glitz-${GLITZ_VER}.tar.gz $OOBUILDDIR/glitz/download/

    mkdir -p $OOBUILDDIR/cairo
    check_tarball $SRCDIR/cairo-${CAIRO_VER}.tar.gz
    mkdir -p $OOBUILDDIR/cairo/download
    cp -a $SRCDIR/cairo-${CAIRO_VER}.tar.gz $OOBUILDDIR/cairo/download/
fi

if test "x$HUNSPELL_UNO_SRC" != "x"; then
    echo "Unpacking hunspell UNO sources"
    tar -xzf $SRCDIR/$HUNSPELL_UNO_SRC -C $OOBUILDDIR || exit 1;
fi

if test "x$HUNSPELL_LIB_SRC" != "x"; then
    echo "Copying hunspell library sources into the tree"
    mkdir -p $OOBUILDDIR/libhunspell/download || exit 1;
    cp -a $SRCDIR/$HUNSPELL_LIB_SRC $OOBUILDDIR/libhunspell/download || exit 1;
fi
