#!/bin/sh

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

if test "z$WITH_SRC" != "z"; then
	echo "Nothing to do - package build"
	exit 0;
fi

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
    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$BUILD_WIN32" != "z"; then
    check_tarball $OOO_SCALED_ICONS_TARBALL
fi

mkdir -p $BUILDDIR
cd $BUILDDIR

if test "z$BUILD_WIN32" != "z"; then
    check_file $UNICOWS_SRC
    check_file $DBGINST_SRC
#    check_file $ODMA_SRC
fi

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

    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
fi

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

    tar $BINUTILS_UNTAR_OPTIONS $SRCDIR/$BINUTILS_TARBALL || exit 1;
    patch -p0 < $BINUTILS_PATCH || exit 1;
    tar $GCC_UNTAR_OPTIONS $SRCDIR/$GCC_TARBALL || exit 1;
fi

echo "Unpacking icons"
tar $OOO_ICONS_UNTAR_OPTIONS $SRCDIR/$OOO_ICONS_TARBALL || exit 1;

if test "z$BUILD_WIN32" != "z"; then
    echo "Unpacking scaled icons"
    tar $OOO_SCALED_ICONS_UNTAR_OPTIONS $SRCDIR/$OOO_SCALED_ICONS_TARBALL || exit 1;
fi

echo "Unpacking OO.o build tree - [ go make some tea ] ..."
tar $OOO_UNTAR_OPTIONS $SRCDIR/$OOO_TARBALL || exit 1

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

# Win32 prerequisites ...
if test "z$BUILD_WIN32" != "z"; then

	    # ODMA
#	    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
fi

if 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
