#!/bin/sh

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

if (echo "testing\c"; echo 1,2,3) | grep c >/dev/null; then
    if (echo -n testing; echo 1,2,3) | sed s/-n/xn/ | grep xn >/dev/null; then
        echo_n=echo echo_c=
    else
        echo_n="echo -n" echo_c=
    fi
else
    echo_n=echo echo_c='\c'
fi

check_tarball()
{
    $echo_n "Looking for $1 ... $echo_c";
    if test -f $1; then
	echo "ok"
    else
	echo "missing the $1 archive; run './download'"
	exit 1;
    fi
}

check_file()
{
    $echo_n "Looking for $1 ... $echo_c";
    if test -f $1; then
	echo "ok"
    else
	echo "missing required archive; run ./download'";
	exit 1;
    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 "ok"
    else
	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
    fi
    case "$DISTRO" in
	NovellWin32*|GoOoWin32*)
	    check_file $SRCDIR/writingaids-$PREBUILT_WRITINGAIDS_TIMESTAMP.zip
	    check_file $SRCDIR/writingaids-doc-$PREBUILT_WRITINGAIDS_TIMESTAMP.zip
	    ;;
    esac
fi

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

    if test "z$BINUTILS_TARBALL" != "z"; then
	($BINUTILS_DECOMPRESS_CMD $SRCDIR/$BINUTILS_TARBALL | $GNUTAR xpf - ) || exit 1;
	patch -p0 < $BINUTILS_PATCH || exit 1;
    fi
    ($GCC_DECOMPRESS_CMD $SRCDIR/$GCC_TARBALL | $GNUTAR xpf - ) || 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 and have some $DRINK ] ..."
echo "Unpacking $OOO_TARBALL..."
($OOO_DECOMPRESS_CMD $SRCDIR/$OOO_TARBALL | $GNUTAR xpf - ) || 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..."
    ($OOO_DECOMPRESS_CMD $SRCDIR/$OOO_SYSTEM_TARBALL | $GNUTAR xpf - )
fi
# -binfilter source
if test -f $SRCDIR/$OOO_BINFILTER_TARBALL; then
   echo "Unpacking $OOO_BINFILTER_TARBALL..."
   ($OOO_DECOMPRESS_CMD $SRCDIR/$OOO_BINFILTER_TARBALL | $GNUTAR xpf - )
fi
# -lang source
if test -f $SRCDIR/$OOO_LANG_TARBALL; then
   echo "Unpacking $OOO_LANG_TARBALL..."
    ($OOO_DECOMPRESS_CMD $SRCDIR/$OOO_LANG_TARBALL | $GNUTAR xpf - )
fi
# -sdk_oo source
if test -f $SRCDIR/$OOO_SDK_OO_TARBALL; then
   echo "Unpacking $OOO_SDK_OO_TARBALL"
    ($OOO_DECOMPRESS_CMD $SRCDIR/$OOO_SDK_OO_TARBALL | $GNUTAR xpf - )
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"
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
#    $GNUCP $OOBUILDDIR/officecfg/registry/component-schema.dtd $OOBUILDDIR/officecfg/registry/schema
# fi

# workaround for i#56622, n#210797
# FIXME: Must be done even when Hungarian language is not build because
#        the changes conflicts with some patches :-(
echo "Fixing Hungarian strings: OOo vs. SO"
for file in `find $OOBUILDDIR -type f -name "localize.sdf"` ; do
    chmod 644 $file
    $TOOLSDIR/bin/a2az.pl $file >$file.a2az
    mv $file.a2az $file
done

# 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
    $GNUCP -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
    $GNUCP -af $SRCDIR/$CLI_TYPES $SRCDIR/cli_types_bridgetest.dll $OOBUILDDIR/external/cli
fi

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

    echo "Removing CRs from a couple of files"
    dos2unix $BUILDDIR/hsqldb/hsqldb_1_8_0
    dos2unix $SRCDIR/../patches/src680/build-java-target.diff

