# If we 'export' all our variables to forked processes
# we end up with a horrible mess - since we propagate
# eg. ACLOCAL_M4, AMDEP_TRUE, COMPATH etc.
# - a nightmare in a nutshell => we need to export on a
#   need-to-know / minimal basis.

STAMP_DIR=$(top_builddir)/stamp

# Source preparation

unpack : $(OOBUILDDIR)/unpack $(top_srcdir)/bin/unpack
$(OOBUILDDIR)/unpack :
	$(TOOLSDIR)/bin/transform --revert $(TOOLSDIR) $(OOBUILDDIR)
	if test -d $(OOBUILDDIR)/applied_patches ; then \
		FLAGS="--distro=$(DISTRO)"; \
		if test -d $(OOBUILDDIR)/binfilter ; then \
			FLAGS="$$FLAGS --distro=Binfilter"; \
		fi ; \
		if grep localize.sdf $(OOBUILDDIR)/sw/source/ui/app/localize.sdf > /dev/null 2>&1 ; then \
			FLAGS="$$FLAGS --distro=Localize"; \
		fi ; \
		$(TOOLSDIR)/patches/apply.pl $(APPLY_DIR) $(OOBUILDDIR) $$FLAGS -f -R ; \
	fi
	cd $(top_srcdir)/bin ; ./unpack
	rm -f $(STAMP_DIR)/build $(STAMP_DIR)/patch.apply \
	      $(STAMP_DIR)/artwork.install
	touch $@

