#!/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
    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
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 "Fixing unfortunate snafus"
DUFF_FILE=$OOBUILDDIR/ooo_custom_images/industrial/res/commandimagelist/lc_starshapes.png
if grep 'Inkscape' $DUFF_FILE; then
    echo "removing bogus svg file"
    rm $DUFF_FILE;
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
cp -af $TOOLSDIR/bin/oowintool $OOBUILDDIR/config_office
chmod a+x $OOBUILDDIR/config_office/oowintool

# hack: fix possible packaging problem in 1.1.3
chmod a+x $OOBUILDDIR/icu/convert.sh

# Win32 prerequisites ...
if test "z$BUILD_WIN32" != "z"; then
    # System bits
    tar $OOO_UNTAR_OPTIONS $SRCDIR/$OOO_SYSTEM_TARBALL || exit 1

    # 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
else
    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
    if test -f $SRCDIR/$OOO_BINFILTER_TARBALL; then
    	tar $OOO_UNTAR_OPTIONS $SRCDIR/$OOO_BINFILTER_TARBALL
    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
    check_tarball $SRCDIR/openclipart-$OPENCLIPART_VER.tar.bz2
    cd $BUILDDIR
    tar xjf $SRCDIR/openclipart-$OPENCLIPART_VER.tar.bz2 || exit 1;
fi

echo "Copying default evolution database into tree"
cp -af $SRCDIR/evolocal.odb $OOBUILDDIR/extras/source/database
