#!/bin/sh

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

# source type DOWNLOAD is the same you get with CGIT, just downloaded from
# somewhere else
if test "$SOURCE_TYPE" = "DOWNLOAD" ; then
    SOURCE_TYPE=CGIT
fi

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

#"# don't confuse vim's syntax highlighting ;-)

check_tarball()
{
    if test "z$PIECE" != "z"; then
	echo "skipping looking for $1 ...";
    else
	$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
    fi
}

check_git_repo()
{
    tree="$CLONEDIR/$1"
    $echo_n "Checking for current branch of $tree ... $echo_c";
    BUILD_BRANCH=`git symbolic-ref HEAD|sed 's|.*/||'`
    if cd "$tree" >/dev/null 2>&1 ; then
        CURRENT_BRANCH=`git symbolic-ref HEAD|sed 's|.*/||'`
        if test "$CURRENT_BRANCH" = "$BUILD_BRANCH"; then
            echo "ok"
        else
            echo "warning, current branch is $CURRENT_BRANCH"
        fi
    else
        echo "missing the $tree clone; 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

# this will become the 'normal' way to do an all-through build name is misleading
ALL_CORE_PKGS="sdk ure base calc help extras writer impress artwork filters testing bootstrap libs-gui libs-core libs-extern libs-extern-sys components postprocess"
if test "$ENABLE_EXTENSIONS" = "YES"
then
       ALL_CORE_PKGS="$ALL_CORE_PKGS extensions"
fi
if test "z$OOO_GIT" = "z" ; then
    if test "z$SPLIT" = "zyes" ; then
	if test "z$PIECE" != "z"; then
	    CORE_PKGS=$PIECE
	else
	    CORE_PKGS=$ALL_CORE_PKGS
	fi
	if test "z$OOO_LANGS" != "z" -a "z$OOO_LANGS" != "zen-US" -a "z$PIECE" = "z" ; then
	    CORE_PKGS="$CORE_PKGS l10n"
	fi
	for pkg in $CORE_PKGS; do
	    check_tarball "$OOO_SPLIT_PREFIX$pkg-$OOO_SOURCEVERSION.tar.bz2"
	done
    # this is the legacy source splitting
    else
	CORE_PKGS=
	check_tarball $OOO_TARBALL
    fi
else
    if test "z$PIECE" != "z"; then
        CORE_PKGS=$PIECE
    else
        CORE_PKGS=$ALL_CORE_PKGS
    fi
    if test \( "z$OOO_LANGS" != "z" -a "z$OOO_LANGS" != "zen-US" -a "z$PIECE" = "z" \) -o "z$BUILD_WIN32" != "z"; then
        CORE_PKGS="$CORE_PKGS l10n"
    fi
    for pkg in $CORE_PKGS ; do
        check_git_repo $pkg
    done
fi

mkdir -p $BUILDDIR
cd $BUILDDIR

if test "z$BUILD_WIN32" != "z"; then
    case "$DISTRO" in
	NovellWin32*|GoOoWin32|Raw*)
	    ;;
	*)
	    check_file $UNICOWS_SRC
	    ;;
    esac
    check_file $DBGINST_SRC

    # Comment out the Mozilla source related bits for now
    #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

    # Prebuilt Mozilla bits
    check_file $SRCDIR/WNTMSCIinc.zip
    check_file $SRCDIR/WNTMSCIlib.zip
    check_file $SRCDIR/WNTMSCIruntime.zip

    # CRT80 that the prebuilt Mozilla needs and the
    # installer-builder wants to put in the installer
    for F in msvcr80.dll msvcp80.dll Microsoft.VC80.CRT.manifest; do
	$echo_n "Looking for $SRCDIR/$F ...  $echo_c"
	if test -f $SRCDIR/$F; then
	    echo "ok"
	else
	    echo "missing"
	    echo "You can find it in MSVS 2008, in VC/redist/x86/Microsoft.VC80.CRT."
	    echo "It is needed by the prebuilt Mozilla binaries that you should use."
	    exit 1
	fi
    done

     # msvcr71.dll that JRE needs
     for F in msvcr71.dll; do
 	$echo_n "Looking for $SRCDIR/$F ...  $echo_c"
 	if test -f $SRCDIR/$F; then
 	    echo "ok"
 	elif test -f "$WINDIR/system32/$F"; then
 	    echo "missing, but copied from $WINDIR/system32"
 	    cp "$WINDIR/system32/$F" $SRCDIR
 	else
 	    echo "missing"
 	    echo "You can find it in MSVS .NET 2003."
 	    echo "It is needed by JRE"
 	    exit 1
 	fi
     done
 
    # instmsi?.exe are the installers for Windows Installer 2.0 for
    # Win9x, Windows NT 4 and Windows 2000, that nobody really needs
    # (I hope). But the installer-builder insists on including them
    # with the installer.

    $echo_n "Looking for $SRCDIR/instmsiw.exe ... $echo_c"
    if test -f $SRCDIR/instmsiw.exe; 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=4B6140F9-2D36-4977-8FA1-6F8A0F5DCA8F&displaylang=en"
	echo "Note that the download requires Genuine Windows validation"
	echo "and can't easily be automated."
	exit 1
    fi

    $echo_n "Looking for $SRCDIR/gdiplus.dll ... $echo_c"
    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

    $echo_n "Looking for $SRCDIR/vcredist_x86.exe ... $echo_c"
    if test -f $SRCDIR/vcredist_x86.exe; 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 "Look for the version of 32-bit runtime redistribuable that corresponds"
	echo "to the version your Visual Studio links with"
	exit 1
    fi

    $echo_n "Looking for $SRCDIR/vcredist_x64.exe ... $echo_c"
    if test -f $SRCDIR/vcredist_x64.exe; 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 "Look for the version of 64-bit runtime redistribuable that corresponds"
	echo "to the version your Visual Studio links with"
	exit 1
    fi
