#!/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()
{
    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_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
if test "z$SPLIT" = "zyes" ; then
	if test "z$PIECE" != "z"; then
		CORE_PKGS=$PIECE
	else
		CORE_PKGS="sdk ure base calc l10n writer impress artwork filters testing bootstrap libs_gui libs_core libs_extern libs_extern_sys components postprocess extensions"
	fi
	for pkg in $CORE_PKGS; do
		check_tarball "$OOO_SPLIT_PREFIX$pkg.tar.bz2"
	done
# this is the legacy source splitting
else
	CORE_PKGS=
	check_tarball $OOO_TARBALL
fi

if test "$ENABLE_SCSOLVER" = "YES"; then
    # Check presence of scsolver (Calc Solver) package...
    SCSOLVER_PACKAGE=scsolver.2008-10-30.tar.bz2
    check_tarball $SRCDIR/$SCSOLVER_PACKAGE
fi

mkdir -p $BUILDDIR
cd $BUILDDIR

if test "z$BUILD_WIN32" != "z"; then
    check_file $UNICOWS_SRC
    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

    # msvc?71.dll that the prebuilt Mozilla needs and the
    # installer-builder wants to put in the installer
    for F in msvcr71.dll msvcp71.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 the prebuilt Mozilla binaries that you have to use"
	    echo "because Mozilla 1.7.5 can't be built with MSVS 2005."
	    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
    check_file $SRCDIR/instmsia.exe
    $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
fi

    case "$DISTRO" in
	NovellWin32*|GoOoWin32*)
	    check_file $SRCDIR/writingaids-$PREBUILT_WRITINGAIDS_TIMESTAMP.zip
	    check_file $SRCDIR/writingaids-doc-$PREBUILT_WRITINGAIDS_TIMESTAMP.zip
	    ;;
    esac

    case "$DISTRO" in
	OxygenOffice*|FSFhu*)
	    check_file $SRCDIR/extensionaids-$PREBUILT_EXTENSIONAIDS_TIMESTAMP.zip
	    ;;
    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 xpf - ) || 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 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 ] ..."
if test "z$SPLIT" != "zyes"; then
	echo "Unpacking $OOO_TARBALL..."
	($OOO_DECOMPRESS_CMD $SRCDIR/$OOO_TARBALL | $GNUTAR xpf - ) || exit 1