# FIXME: add check for system bits ...

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

    # gdiplus.dll
    cd $BUILDDIR;
    if test -f $OOBUILDDIR/external/gdiplus/gdiplus.dll; then
    	echo "Already copied gdiplus.dll"
    else
	$GNUCP -p $SRCDIR/gdiplus.dll $OOBUILDDIR/external/gdiplus/gdiplus.dll
    fi

    # dbghelp
    cd $BUILDDIR;
    if test -f $OOBUILDDIR/external/dbghelp/DbgHelp.Dll; then
	echo "Already have dbghelp";
    else
	$echo_n "Extracting dbghelp $echo_c"
	cabextract -q $DBGINST_SRC
	TARGET=dbghelp.exe
	if test -f $TARGET; then
	    unzip -o $TARGET
	    mv -f License.Txt dbghelp-license.txt
            mv -f redist.txt dbghelp-redist.txt
	    echo "done";
	else
	    echo "failed";
	    exit 1;
	fi
    fi
    
    # unicows
    cd $BUILDDIR;
    if test -f $OOBUILDDIR/external/unicows/unicows.dll; then
	echo "Already have unicows";
    else
	$echo_n "Extracting unicows $echo_c"
	cabextract -q $UNICOWS_SRC
	TARGET=unicows.dll
	if test -f $TARGET; then
	    mv -f license.txt unicows-license.txt
	    mv -f redist.txt unicows-redist.txt
	    echo "done";
	else
	    echo "failed";
	    exit 1;
	fi
    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 $echo_c"
	(gunzip -dc $TOOLSDIR/src/$APACHE_ANT_TARBALL | $GNUTAR xpf - )|| 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 $echo_c"
	$GNUCP -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 $echo_c"
	$GNUCP -f $TOOLSDIR/src/$MOZILLA_IDL_SRC $OOBUILDDIR/moz/download || exit 1;
	$GNUCP -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 $echo_c"
	$GNUCP -f $TOOLSDIR/src/$MOZILLA_GLIB_SRC $OOBUILDDIR/moz/download || exit 1;
	$GNUCP -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 $echo_c"
	$GNUCP -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 "Already have Agfa Monotype fonts"
	else
	    $echo_n "Installing Agfa Monotype fonts $echo_c"
	    mkdir -p $OOBUILDDIR/agfa_monotype_fonts/download
	    $GNUCP -f $TOOLSDIR/src/$AGFA_MONOTYPE_FONTS_SOURCE $OOBUILDDIR/agfa_monotype_fonts/download || exit 1
	    echo "done"
        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
    else
        case "$DISTRO" in
	    NovellWin32*)
	        echo missing $TOOLSDIR/src/$AGFA_MONOTYPE_FONTS_SOURCE
		echo which should always be included when building $DISTRO
		exit 1
		;;
	esac
    fi

    case "$DISTRO" in
	NovellWin32*|GoOoWin32*)
	    # Create prebuilt writingaids.zip
	    tmpdir=`mktemp -d`
	    (cd $tmpdir
	    unzip $SRCDIR/writingaids-$PREBUILT_WRITINGAIDS_TIMESTAMP.zip
	    unzip $SRCDIR/writingaids-doc-$PREBUILT_WRITINGAIDS_TIMESTAMP.zip
	    mkdir $OOBUILDDIR/dictionaries/prebuilt
	    zip -r $OOBUILDDIR/dictionaries/prebuilt/writingaids.zip .)
	    rm -rf $tmpdir
	    ;;
    esac

    if test "x$DEJAVU_FONTS_VER" != "x"; then
        DEJAVU_FONTSDEST=$OOBUILDDIR/dejavufonts/download
	if [ "$DEJAVU_FONTS_VER" -gt "2.11" ]; then
		DEJAVU_FONTSSRC=dejavu-ttf-$DEJAVU_FONTS_VER.tar.bz2
	else
		DEJAVU_FONTSSRC=dejavu-ttf-$DEJAVU_FONTS_VER.tar.gz
	fi
        if test -f $DEJAVU_FONTSDEST/$DEJAVU_FONTSSRC; then
            echo "Already have DejaVu fonts"
        else
            echo "Installing DejaVu fonts"
            mkdir -p $DEJAVU_FONTSDEST || exit 1;
            $GNUCP -f $SRCDIR/$DEJAVU_FONTSSRC $DEJAVU_FONTSDEST || exit 1;
            echo "done"
        fi
    fi

    # Icons.
    # Just plunk them on top of the upstream ones for now. I'm
    # too lazy to add configury to make it optional and to check for
    # existence of the tarball, etc.
    case "$DISTRO" in
	# Add other Win32 "distros" here if they too want to use
	# these icons
        NovellWin32*|GoOoWin32*)
	    bzip2 -dc $SRCDIR/ooo-windows-icons-0.1.tar.bz2 | $GNUTAR xpf -
	    rm ico/ooo-web-app.ico
	    mv ico/ooo-*.ico $OOBUILDDIR/sysui/desktop/icons
	    ;;
    esac