artwork.install : $(STAMP_DIR)/artwork.install
$(STAMP_DIR)/artwork.install : $(OOBUILDDIR)/unpack \
			      $(STAMP_DIR)/prebuild \
			      $(wildcard $(top_srcdir)/src/*.png) \
			      $(wildcard $(top_srcdir)/src/*.bmp)
	mkdir -p $(OOBUILDDIR)/svx/res
	cp $(top_srcdir)/src/ooobitmap_*.bmp $(OOBUILDDIR)/setup_native/source/win32/nsis/ || echo "no nsis installer images to copy"
	cp $(top_srcdir)/src/ooobanner_*.bmp $(OOBUILDDIR)/setup_native/source/win32/nsis/ || echo "no nsis banner images to copy"
	cp $(top_srcdir)/src/ooosetup_*.ico $(OOBUILDDIR)/setup_native/source/win32/nsis/ || echo "no installer icon to copy"
	cp $(top_srcdir)/src/nologoinstall_*.bmp $(OOBUILDDIR)/instsetoo_native/res/ || echo "no installer sidebar images to copy"
	cp $(top_srcdir)/src/banner_*.bmp $(OOBUILDDIR)/instsetoo_native/res/ || echo "no installer banner images to copy"
	cp $(top_srcdir)/src/open*_*.bmp $(OOBUILDDIR)/svx/res/ || echo "no intro images to copy"
	cp $(top_srcdir)/src/open*_*.png $(OOBUILDDIR)/default_images/svx/res/ || echo "no about images to copy"
	cp $(top_srcdir)/src/*-team.png $(OOBUILDDIR)/default_images/sw/res/ || echo "no team images to copy"
	cp -vf $(TOOLSDIR)/fonts/opens___.ttf $(OOBUILDDIR)/extras/source/truetype/symbol || echo "font copy failed";
	touch $@

patch.apply: $(OOBUILDDIR)/unpack $(STAMP_DIR)/patch.apply 
$(STAMP_DIR)/patch.apply : $(top_srcdir)/patches/apply.pl \
			   $(top_srcdir)/patches/*/*.diff \
			   $(APPLY_DIR)/apply
	if test "z$(BUILD_WIN32)" != "z"; then \
	    cp -af $(BUILDDIR)/x86/* $(OOBUILDDIR)/external/dbghelp || true; \
	    cp -af $(BUILDDIR)/*.dll $(OOBUILDDIR)/external/unicows || true; \
	    mv $(OOBUILDDIR)/external/dbghelp/DbgHelp.Dll  \
		     $(OOBUILDDIR)/external/dbghelp/dbghelp.dll.temp || true; \
	    mv $(OOBUILDDIR)/external/dbghelp/dbghelp.dll.temp  \
		     $(OOBUILDDIR)/external/dbghelp/dbghelp.dll || true; \
	    chmod +x $(OOBUILDDIR)/external/dbghelp/*.dll \
		     $(OOBUILDDIR)/external/unicows/*.dll || true; \
	fi
	$(TOOLSDIR)/bin/transform --revert $(TOOLSDIR) $(OOBUILDDIR)
	FLAGS="--distro=$(DISTRO)"; \
	if test -d $(OOBUILDDIR)/binfilter ; then \
		FLAGS="$$FLAGS --distro=Binfilter"; \
	fi ; \
	if grep localize.sdf $(OOBUILDDIR)/sw/source/ui/app/localize.sdf > /dev/null 2>&1 ; then \
		FLAGS="$$FLAGS --distro=Localize"; \
	fi ; \
	$(TOOLSDIR)/patches/apply.pl $(APPLY_DIR) $(OOBUILDDIR) $$FLAGS --tag=$(CVSTAG) ;
	$(TOOLSDIR)/bin/transform --apply $(TOOLSDIR) $(OOBUILDDIR)
	$(TOOLSDIR)/bin/fix-deps $(OOBUILDDIR)
	rm -f $(STAMP_DIR)/build
	touch $@

patch.unapply:
	$(TOOLSDIR)/bin/transform --revert $(TOOLSDIR) $(OOBUILDDIR)
	FLAGS="--distro=$(DISTRO)"; \
	if test -d $(OOBUILDDIR)/binfilter ; then \
		FLAGS="$$FLAGS --distro=Binfilter"; \
	fi ; \
	if grep localize.sdf $(OOBUILDDIR)/sw/source/ui/app/localize.sdf > /dev/null 2>&1 ; then \
		FLAGS="$$FLAGS --distro=Localize"; \
	fi ; \
	$(TOOLSDIR)/patches/apply.pl $(APPLY_DIR) $(OOBUILDDIR) $$FLAGS -R ;
	rm -f $(STAMP_DIR)/patch.apply

# Build bits:

prebuild : $(STAMP_DIR)/prebuild
$(STAMP_DIR)/prebuild : $(OOBUILDDIR)/unpack
	echo "Creating environment"
	mkdir -p $(BUILDDIR)
	mkdir -p $(BUILDDIR)/bin
	mkdir -p $(BUILDDIR)/share
	mkdir -p $(BUILDDIR)/share/aclocal
	mkdir -p $(BUILDDIR)/share/autoconf
	mkdir -p $(BUILDDIR)/lib
	mkdir -p $(BUILDDIR)/man
	mkdir -p $(BUILDDIR)/man/man1
	mkdir -p $(OOBUILDDIR)
	mkdir -p $(OOBUILDDIR)/external/msvcp71
	touch $@

build.tools : $(STAMP_DIR)/build.tools
$(STAMP_DIR)/build.tools : $(STAMP_DIR)/prebuild
	cd $(top_srcdir)/bin ; ./build-tools
	touch $@

build : $(STAMP_DIR)/build
$(STAMP_DIR)/build : $(OOBUILDDIR)/unpack \
		     $(STAMP_DIR)/patch.apply \
		     $(STAMP_DIR)/artwork.install \
		     $(STAMP_DIR)/build.tools \
		     $(STAMP_DIR)/prebuild
	cd $(top_srcdir)/bin ; ./build-ooo
	touch $@

all: build

install: $(STAMP_DIR)/build
if BUILD_WIN32
	cd bin ; ./make-win32-iso
else
	cd bin ; ./package-ooo
endif
