#!/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
    echo -n "Looking for $SRCDIR/gdiplus.dll ... "
    if test ! -f $SRCDIR/gdiplus.dll; then
	echo "missing"
	echo "Get it from the Microsoft site and put it into $SRCDIR"
	echo "You may have to search Microsoft's website."
	echo "Last time it was seen at: http://www.microsoft.com/downloads/details.aspx?familyid=6A63AB9C-DF12-4D41-933C-BE590FEAA05A&displaylang=en"
	echo "Note that the download requires Genuine Windows validation"
	echo "and can't easily be automated."
	exit 1
    else
	echo "ok"
    fi
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 ] ..."
echo "Unpacking $OOO_TARBALL..."
tar $OOO_UNTAR_OPTIONS $SRCDIR/$OOO_TARBALL || exit 1

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

if test "$ENABLE_CCACHE" = "yes" ; then
    if test "$BUILDDIR/$CVSTAG" != "$OOBUILDDIR" ; then
        mv "$BUILDDIR/$CVSTAG" "$OOBUILDDIR"
    fi
    echo "$CVSTAG" > "$OOBUILDDIR/.tag"
fi

echo "Fixing unfortunate snafus"
INDUSTRIAL_DIR=$OOBUILDDIR/ooo_custom_images/industrial/res/commandimagelist
# more unfortunate bits i#65746
cp -a $INDUSTRIAL_DIR/lc_basicshapes.diamond.png $INDUSTRIAL_DIR/lc_basicshapes.png 
cp -a $INDUSTRIAL_DIR/sc_basicshapes.diamond.png $INDUSTRIAL_DIR/sc_basicshapes.png 
cd $BUILDDIR

# Work around component-schema.dtd mess in m142 up to m151; seems to be fixed in m152
if test -e "$OOBUILDDIR/officecfg/registry/component-schema.dtd"; then
    cp $OOBUILDDIR/officecfg/registry/component-schema.dtd $OOBUILDDIR/officecfg/registry/schema
fi

# copy in unowinreg.dll if we need to
# it is necessary in ODK
# it can be built even on linux by mingw32, though
if test "$USE_PREBUILD_UNOWINREG_DLL" = "YES" ; then
    cp -f $SRCDIR/unowinreg.dll $OOBUILDDIR/external/unowinreg/ || exit 1;
fi

# 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
if test -f $OOBUILDDIR/config_office/oowintool; then
    :
