VPATH		= @srcdir@
SRCDIR		= @srcdir@
PREFIX		= @prefix@
CYGPATH		= $(SRCDIR)/cygpath.sh
CYG_PREFIX	= $(shell $(CYGPATH) $(PREFIX))
CYG_SRCDIR	= $(shell $(CYGPATH) $(SRCDIR))
OZENGINE	= @VAR_OZE@
BUILDDIR	= @BUILDDIR@
CYG_BUILDDIR_OZMAKE = $(shell $(CYGPATH) $(BUILDDIR)/ozmake)
OZTOOL		= @VAR_OZTOOL@
PLATFORM	= $(shell $(OZTOOL) platform)
NOTDIRSRCDIR	= $(notdir $(SRCDIR))
BUILDDATE       = $(shell date '+%Y%m%d')
VERSION         = $(shell $(OZTOOL) version)

ifeq ($(PLATFORM),win32-i486)
OZEMULATOR	= $(shell $(CYGPATH) $(PREFIX)/platform/win32-i486/emulator.dll)
export OZEMULATOR
endif

all: build.ozmake
	$(OZENGINE) ozmake/ozmake.ozf --prefix=$(CYG_PREFIX) --srcdir=$(CYG_SRCDIR)

build.ozmake:
	cd ozmake && $(MAKE) ozmake.ozf

install: all
	$(OZENGINE) ozmake/ozmake.ozf --prefix=$(CYG_PREFIX) --srcdir=$(CYG_SRCDIR) --install --nosavedb
	$(OZENGINE) ozmake/ozmake.ozf --prefix=$(CYG_PREFIX) --srcdir=$(CYG_SRCDIR)/ozmake --builddir=$(CYG_BUILDDIR_OZMAKE) --install --nosavedb

export CLEANDIR

clean:
	$(MAKE) CLEANDIR="$(BUILDDIR)"          clean.dir
	$(MAKE) CLEANDIR="$(BUILDDIR)/adt"      clean.dir
	$(MAKE) CLEANDIR="$(BUILDDIR)/os"       clean.dir
	$(MAKE) CLEANDIR="$(BUILDDIR)/wp"       clean.dir
	$(MAKE) CLEANDIR="$(BUILDDIR)/wp/qhtml" clean.dir
	$(MAKE) CLEANDIR="$(BUILDDIR)/wp/qtk"   clean.dir
	$(MAKE) CLEANDIR="$(BUILDDIR)/xml"      clean.dir
	cd ozmake && $(MAKE) clean

clean.dir:
	-cd $(CLEANDIR) && rm -f *~ *.ozf

distclean: clean
	-cd ozmake && $(MAKE) distclean
	-rm -f Makefile config.cache config.log config.status

######################################################################
# source tarball
######################################################################

COMPRESS=	gzip -9
TAR=		tar
PACKDIR=	/tmp/pack
INSTALL_DIR=	@INSTALL@ -d

src: $(PACKDIR)
	echo "[" $(NOTDIRSRCDIR) "]"
	test -n "$(NOTDIRSRCDIR)" || { echo "bad"; exit 1; }
	 ## Create empty temp directory
	-rm -rf $(PACKDIR)/mozart-$(VERSION).$(BUILDDATE)-std
	mkdir $(PACKDIR)/mozart-$(VERSION).$(BUILDDATE)-std
	 ## Move source files to temp directory
	cd $(PACKDIR)/mozart-$(VERSION).$(BUILDDATE)-std && \
	  ((cd $(SRCDIR) && $(TAR) -cf - .) | tar -xf -)
	 ## Remove CVS cruft
	cd $(PACKDIR) && \
	(tbr=`find $(PACKDIR)/mozart-$(VERSION).$(BUILDDATE)-std -name CVS -type d 2>/dev/null` ; \
	 rm -rf $$tbr ; \
	 find $(PACKDIR)/mozart-$(VERSION).$(BUILDDATE)-std -name '.cvsignore' -type f -exec rm -f '{}' ';' ;) \
	 ## tar up and compress
	(cd $(PACKDIR) && $(TAR) -cf - mozart-$(VERSION).$(BUILDDATE)-std) | \
	$(COMPRESS) > mozart-$(VERSION).$(BUILDDATE)-std.tar.gz
	 ## remove temp directory
	rm -rf $(PACKDIR)/mozart-$(VERSION).$(BUILDDATE)-std

$(PACKDIR):
	INSTALL_DIR_CHMOD=777; export INSTALL_DIR_CHMOD && $(INSTALL_DIR) $@