fi

    case "$DISTRO" in
	NovellWin32*)
	    check_file $SRCDIR/$AGFA_MONOTYPE_FONTS_SOURCE
	    ;;
    esac

    case "$DISTRO" in
	NovellWin32*|GoOoWin32*)
	    check_file $SRCDIR/writingaids-$PREBUILT_WRITINGAIDS_TIMESTAMP.zip
	    check_file $SRCDIR/writingaids-doc-$PREBUILT_WRITINGAIDS_TIMESTAMP.zip
	    check_file $SRCDIR/ooo-windows-icons-0.2.tar.bz2
	    ;;
    esac

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 xf - ) || exit 1;
        for P in $BINUTILS_PATCHES ; do
            patch -d "$BINUTILS_VER" -p1 -s < "$SRCDIR/$P"
        done
    fi
    ($GCC_DECOMPRESS_CMD $SRCDIR/$GCC_TARBALL | $GNUTAR xf - ) || exit 1;
    for P in $GCC_PATCHES ; do
        patch -d "$GCC_VER" -p1 -s < "$SRCDIR/$P"
    done
fi

echo "Unpacking LibreOffcice build tree - [ go and have some $DRINK ] ..."
if test "z$OOO_GIT" = "z" ; then
    if test "z$SPLIT" != "zyes"; then
	echo "Unpacking $OOO_TARBALL..."
	($OOO_DECOMPRESS_CMD $SRCDIR/$OOO_TARBALL | $GNUTAR xf - ) || exit 1
    else # split packages
	if test -d $OOBUILDDIR; then
	    echo "	removing old build dir: $OOBUILDDIR"
	    rm -rf $OOBUILDDIR
	fi
	mkdir $OOBUILDDIR
	if test "$SOURCE_TYPE" = "CGIT" -a -d "$BUILDDIR/ooo" ; then
	    echo "	removing the old unpack dir: $BUILDDIR/ooo"
	    rm -rf "$BUILDDIR/ooo"
	fi
	for pkg in $CORE_PKGS; do
            echo "	unpacking $pkg..."
            ($OOO_DECOMPRESS_CMD "$SRCDIR/$OOO_SPLIT_PREFIX$pkg-$OOO_SOURCEVERSION.tar.bz2" | $GNUTAR xf - ) || exit 1
            echo "	moving it into place..."
            mv $OOO_SPLIT_PREFIX$pkg-$OOO_SOURCEVERSION/* $OOBUILDDIR
            rmdir $OOO_SPLIT_PREFIX$pkg-$OOO_SOURCEVERSION
	done
	if test "z$PIECE" != "z"; then
            if test "$SOURCE_TYPE" = "CGIT" ; then
		ln -s "$BUILDDIR/$GITTAG" $OOBUILDDIR
            else
		ln -s "$BUILDDIR/$OOO_SOURCEDIRNAME-$PIECE" $OOBUILDDIR
            fi
            # ugly hack around odd positioning of this info
            echo "Unpacking setup_native .txt pieces"
            ($OOO_DECOMPRESS_CMD $SRCDIR/setup_native_packinfo-3.3.2.1.tar.bz2 | $GNUTAR xf - ) || exit
            # hack to patch new files in the root dir
            if test "z$PIECE" = "zbootstrap" ; then
                touch $OOBUILDDIR/boost.m4
            fi
	fi
    fi
else
    if test -d "$RAWBUILDDIR" ; then
        echo "* removing old nonsplit dir: $RAWBUILDDIR"
        rm -rf "$RAWBUILDDIR"
    fi
    mkdir "$RAWBUILDDIR"
    echo "* creating symlinks in $RAWBUILDDIR"
    for pkg in $CORE_PKGS; do
	if test "$pkg" != "l10n" ; then
	    # l10n cannot be easily handled in nonsplit/, we only copy it to
	    # build dir (the one with the applied patches)
	    ln -s "$CLONEDIR/$pkg"/* "$RAWBUILDDIR/"
	fi
    done
    if test -d $OOBUILDDIR; then
        echo "* removing old build dir: $OOBUILDDIR"
        rm -rf $OOBUILDDIR
    fi
    echo "* copying $RAWBUILDDIR into $OOBUILDDIR..."
    #cp -pLR "$RAWBUILDDIR" "$OOBUILDDIR"
    touch "$CLONEDIR/repos_changed"
    rsync -prLt --exclude .git --exclude 'unx*.pro' --exclude 'wnt*.pro' "$RAWBUILDDIR/" "$OOBUILDDIR/"
    touch "$TOOLSDIR/stamp/refresh"
    if test -d "$CLONEDIR/l10n" && [ "$OOO_LANGS" != "en-US" ]; then
	echo "* copying translations into $OOBUILDDIR..."
	rsync -a --exclude .git $CLONEDIR/l10n/* $OOBUILDDIR/
    fi
fi

# original upstream sources
if test "z$SPLIT" != "zyes"; then
    # -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 xf - )
    fi
    # -binfilter source
    if test -f $SRCDIR/$OOO_BINFILTER_TARBALL; then
        echo "Unpacking $OOO_BINFILTER_TARBALL..."
        ($OOO_DECOMPRESS_CMD $SRCDIR/$OOO_BINFILTER_TARBALL | $GNUTAR xf - )
    fi
    # -lang source
    if test -f $SRCDIR/$OOO_LANG_TARBALL; then
        echo "Unpacking $OOO_LANG_TARBALL..."
        ($OOO_DECOMPRESS_CMD $SRCDIR/$OOO_LANG_TARBALL | $GNUTAR xf - )
    fi
    if test -f $SRCDIR/$OOO_EXTENSIONS_TARBALL; then
        echo "Unpacking $OOO_EXTENSIONS_TARBALL..."
        ($OOO_DECOMPRESS_CMD $SRCDIR/$OOO_EXTENSIONS_TARBALL | $GNUTAR xf - )
    fi
fi

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

echo "Fixing unfortunate snafus"
cd $BUILDDIR

# workaround for i#95634, bnc#571489

if test "z$PIECE" = "z"; then
    if [ -d $OOBUILDDIR/l10n ]; then
    for file in `find $OOBUILDDIR/l10n -type f -name "localize.sdf"` ; do
	chmod 644 $file
	perl -i -pe 's/(instsetoo_native\tinc_openoffice\\windows\\msi_languages\\Control.ulf.*OOO_CONTROL_124.*)(Sun Microsystems, Inc.|Oracle)/\1$ENV{"VENDORNAME"}/' $file
    done
    fi
fi

# copy in the prebuilt jsr173_1.0_api.jar if available
# allows to build with JDK 1.4
JSR_API_JAR=jsr173_1.0_api.jar
if test -f "$SRCDIR/$JSR_API_JAR"; then
    mkdir -p $OOBUILDDIR/stax/download
    cp -f $SRCDIR/$JSR_API_JAR $OOBUILDDIR/stax/download
fi

# copy in misc Mono related dlls if we need to
# we can't build them on Unix.
OOO_CLI_PREBUILT_ARCHIVE=ooo-cli-prebuilt-3.3.tar.bz2
if test "z$PIECE" = "z"; then
if test -f "$SRCDIR/$OOO_CLI_PREBUILT_ARCHIVE"; then
    mkdir -p $OOBUILDDIR/external/cli
    ( cd $OOBUILDDIR/external/cli; bzip2 -dc $SRCDIR/$OOO_CLI_PREBUILT_ARCHIVE | $GNUTAR xf - ) || exit 1;
fi
fi

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

    # gdiplus.dll
    if test -f $OOBUILDDIR/external/gdiplus/gdiplus.dll; then
        echo "Already have gdiplus.dll"
    else
        cp -p $SRCDIR/gdiplus.dll $OOBUILDDIR/external/gdiplus/gdiplus.dll
    fi

    # vcredist_x86.exe
    if test -f $OOBUILDDIR/external/vcredist/vcredist_x86.exe; then
        echo "Already have vcredist_x86.exe"
    else
        cp -p $SRCDIR/vcredist_x86.exe $OOBUILDDIR/external/vcredist/vcredist_x86.exe
    fi

    # vcredist_x64.exe
    if test -f $OOBUILDDIR/external/vcredist/vcredist_x64.exe; then
        echo "Already have vcredist_x86.exe"
    else
        cp -p $SRCDIR/vcredist_x64.exe $OOBUILDDIR/external/vcredist/vcredist_x64.exe
    fi

    # dbghelp
    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
    case "$DISTRO" in
	NovellWin32*|GoOoWin32*)
	    ;;
	*)
	    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
	    ;;
    esac

    # Bypass the Mozilla source bits for now
    if false; then

    if test -f $SRCDIR/$BUILD_MOZILLA_SOURCE;  then
        $echo_n "Installing Mozilla source $echo_c"
	cp -f $SRCDIR/$BUILD_MOZILLA_SOURCE $OOBUILDDIR/moz/download || exit 1;
	echo "done";
    else
	echo "Don't need to unpack Mozilla sources";
    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 $OOBUILDDIR/moz/download/$MOZILLA_IDL_SRC; then
	echo "Already have Mozilla libIDL sources";
    else
        $echo_n "Installing Mozilla libIDL source $echo_c"
	cp -f $SRCDIR/$MOZILLA_IDL_SRC $OOBUILDDIR/moz/download || exit 1;
	cp -f $SRCDIR/$MOZILLA_IDL_PATCH $OOBUILDDIR/moz/download || exit 1;
	echo "done";
    fi

    if test -f $OOBUILDDIR/moz/download/$MOZILLA_GLIB_SRC; then
	echo "Already have Mozilla GLib source";
    else
        $echo_n "Installing Mozilla GLib source $echo_c"
	cp -f $SRCDIR/$MOZILLA_GLIB_SRC $OOBUILDDIR/moz/download || exit 1;
	cp -f $SRCDIR/$MOZILLA_GLIB_PATCH $OOBUILDDIR/moz/download || exit 1;
	echo "done";
    fi

    if test -f $OOBUILDDIR/moz/download/$MOZILLA_WINTOOLS_BIN; then
	echo "Already have Mozilla wintools";
    else
        $echo_n "Installing Mozilla wintools $echo_c"
	cp -f $SRCDIR/$MOZILLA_WINTOOLS_BIN $OOBUILDDIR/moz/download || exit 1;
	echo "done";
    fi

    fi # bypass for now

    MOZILLA_PREBUILT_INC='WNTMSCIinc.zip'
    MOZILLA_PREBUILT_LIB='WNTMSCIlib.zip'
    MOZILLA_PREBUILT_RUNTIME='WNTMSCIruntime.zip'

    if test -f $OOBUILDDIR/moz/zipped/$MOZILLA_PREBUILT_INC -a \
	    -f $OOBUILDDIR/moz/zipped/$MOZILLA_PREBUILT_LIB -a \
	    -f $OOBUILDDIR/moz/zipped/$MOZILLA_PREBUILT_RUNTIME; then
	echo "Already have the prebuilt Mozilla zip files";
    else
        $echo_n "Installing prebuilt Mozilla zip files $echo_c"
	cp -f $SRCDIR/$MOZILLA_PREBUILT_INC $OOBUILDDIR/moz/zipped || exit 1;
	cp -f $SRCDIR/$MOZILLA_PREBUILT_LIB $OOBUILDDIR/moz/zipped || exit 1;
	cp -f $SRCDIR/$MOZILLA_PREBUILT_RUNTIME $OOBUILDDIR/moz/zipped || exit 1;
	echo "done";
    fi

    if test -f $OOBUILDDIR/external/msvcp80/msvcr80.dll -a \
            -f $OOBUILDDIR/external/msvcp80/msvcp80.dll -a \
            -f $OOBUILDDIR/external/msvcp80/Microsoft.VC80.CRT.manifest; then
	echo "Already have msvcr80.dll, msvcp80.dll and Microsoft.VC80.CRT.manifest"
    else
	cp -p $SRCDIR/msvcr80.dll $OOBUILDDIR/external/msvcp80
	cp -p $SRCDIR/msvcp80.dll $OOBUILDDIR/external/msvcp80
	cp -p $SRCDIR/Microsoft.VC80.CRT.manifest $OOBUILDDIR/external/msvcp80
    fi

    if test "z`uname -s`" = "zInterix"; then
	if test "$PROCESSOR_ARCHITECTURE" = AMD64; then
	    CommonProgsX86="`env|grep '^COMMONPROGRAMFILES(X86)='|sed -e 's/.*=//'`"
	else
	    CommonProgsX86="$COMMONPROGRAMFILES"
	fi
	case "$CommonProgsX86" in
	    ?:*)
	        CommonProgsX86=`winpath2unix "$CommonProgsX86"`
		;;
	esac
    else
	CommonProgsX86="$COMMONPROGRAMFILES"
    fi

     if test -f $OOBUILDDIR/external/msvcp71/msvcr71.dll; then
 	echo "Already have msvcr71.dll"
     else
       cp -p $SRCDIR/msvcr71.dll $OOBUILDDIR/external/msvcp71
     fi
 
    if test -f $OOBUILDDIR/external/msm90/Microsoft_VC90_CRT_x86.msm -a \
            -f $OOBUILDDIR/external/msm90/policy_9_0_Microsoft_VC90_CRT_x86.msm; then
	echo "Already have Microsoft_VC90_CRT_x86.msm and policy_9_0_Microsoft_VC90_CRT_x86.msm"
    elif test -f "$CommonProgsX86/Merge Modules/Microsoft_VC90_CRT_x86.msm" -a \
              -f "$CommonProgsX86/Merge Modules/policy_9_0_Microsoft_VC90_CRT_x86.msm"; then
	echo "Copying Microsoft VC9.0 CRT Merge Modules"
	cp -p "$CommonProgsX86/Merge Modules/Microsoft_VC90_CRT_x86.msm" $OOBUILDDIR/external/msm90
	cp -p "$CommonProgsX86/Merge Modules/policy_9_0_Microsoft_VC90_CRT_x86.msm" $OOBUILDDIR/external/msm90
    else
	echo "Missing Microsoft VC9.0 CRT Merge Modules:"
	echo "Microsoft_VC90_CRT_x86.msm and policy_9_0_Microsoft_VC90_CRT_x86.msm."
	echo "Did not find them in $CommonProgsX86/Merge Modules"
	echo "Do you have a Visual Studio 2008 installation?"
	exit 1
    fi
 
   if test -f $OOBUILDDIR/external/msi/instmsiw.exe; then
	echo "Already have instmsiw.exe"
    else
	cp $SRCDIR/instmsiw.exe $OOBUILDDIR/external/msi
    fi

    case "$DISTRO" in
        NovellWin32*)
            if test -f $SRCDIR/$AGFA_MONOTYPE_FONTS_SOURCE; then
                agfa_md5=`md5sum $SRCDIR/$AGFA_MONOTYPE_FONTS_SOURCE | cut -d' ' -f1`
                if test -f $OOBUILDDIR/agfa_monotype_fonts/download/${agfa_md5}-agfa-monotype-fonts.tar.gz; then
                    echo "Already have the Agfa Monotype fonts"
                else
                    $echo_n "Installing Agfa Monotype fonts $echo_c"
                    mkdir -p $OOBUILDDIR/agfa_monotype_fonts/download
                    cp -f $SRCDIR/$AGFA_MONOTYPE_FONTS_SOURCE $OOBUILDDIR/agfa_monotype_fonts/download/${agfa_md5}-agfa-monotype-fonts.tar.gz || 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
                echo Missing $SRCDIR/$AGFA_MONOTYPE_FONTS_SOURCE
                echo which should always be included when building $DISTRO
                exit 1
            fi
            ;;
    esac

    # 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.2.tar.bz2 | $GNUTAR xf -
	    mv ico/ooo3_*.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
        cp $SRCDIR/$BUILD_MOZILLA_SOURCE $OOBUILDDIR/moz/download/ || exit 1
    fi

fi

if test "z$DISTRO" != "zOOo4Kids"; then
    if test "z$BUILD_WIN32" != "z"; then
	    check_file $SRCDIR/$APACHE_ANT_TARBALL
    else
	ANT_VERSION=`ant -version | awk '{ print $4 }'`
	if test -x /usr/bin/ant -a \
	    "`echo $ANT_VERSION | cut -d. -f1`" -eq "1" \
		-a "`echo $ANT_VERSION | cut -d. -f2`" -lt "7" \
		-a "`echo $DISTRO`" != "SUSE-10.1" ; then
		check_file $SRCDIR/$APACHE_ANT_TARBALL
	fi
    fi
fi

if test -f $SRCDIR/$APACHE_ANT_TARBALL; then
    $echo_n "Extracting ant $echo_c"
    (gunzip -c $SRCDIR/$APACHE_ANT_TARBALL | $GNUTAR xf - )|| exit 1;
    echo "done";
else
    echo "Don't need to unpack ant";
fi


case "$DISTRO" in
    NovellWin32*|GoOoWin32*)
    echo "Creating prebuilt writing aids extension (dict-bundle.oxt)..."

    # Create prebuilt dict-bundle.oxt

    tmpdir=`mktemp -d`
    (cd $tmpdir
    unzip -q $SRCDIR/writingaids-$PREBUILT_WRITINGAIDS_TIMESTAMP.zip
    unzip -q $SRCDIR/writingaids-doc-$PREBUILT_WRITINGAIDS_TIMESTAMP.zip

    echo '<?xml version="1.0" encoding="UTF-8"?>
<description xmlns="http://openoffice.org/extensions/description/2006" xmlns:d="http://openoffice.org/extensions/description/2006"  xmlns:xlink="http://www.w3.org/1999/xlink">
    <version value="'$PREBUILT_WRITINGAIDS_TIMESTAMP.`date +%Y%m%d`'" />
    <identifier value="org.go-oo.dictionaries.bundle" />
    <display-name>
        <name lang="en">Built-in spelling and hyphenation dictionaries and thesauri</name>
    </display-name>
    <platform value="all" />
    <dependencies>
        <OpenOffice.org-minimal-version value="3.0" d:name="OpenOffice.org 3.0" />
    </dependencies>
</description>' >description.xml

    mkdir META-INF
    echo '<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE manifest:manifest PUBLIC "-//OpenOffice.org//DTD Manifest 1.0//EN" "Manifest.dtd">
<manifest:manifest xmlns:manifest="http://openoffice.org/2001/manifest">
    <manifest:file-entry manifest:media-type="application/vnd.sun.star.configuration-data"
        manifest:full-path="dictionaries.xcu"/>
</manifest:manifest>' >META-INF/manifest.xml

    # Read the dictionary.lst file and write a dictionaries.xcu file
    # Note that unlike upstream's dictionaries.xcu files,
    # we can't support several locales for one entry in the dictionary.lst
    # file. (For instance the upstream af_ZA dictionairs.xcu supports
    # both af-ZA and af-NA.)
    (
    echo '<?xml version="1.0" encoding="UTF-8"?>
<oor:component-data xmlns:oor="http://openoffice.org/2001/registry" xmlns:xs="http://www.w3.org/2001/XMLSchema" oor:name="Linguistic" oor:package="org.openoffice.Office">
 <node oor:name="ServiceManager">
    <node oor:name="Dictionaries">'
    while read type lang country root; do
	case "$type" in
	    \#*)
		;;
	    '')
		;;
	    DICT)
		echo '        <node oor:name="HunSpellDic_'$lang-$country'" oor:op="fuse">
	    <prop oor:name="Locations" oor:type="oor:string-list">
		<value>%origin%/'$root'.aff %origin%/'$root'.dic</value>
	    </prop>
	    <prop oor:name="Format" oor:type="xs:string">
		<value>DICT_SPELL</value>
	    </prop>
	    <prop oor:name="Locales" oor:type="oor:string-list">
		<value>'$lang-$country'</value>
	    </prop>
	</node>'
		;;
	    HYPH)
		echo '        <node oor:name="HyphDic_'$lang-$country'" oor:op="fuse">
	    <prop oor:name="Locations" oor:type="oor:string-list">
		<value>%origin%/'$root'.dic</value>
	    </prop>
	    <prop oor:name="Format" oor:type="xs:string">
		<value>DICT_HYPH</value>
	    </prop>
	    <prop oor:name="Locales" oor:type="oor:string-list">
		<value>'$lang-$country'</value>
	    </prop>
	</node>'
		;;
	    THES)
		echo '        <node oor:name="ThesDic_'$lang-$country'" oor:op="fuse">
	    <prop oor:name="Locations" oor:type="oor:string-list">
		<value>%origin%/'$root'.dat %origin%/'$root'.idx</value>
	    </prop>
	    <prop oor:name="Format" oor:type="xs:string">
		<value>DICT_THES</value>
	    </prop>
	    <prop oor:name="Locales" oor:type="oor:string-list">
		<value>'$lang-$country'</value>
	    </prop>
	</node>'
		;;
	    *)
		echo "Unknown code '$type' in dictionary.lst in writingaids-$PREBUILT_WRITINGAIDS_TIMESTAMP.zip" >&2
		exit 1
		;;
	esac
    done <dictionary.lst

    echo '    </node>
 </node>
</oor:component-data>'
    ) >dictionaries.xcu

    rm dictionary.lst

    mkdir -p $OOBUILDDIR/dictionaries/prebuilt
    rm -f $OOBUILDDIR/dictionaries/prebuilt/dict-bundle.oxt
    zip -qDr $OOBUILDDIR/dictionaries/prebuilt/dict-bundle.oxt .)
    rm -rf $tmpdir
    ;;
esac

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

if test "z$PIECE" = "z"; then

if test "x$OOO_EXTRA_ARTWORK" != "x"; then
# Html export rulers etc.
    cd $OOBUILDDIR
    ( bzip2 -dc $SRCDIR/$OOO_EXTRA_ARTWORK | $GNUTAR xf - ) || 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 --exclude={"README","LICENSE"} || 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$DEJAVU_FONTS_VER" != "x"; then
    echo "Correcting the package names of DejaVu font packages to $DEJAVU_FONTS_PACK_NAME"
    sed -e "s|TARFILE_NAME=.*$|TARFILE_NAME=$DEJAVU_FONTS_PACK_NAME|" -e "s|TARFILE_ROOTDIR=.*$|TARFILE_ROOTDIR=$DEJAVU_FONTS_PACK_NAME|" $OOBUILDDIR/more_fonts/fonts/ttf_dejavu/makefile.mk >  $OOBUILDDIR/more_fonts/fonts/ttf_dejavu/makefile.mk.ver
    mv $OOBUILDDIR/more_fonts/fonts/ttf_dejavu/makefile.mk.ver $OOBUILDDIR/more_fonts/fonts/ttf_dejavu/makefile.mk
    echo "done"
    echo "Correcting the delivery file of DejaVu Font packages"
    sed -e "s|dejavu-fonts-ttf-.*|$DEJAVU_FONTS_PACK_NAME|"  $OOBUILDDIR/more_fonts/prj/d.lst > $OOBUILDDIR/more_fonts/prj/d.lst.ver
    mv $OOBUILDDIR/more_fonts/prj/d.lst.ver $OOBUILDDIR/more_fonts/prj/d.lst
    echo "done"

fi

if test "x$LIBERATION_FONTS_VER" != "x"; then
    mkdir -p $OOBUILDDIR/bitstream_vera_fonts/download/
    LIBERATION_FONTSDEST=$OOBUILDDIR/bitstream_vera_fonts/download/liberation-fonts.zip
    echo "Updating Liberation fonts zip archive to version $LIBERATION_FONTS_VER"
    LIBERATION_FONTSSRC=liberation-fonts-ttf-$LIBERATION_FONTS_VER.zip
    cp -f $SRCDIR/$LIBERATION_FONTSSRC $LIBERATION_FONTSDEST || exit 1;
    echo "done"
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;
    cp $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;
    cp $SRCDIR/$GRAPHITE_SRC $GRAPHITE_DEST || exit 1;
fi

$TOOLSDIR/bin/piece/unpack-extras $TOOLSDIR $OOBUILDDIR || exit 1;

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

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
    cp $SRCDIR/cairo-${CAIRO_VER}.tar.gz $OOBUILDDIR/cairo/download/
fi

# OxygenOffice extras
for pack in $OOOP_FONTS_PACK $OOOP_GALLERY_PACK $OOOP_SAMPLES_PACK $OOOP_TEMPLATES_PACK ; do
    check_file $SRCDIR/$pack
    echo "Unpacking $pack"
    unzip -o -q $SRCDIR/$pack -d $OOBUILDDIR/extras/source
done

# Sun Professional Templates Pack
for pack in $SUNTEMPLATES_LANG ; do
    check_file $SRCDIR/Sun_ODF_Template_Pack_$pack.oxt
    echo "Copying Sun_ODF_Template_Pack_$pack.oxt"
    mkdir -p $OOBUILDDIR/extras/source/extensions/ || exit 1
    cp $SRCDIR/Sun_ODF_Template_Pack_$pack.oxt $OOBUILDDIR/extras/source/extensions/ || exit 1
done

for pack in $LIGHTPROOF_FILENAMES ; do
    check_file $SRCDIR/$pack
    echo "Copying LightProof extension ($pack) into the tree"
    mkdir -p $OOBUILDDIR/extras/source/extensions/ || exit 1
    cp $SRCDIR/$pack $OOBUILDDIR/extras/source/extensions/ || exit 1
done

if test "x$NUMBERTEXT_EXTENSION" != "x"; then
    check_file $SRCDIR/$NUMBERTEXT_EXTENSION
    echo "Copying NumberText extension into the tree"
    mkdir -p $OOBUILDDIR/extras/source/extensions/ || exit 1
    cp $SRCDIR/$NUMBERTEXT_EXTENSION $OOBUILDDIR/extras/source/extensions/numbertext.oxt || exit 1
fi

if test "x$HUNART_EXTENSION" != "x"; then
    check_file $SRCDIR/$HUNART_EXTENSION
    echo "Copying Hungarian Cross/reference Toolbar extension into the tree"
    mkdir -p $OOBUILDDIR/extras/source/extensions/ || exit 1
    cp $SRCDIR/$HUNART_EXTENSION $OOBUILDDIR/extras/source/extensions/hunart.oxt || exit 1
fi

if test "x$TYPO_EXTENSION" != "x"; then
    check_file $SRCDIR/$TYPO_EXTENSION
    echo "Copying Typography Toolbar extension into the tree"
    mkdir -p $OOBUILDDIR/extras/source/extensions/ || exit 1
    cp $SRCDIR/$TYPO_EXTENSION $OOBUILDDIR/extras/source/extensions/typo.oxt || exit 1
fi

if test "x$CT2N_EXTENSION" != "x"; then
    check_file $SRCDIR/$CT2N_EXTENSION
    echo "Linking ConvertTextToNumber extension sources to the expected name"
    md5=`md5sum $SRCDIR/$CT2N_EXTENSION | cut -d' ' -f1`
    ln -sf $SRCDIR/$CT2N_EXTENSION $SRCDIR/$md5-$CT2N_EXTENSION || exit 1
    # create empty module dir so bin/applyflags could force applying the build patches
    mkdir -p $OOBUILDDIR/ct2n
fi

if test "x$WATCH_WINDOW_EXTENSION" != "x"; then
    check_file $SRCDIR/$WATCH_WINDOW_EXTENSION
    echo "Copying Watch Window extension into the tree"
    mkdir -p $OOBUILDDIR/extras/source/extensions/ || exit 1
    cp $SRCDIR/$WATCH_WINDOW_EXTENSION $OOBUILDDIR/extras/source/extensions/WatchWindow.oxt || exit 1
fi

if test "x$DIAGRAM_EXTENSION" != "x"; then
    check_file $SRCDIR/$DIAGRAM_EXTENSION
    echo "Copying Diagram extension into the tree"
    mkdir -p $OOBUILDDIR/extras/source/extensions/ || exit 1
    cp $SRCDIR/$DIAGRAM_EXTENSION $OOBUILDDIR/extras/source/extensions/Diagram.oxt || exit 1
fi

if test "x$VALIDATOR_EXTENSION" != "x"; then
    check_file $SRCDIR/$VALIDATOR_EXTENSION
    echo "Copying Validator extension into the tree"
    mkdir -p $OOBUILDDIR/extras/source/extensions/ || exit 1
    cp $SRCDIR/$VALIDATOR_EXTENSION $OOBUILDDIR/extras/source/extensions/Validator.oxt || exit 1
fi

if test "x$BARCODE_EXTENSION" != "x"; then
    check_file $SRCDIR/$BARCODE_EXTENSION
    echo "Copying Barcode extension into the tree"
    mkdir -p $OOBUILDDIR/extras/source/extensions/ || exit 1
    cp $SRCDIR/$BARCODE_EXTENSION $OOBUILDDIR/extras/source/extensions/Barcode.oxt || exit 1
fi

if test "x$GOOGLE_DOCS_EXTENSION" != "x"; then
    check_file $SRCDIR/$GOOGLE_DOCS_EXTENSION
    echo "Copying Google Docs extension into the tree"
    mkdir -p $OOBUILDDIR/extras/source/extensions/ || exit 1
    cp $SRCDIR/$GOOGLE_DOCS_EXTENSION $OOBUILDDIR/extras/source/extensions/gdocs.oxt || exit 1
fi

if test "x$NLPSOLVER_EXTENSION" != "x"; then
    mkdir -p $OOBUILDDIR/nlpsolver
fi

if test "x$OOOBLOGGER_EXTENSION" != "x"; then
    check_file $SRCDIR/$OOOBLOGGER_EXTENSION
    echo "Copying oooblogger extension into the tree"
    mkdir -p $OOBUILDDIR/extras/source/extensions/ || exit 1
    cp $SRCDIR/$OOOBLOGGER_EXTENSION $OOBUILDDIR/extras/source/extensions/oooblogger.oxt || exit 1
fi

fi # PIECES hack