else
    cp -af $TOOLSDIR/bin/oowintool $OOBUILDDIR/config_office
    chmod a+x $OOBUILDDIR/config_office/oowintool
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

    # gdiplus.dll
    cd $BUILDDIR;
    if test ! -f $OOBUILDDIR/external/gdiplus/gdiplus.dll; then
	cp -p $SRCDIR/gdiplus.dll $OOBUILDDIR/external/gdiplus/gdiplus.dll
    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
	mv -f License.Txt dbghelp-license.txt
	mv -f redist.txt dbghelp-redist.txt
	echo "done";
    else
	echo "Already have dbghelp";
    fi
    
    # unicows
    cd $BUILDDIR;
    if test ! -f $OOBUILDDIR/external/unicows/unicows.dll; then
	echo -n "Extracting unicows "
	cabextract -q $UNICOWS_SRC
	TARGET=unicows.dll
	if test ! -f $TARGET; then
	    echo "failed";
	    exit 1;
	fi
	mv -f license.txt unicows-license.txt
	mv -f redist.txt unicows-redist.txt
	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

    if test -f $TOOLSDIR/src/$AGFA_MONOTYPE_FONTS_SOURCE; then
        if test ! -f $OOBUILDDIR/agfa_monotype_fonts/download/$AGFA_MONOTYPE_FONTS_SOURCE; then
	    echo -n "Installing Agfa Monotype fonts "
	    mkdir -p $OOBUILDDIR/agfa_monotype_fonts/download
	    cp -f $TOOLSDIR/src/$AGFA_MONOTYPE_FONTS_SOURCE $OOBUILDDIR/agfa_monotype_fonts/download || exit 1
	    echo "done"
	else
            echo "Already have Agfa Monotype fonts"
        fi
	if grep 'Agfa Monotype' $OOBUILDDIR/readlicense_oo/html/THIRDPARTYLICENSEREADME.html; then
	  echo "THIRDPARTYLICENSEREADME.html already contains Agfa Monotype EULA"
	else
	  echo "Adding Agfa Monotype EULA to THIRDPARTYLICENSEREADME.html"
	  $TOOLSDIR/bin/amt-license-patch <$OOBUILDDIR/readlicense_oo/html/THIRDPARTYLICENSEREADME.html >/tmp/amt.$$ &&
	  mv /tmp/amt.$$ $OOBUILDDIR/readlicense_oo/html/THIRDPARTYLICENSEREADME.html
	fi
    fi

    if test "x$DEJAVU_FONTS_VER" != "x"; then
        DEJAVU_FONTSDEST=$OOBUILDDIR/dejavufonts/download
        DEJAVU_FONTSSRC=dejavu-ttf-$DEJAVU_FONTS_VER.tar.gz
        if test ! -f $DEJAVU_FONTSDEST/$DEJAVU_FONTSSRC; then
            echo "Installing DejaVu fonts"
            mkdir -p $DEJAVU_FONTSDEST || exit 1;
            cp -f $SRCDIR/$DEJAVU_FONTSSRC $DEJAVU_FONTSDEST || exit 1;
            echo "done"
        else
            echo "Already have DejaVu fonts"
        fi
    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 "x$OOO_EXTRA_ARTWORK" != "x"; then
# Html export rulers etc.
    cd $OOBUILDDIR
    tar xjf $SRCDIR/$OOO_EXTRA_ARTWORK || exit 1;
fi

for i in $OOO_LANGS; do
	if [ -e $SRCDIR/extras_${i}.tar.gz ]; then
		echo "Extracting extra ${i} extras"
		cd $OOBUILDDIR
		tar xzf $SRCDIR/extras_${i}.tar.gz || exit 1
	fi
	if [ -e $SRCDIR/help_images_${i}.tar.gz ]; then
		echo "Extracting extra ${i} help images"
		cd $OOBUILDDIR
		tar xzf $SRCDIR/help_images_${i}.tar.gz || exit 1
	fi
done

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" -a "z$SYSTEM_MDBTOOLS" != "YES"; 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

XT_ZIP=xt-20051206-src-only.zip
check_tarball $SRCDIR/$XT_ZIP
echo "Copying $XT_ZIP into the tree"
mkdir -p $OOBUILDDIR/xt/download || exit 1
cp $SRCDIR/$XT_ZIP $OOBUILDDIR/xt/download/ || exit 1

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

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

echo "Installing bibliography overwrides";
tar xjf $SRCDIR/biblio.tar.bz2 -C $OOBUILDDIR/extras/source/database

echo "Copying custom user-dicts into tree"
cp -af $TOOLSDIR/src/*.dic $OOBUILDDIR/extras/source/wordbook || exit 1;

if test "$CAIRO_ENABLED" = "TRUE" -a "$SYSTEM_CAIRO" != "TRUE"; then

    if test "z$BUILD_WIN32" = "z"; then
         echo "Copying glitz into tree"
	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/
    fi

    echo "Copying cairo into tree"
    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

LPSOLVE_PACKAGE=lp_solve_5.5.tar.gz
check_tarball $SRCDIR/$LPSOLVE_PACKAGE
echo "Copying lp_solve package into the tree"
mkdir -p $OOBUILDDIR/lpsolve/download || exit 1
cp -a $SRCDIR/$LPSOLVE_PACKAGE $OOBUILDDIR/lpsolve/download/ || exit 1