else

    if test -f $SRCDIR/$BUILD_MOZILLA_SOURCE; then
        echo "Copying mozilla package into the tree"
        mkdir -p $OOBUILDDIR/moz/download || exit 1
        $GNUCP -a $SRCDIR/$BUILD_MOZILLA_SOURCE $OOBUILDDIR/moz/download/ || exit 1
    fi

fi


if test "z$SYSTEM_GCC" = "z"; then
    echo "Checking for gcc sources and prerequisities"
    if test -d "$BUILDDIR/$GCC_VER" &&
	test -d "$BUILDDIR/$BINUTILS_VER"; then
	echo "ok"
    else
        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 -d $OOBUILDDIR/ooo_custom_images/tango/res ; then
    echo "Installing Tango icon for the GNOME quickstarter applet..."
    $GNUCP $TOOLSDIR/src/tango_mainapp_16.png $OOBUILDDIR/ooo_custom_images/tango/res/mainapp_16.png || exit 1;
fi

if test "x$OOO_EXTRA_ARTWORK" != "x"; then
# Html export rulers etc.
    cd $OOBUILDDIR
    ( bzip2 -dc $SRCDIR/$OOO_EXTRA_ARTWORK | $GNUTAR xpf - ) || exit 1;
fi

for i in $OOO_LANGS; do
	if test -f $SRCDIR/extras_$i.tar.gz; then
		echo "Extracting extra $i extras"
		cd $OOBUILDDIR
		$GNUTAR xzf $SRCDIR/extras_$i.tar.gz || exit 1
	fi
	if test -f $SRCDIR/help_images_$i.tar.gz; then
		echo "Extracting extra $i help images"
		cd $OOBUILDDIR
		$GNUTAR 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
    $GNUTAR xjf $SRCDIR/openclipart-$OPENCLIPART_VER.tar.bz2 || exit 1;
fi

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

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

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

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

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

if test "$CAIRO_ENABLED" = "TRUE" -a "$SYSTEM_CAIRO" != "TRUE"; then
    echo "Copying cairo into tree"
    mkdir -p $OOBUILDDIR/cairo
    check_tarball $SRCDIR/cairo-${CAIRO_VER}.tar.gz
    mkdir -p $OOBUILDDIR/cairo/download
    $GNUCP -a $SRCDIR/cairo-${CAIRO_VER}.tar.gz $OOBUILDDIR/cairo/download/
fi

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

if test "$SYSTEM_LIBWPD" != "YES"; then
  LIBWPD_PACKAGE=libwpd-0.8.12.tar.gz
  check_tarball $SRCDIR/$LIBWPD_PACKAGE
  echo "Copying libwpd package into the tree"
  mkdir -p $OOBUILDDIR/libwpd/download || exit 1
  $GNUCP -a $SRCDIR/$LIBWPD_PACKAGE $OOBUILDDIR/libwpd/download/ || exit 1
fi

if test "$SYSTEM_LIBWPS" != "YES"; then
  LIBWPS_PACKAGE=libwps-0.1.0.tar.gz
  check_tarball $SRCDIR/$LIBWPS_PACKAGE
  echo "Copying libwps package into the tree"
  mkdir -p $OOBUILDDIR/libwps/download || exit 1
  $GNUCP -a $SRCDIR/$LIBWPS_PACKAGE $OOBUILDDIR/libwps/download/ || exit 1
fi

if test "$SYSTEM_LIBWPG" != "YES"; then
  LIBWPG_PACKAGE=libwpg-0.1.0.tar.gz
  check_tarball $SRCDIR/$LIBWPG_PACKAGE
  echo "Copying libwpg package into the tree"
  mkdir -p $OOBUILDDIR/libwpg/download || exit 1
  $GNUCP -a $SRCDIR/$LIBWPG_PACKAGE $OOBUILDDIR/libwpg/download/ || exit 1
fi

if test "$SYSTEM_LIBSVG" != "YES"; then
  LIBSVG_PACKAGE=libsvg-0.1.4.tar.gz
  check_tarball $SRCDIR/$LIBSVG_PACKAGE
  echo "Copying libsvg package into the tree"
  mkdir -p $OOBUILDDIR/libsvg/download || exit 1
  $GNUCP -a $SRCDIR/$LIBSVG_PACKAGE $OOBUILDDIR/libsvg/download/ || exit 1
fi

# oox source
if test -f $SRCDIR/$OOX_MODULE_TARBALL; then
    echo "Unpacking $OOX_MODULE_TARBALL"
    cd $OOBUILDDIR
    ($OOO_DECOMPRESS_CMD $SRCDIR/$OOX_MODULE_TARBALL | $GNUTAR xpf - ) || exit 1
fi