else # split packages
	if test -d $OOBUILDDIR; then
	    echo "	removing old build dir: $OOBUILDDIR"
	    rm -rf $OOBUILDDIR
	fi
	mkdir $OOBUILDDIR
	for pkg in $CORE_PKGS; do
		echo "	unpacking $pkg..."
		($OOO_DECOMPRESS_CMD "$SRCDIR/$OOO_SPLIT_PREFIX$pkg.tar.bz2" | $GNUTAR xpf - ) || exit 1
		echo "	moving it into place..."
		mv $OOO_SPLIT_PREFIX$pkg/* $OOBUILDDIR
		rmdir $OOO_SPLIT_PREFIX$pkg
	done
	if test "z$PIECE" != "z"; then
		ln -s "$BUILDDIR/$CVSTAG-$PIECE" $OOBUILDDIR
		# ugly hack around odd positioning of this info
		echo "Unpacking setup_native .txt pieces"
		($OOO_DECOMPRESS_CMD $SRCDIR/setup_native_packinfo-ooo300-m3.tar.bz2 | $GNUTAR xpf - ) || exit
	fi
fi

# -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
if test "z$SPLIT" != "zyes"; then
  if test -f $SRCDIR/$OOO_EXTENSIONS_TARBALL; then
     echo "Unpacking $OOO_EXTENSIONS_TARBALL..."
      ($OOO_DECOMPRESS_CMD $SRCDIR/$OOO_EXTENSIONS_TARBALL | $GNUTAR xpf - )
  fi
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

if test "z$PIECE" = "z"; then
case "$OOO_LANGS" in
*\ hu\ *|*\ hu|hu\ *|hu)
    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
    ;;
esac
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 "z$PIECE" = "z" -o "z$PIECE" = "zbootstrap" ; then
if test "$USE_PREBUILD_UNOWINREG_DLL" = "YES" ; then
    mkdir -p $OOBUILDDIR/external/unowinreg/ || exit 1;
    $GNUCP -f $SRCDIR/unowinreg.dll $OOBUILDDIR/external/unowinreg/ || exit 1;
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 "z$PIECE" = "z"; then
if test -f "$SRCDIR/$JSR_API_JAR"; then
    mkdir -p $OOBUILDDIR/external/StAX
    $GNUCP -af $SRCDIR/$JSR_API_JAR $OOBUILDDIR/external/StAX
fi
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.0.1.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 xpf - ) || exit 1;
fi
fi

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

    # gdiplus.dll
    cd $BUILDDIR;
    if test -f $OOBUILDDIR/external/gdiplus/gdiplus.dll; then
    	echo "Already have 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.7.0'
    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;

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

    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 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"
	$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";
    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"
	$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";
    fi

    if test -f $OOBUILDDIR/moz/download/$MOZILLA_WINTOOLS_BIN; then
	echo "Already have Mozilla wintools";
    else
        $echo_n "Installing Mozilla wintools $echo_c"
	$GNUCP -f $TOOLSDIR/src/$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"
	$GNUCP -f $TOOLSDIR/src/$MOZILLA_PREBUILT_INC $OOBUILDDIR/moz/zipped || exit 1;
	$GNUCP -f $TOOLSDIR/src/$MOZILLA_PREBUILT_LIB $OOBUILDDIR/moz/zipped || exit 1;
	$GNUCP -f $TOOLSDIR/src/$MOZILLA_PREBUILT_RUNTIME $OOBUILDDIR/moz/zipped || exit 1;
	echo "done";
    fi

    if test -f $OOBUILDDIR/external/msvcp71/msvcr71.dll -a \
            -f $OOBUILDDIR/external/msvcp71/msvcp71.dll; then
	echo "Already have msvcr71.dll and msvcp71.dll"
    else
	$GNUCP -p $SRCDIR/msvcr71.dll $OOBUILDDIR/external/msvcp71
	$GNUCP -p $SRCDIR/msvcp71.dll $OOBUILDDIR/external/msvcp71
    fi

    if test -f $OOBUILDDIR/external/msm90/Microsoft_VC90_CRT_x86.msm -a \
            -f $OOBUILDDIR/external/msm90policy_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 "$COMMONPROGRAMFILES/Merge Modules/Microsoft_VC90_CRT_x86.msm" -a \
              -f "$COMMONPROGRAMFILES/Merge Modules/policy_9_0_Microsoft_VC90_CRT_x86.msm"; then
	echo "Copying Microsoft VC9.0 CRT Merge Modules"
	$GNUCP -p "$COMMONPROGRAMFILES/Merge Modules/Microsoft_VC90_CRT_x86.msm" $OOBUILDDIR/external/msm90
	$GNUCP -p "$COMMONPROGRAMFILES/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 $COMMONPROGRAMFILES/Merge Modules"
	echo "Do you have a Visual Studio 2008 installation?"
	exit 1
    fi
 
   if test -f $OOBUILDDIR/external/msi/instmsia.exe -a \
           -f $OOBUILDDIR/external/msi/instmsiw.exe; then
	echo "Already have instmsia.exe and instmsiw.exe"
    else
	$GNUCP -p $SRCDIR/instmsia.exe $OOBUILDDIR/external/msi
	$GNUCP -p $SRCDIR/instmsiw.exe $OOBUILDDIR/external/msi
	chmod a+x $OOBUILDDIR/external/msi/instmsia.exe
	chmod a+x $OOBUILDDIR/external/msi/instmsiw.exe
    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 the 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

    # 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 xpf -
	    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
        $GNUCP -a $SRCDIR/$BUILD_MOZILLA_SOURCE $OOBUILDDIR/moz/download/ || exit 1
    fi
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

case "$DISTRO" in
	OxygenOffice*|FSFhu*)
	    echo "Extracting extension aids..."
	    unzip -q -o $SRCDIR/extensionaids-$PREBUILT_EXTENSIONAIDS_TIMESTAMP.zip -d $OOBUILDDIR/dictionaries
	    ;;
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

# 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 "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 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$DEJAVU_FONTS_VER" != "x"; then
    DEJAVU_FONTSDEST=$OOBUILDDIR/bitstream_vera_fonts/download/
    echo "Updating DejaVu fonts zip archive to version $DEJAVU_FONTS_VER"
    $GNUCP -f $SRCDIR/$DEJAVU_FONTS_PACK_NAME.zip $DEJAVU_FONTSDEST || exit 1;
    echo "done"
    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/bitstream_vera_fonts/fonts/ttf_dejavu/makefile.mk >  $OOBUILDDIR/bitstream_vera_fonts/fonts/ttf_dejavu/makefile.mk.ver
    mv $OOBUILDDIR/bitstream_vera_fonts/fonts/ttf_dejavu/makefile.mk.ver $OOBUILDDIR/bitstream_vera_fonts/fonts/ttf_dejavu/makefile.mk
    echo "done"
    echo "Correcting the delivery file of DejaVu Font packages"
    sed -e "s|dejavu-ttf-2.21|$DEJAVU_FONTS_PACK_NAME|"  $OOBUILDDIR/bitstream_vera_fonts/prj/d.lst > $OOBUILDDIR/bitstream_vera_fonts/prj/d.lst.ver
    mv $OOBUILDDIR/bitstream_vera_fonts/prj/d.lst.ver $OOBUILDDIR/bitstream_vera_fonts/prj/d.lst
    echo "done"

fi

if test "x$LIBERATION_FONTS_VER" != "x"; then
    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
    $GNUCP -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;
    $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

$TOOLSDIR/bin/piece/unpack-l10n $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
    $GNUCP -a $SRCDIR/cairo-${CAIRO_VER}.tar.gz $OOBUILDDIR/cairo/download/
fi

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

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

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

if test "$ENABLE_SCSOLVER" = "YES"; then
    # Unpack the scsolver package
    echo "Unpacking $SCSOLVER_PACKAGE"
    tar xfj $SRCDIR/$SCSOLVER_PACKAGE -C $OOBUILDDIR
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 -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 "Coping Sun_ODF_Template_Pack_$pack.oxt"
    mkdir -p $OOBUILDDIR/extras/source/extensions/ || exit 1
    $GNUCP -a $SRCDIR/Sun_ODF_Template_Pack_$pack.oxt -d $OOBUILDDIR/extras/source/extensions || exit 1
done

# Extra command images
echo "Copying extra command images"
$GNUCP -f $TOOLSDIR/src/icons/*.png $OOBUILDDIR/default_images/res/commandimagelist/ || exit 1
$GNUCP -f $TOOLSDIR/src/icons/tango/*.png $OOBUILDDIR/ooo_custom_images/tango/res/commandimagelist/ || exit 1

fi # PIECES hack
