#=====================================================================
# FILL IN THE BLANKS!
#
# PKG_NAME
#	the name of your package.  This is used as the base name for
#	creating distribution files (e.g. `tgz' and `zip' files).
#	Make sure you pick a name such that adding a `tgz' or `zip'
#	extension results in a filename that does not conflict with
#	any file or directory existing or created by your package.
# PKG_MOGUL_URI
#	is the URI that uniquely identifies your package in the MOGUL
#	database.
# PKG_URI
#	is the base URI for the modules in your package.  This is
#	what's going to be used in `import' specifications.
# PKG_URI_AUX
#	this is a sub URI for auxiliary modules and files that you
#	don't want to expose at the same level as the base URI, but
#	prefer to isolate in their own directory.
# PKG_SOURCES
#	All the source files for your application.  This is used for
#	creating source distributions.
# PKG_INSTALLS
#	These are the filenames of the main modules of your
#	applications, e.g. Foo.ozf (i.e. the name of the compiled
#	functor Foo).  They are used to determine what needs to be
#	installed, and, therefore, what needs to be built.
# PKG_INSTALLS_AUX
#	Auxilliary modules to be installed at the auxiliary URI.
#	Note that these could also include data files, such as GIF
#	files etc...
# PKG_INSTALLS_DOC
#	Documentation files to be installed (and therefore to be
#	built if necessary).
# PKG_INSTALLS_BIN
#	Executable functors and scripts to be installed in $(BINDIR)
#	i.e. typically ~/.oz/bin
# PKG_SUBDIRS
#	are the immediate subdirectories in which MAKE should recurse
# PKG_API
#	a list of source functor files to hand to ozh for creating the
#	API documentation
# PKG_FIND_SOURCES
#	change the value from no to yes if you want find to
#	automatically compute the list of source files to include in
#	your source archives.  for lazy souls only.
#=====================================================================

PKG_NAME	= franzen-browsercontrol
PKG_MOGUL_URI	= mogul:/franzen/browsercontrol
PKG_URI		= x-ozuser://franzen
PKG_URI_AUX	= 
PKG_SOURCES	= BrowserControl.oz
PKG_INSTALLS	= BrowserControl.ozf
PKG_INSTALLS_AUX=
PKG_INSTALLS_DOC=
PKG_INSTALLS_BIN=
PKG_SUBDIRS	=
PKG_API		= BrowserControl.oz
PKG_FIND_SOURCES= no

#=====================================================================
# DIRECTORIES
#=====================================================================

VPATH		= @srcdir@
SRCDIR		= @srcdir@
BUILDDIR	= @builddir@
PREFIX		= @prefix@
OZHOME		= @OZHOME@
CACHE		= $(PREFIX)/cache
DOCDIR		= $(PREFIX)/doc
BINDIR		= $(PREFIX)/bin
PKGDIR		= $(PREFIX)/pkg

#=====================================================================
# TOOLS
#=====================================================================

OZTOOL		= @VAR_OZTOOL@
OZE		= @VAR_OZE@
OZC		= $(OZE) @VAR_OZC@
OZL		= $(OZE) @VAR_OZL@
OZH		= $(OZE) @VAR_OZH@
INSTALL		= @INSTALL@
INSTALL_BIN	= $(INSTALL) -m 555
INSTALL_FILE	= $(INSTALL) -m 444
INSTALL_DIR	= @VAR_INSTALL_DIR@
CFLAGS		= @CFLAGS@
OZCFLAGS	= -z 9
OZLFLAGS	= -z 9
OZHFLAGS	=
PLATFORM	= $(shell $(OZTOOL) platform)
ZIP		= zip

#=====================================================================
# RECURSIVE PROCESSING AND CLEANING UP
#	note that, when doing `make distclean', it is very important
# to first recurse into subdirectories before removing the local
# Makefile, otherwise there is no Makefile left to perform the
# recursion!
#=====================================================================

@SET_MAKE@
all::

veryclean:: clean
distclean:: veryclean

all clean distclean install install.top install.sub install.bin install.doc::
	@dirs="$(PKG_SUBDIRS)"; \
	for i in $$dirs; do \
	  if (cd $$i && $(MAKE) $@); \
          then true; \
          else exit 1; \
          fi; \
        done

clean::
	-rm -f *~ *.o *.so-* *.ozf confdefs.h

distclean::
	-rm -f config.cache config.log config.status Makefile

#=====================================================================
# RULES
#=====================================================================

.PHONY: all all.top all.aux all.bin all.doc		\
	clean veryclean distclean			\
	install install.top install.aux install.bin	\
	install.doc install.api				\
	tgz update zip

all	:: Makefile all.top all.aux all.bin
all.top	:: $(PKG_INSTALLS)
all.aux	:: $(PKG_INSTALLS_AUX)
all.bin	:: $(PKG_INSTALLS_BIN)


%.so-$(PLATFORM): %.o
	$(OZTOOL) ld $< -o $@
%.o: %.cc
	$(OZTOOL) c++ $(CFLAGS) -c $< -o $@
%.ozf: %.oz
	$(OZC) $(OZCFLAGS) -c $< -o $@
%.exe: %.ozf
	$(OZL) $(OZLFLAGS) -x $< -o $@

#=====================================================================
# INSTALLATION
#=====================================================================

LIBPKG_DIR	= $(subst //,/,$(subst :/,/,$(PKG_URI)))
LIBPKG_DIR_AUX	= $(subst //,/,$(subst :/,/,$(PKG_URI_AUX)))
LIBPKG_MOGUL_DIR1 = $(subst mogul:,,$(PKG_MOGUL_URI))
LIBPKG_MOGUL_DIR2 = $(subst :/,/,$(LIBPKG_MOGUL_DIR1))
LIBPKG_MOGUL_DIR3 = O_Z_S_K_E_L_$(subst //,/,$(LIBPKG_MOGUL_DIR2))
LIBPKG_MOGUL_DIR4 = $(subst O_Z_S_K_E_L_/,,$(LIBPKG_MOGUL_DIR3))
LIBPKG_MOGUL_DIR  = $(subst O_Z_S_K_E_L_,,$(LIBPKG_MOGUL_DIR4))

LIBDIR_TOP	= $(CACHE)/$(LIBPKG_DIR)
LIBDIR_AUX	= $(CACHE)/$(LIBPKG_DIR_AUX)
LIBDIR_DOC	= $(DOCDIR)/$(LIBPKG_MOGUL_DIR)
LIBDIR_API	= $(LIBDIR_DOC)/API
LIBDIR_BIN	= $(BINDIR)
LIBFILES_TOP	= $(addprefix $(LIBDIR_TOP)/,$(PKG_INSTALLS))
LIBFILES_AUX	= $(addprefix $(LIBDIR_AUX)/,$(PKG_INSTALLS_AUX))
LIBFILES_BIN	= $(addprefix $(LIBDIR_BIN)/,$(PKG_INSTALLS_BIN))
LIBFILES_DOC	= $(addprefix $(LIBDIR_DOC)/,$(PKG_INSTALLS_DOC))

API_FROM_SRC	= $(wildcard $(SRCDIR)/API/*)
API_FROM_BUILD	= $(wildcard $(BUILDDIR)/API/*)

ifeq ($(API_FROM_BUILD),)
API_DIR		= $(SRCDIR)/API
API_SOURCES	= $(API_FROM_SRC)
LIBFILES_API	= $(patsubst $(SRCDIR)/API/%,$(LIBDIR_API)/%,$(API_FROM_SRC))
else
API_DIR		= $(BUILDDIR)/API
API_SOURCES	= $(API_FROM_BUILD)
LIBFILES_API	= $(patsubst $(BUILDDIR)/API/%,$(LIBDIR_API)/%,$(API_FROM_BUILD))
endif

install		:: install.top install.aux install.bin install.doc install.api
install.top	:: $(LIBFILES_TOP)
install.aux	:: $(LIBFILES_AUX)
install.bin	:: $(LIBFILES_BIN)
install.doc	:: $(LIBFILES_DOC)
install.api	:: $(LIBFILES_API)

$(LIBFILES_TOP): $(LIBDIR_TOP)/% : % $(LIBDIR_TOP)
	$(INSTALL_FILE) $< $@
$(LIBFILES_AUX): $(LIBDIR_AUX)/% : % $(LIBDIR_AUX)
	$(INSTALL_FILE) $< $@
$(LIBFILES_BIN): $(LIBDIR_BIN)/% : % $(LIBDIR_BIN)
	$(INSTALL_BIN) $< $@
$(LIBFILES_DOC): $(LIBDIR_DOC)/% : % $(LIBDIR_DOC)
	$(INSTALL_FILE) $< $@
$(LIBFILES_API): $(LIBDIR_API)/% : API/% $(LIBDIR_API)
	$(INSTALL_FILE) $< $@
$(LIBDIR_TOP) $(LIBDIR_AUX) $(LIBDIR_BIN) $(LIBDIR_DOC) $(LIBDIR_API) $(TARDIR):
	$(INSTALL_DIR) $@

#=====================================================================
# AUTOMATICALLY REMAKE MAKEFILE WHEN NECESSARY
# =====================================================================

Makefile: Makefile.in
	cd $(BUILDDIR) && ./config.status

#=====================================================================
# API DOCUMENTATION (using ozh)
#=====================================================================

OZHDIR	= $(API_DIR)

api:
	-rm -rf $(OZHDIR)
	OZLOAD=all=$(SRCDIR):cache=${HOME}/.oz/cache:cache=$(OZHOME)/cache \
	$(OZH) $(OZHFLAGS) -o $(OZHDIR) $(PKG_API)

veryclean::
	-rm -rf $(OZHDIR)

#=====================================================================
# SOURCE TAR BALL
#=====================================================================

TMP_DIR		= ozskel_tmpdir
TAR_DIR		= $(TMP_DIR)/$(PKG_NAME)
TAR_FILE	= $(PKG_NAME).tgz

ADMIN_SOURCES	= Makefile.in configure install-sh mkinstalldirs \
	config.sub config.guess

ifeq ($(PKG_FIND_SOURCES),yes)
FIND_DIRS	= $(SRCDIR)
FIND_OPTIONS	= \
	-not -type d			\
	-not -path "*/$(TMP_DIR)/*"	\
	-not -name CVS			\
	-not -path '*/CVS/*'		\
	-not -name '.cvsignore'		\
	-not -name Makefile		\
	-not -name '*.ozf'		\
	-not -name '*.exe'		\
	-not -name '*.o'		\
	-not -name '*.so-*'		\
	-not -name '*~'			\
	-not -name 'API'		\
	-not -path '*/API/*'		\
        -not -name aclocal.m4		\
        -not -name configure.in		\
        -not -name config.status	\
        -not -name config.cache		\
        -not -name config.log		\
	-not -name $(TAR_FILE)
FIND_SOURCES	= $(shell find $(FIND_DIRS) $(FIND_OPTIONS) -print)
ALL_PKG_SOURCES	= $(subst $(SRCDIR)/,,$(FIND_SOURCES))
else
ALL_PKG_SOURCES	= $(ADMIN_SOURCES) $(PKG_SOURCES)
endif
ALL_API_SOURCES = $(API_SOURCES)
TAR_PKG_SOURCES	= $(addprefix $(TAR_DIR)/,$(ALL_PKG_SOURCES))
TAR_API_SOURCES	= $(subst $(API_DIR)/,$(TAR_DIR)/API/,$(ALL_API_SOURCES))

tgz:: $(TAR_FILE)

$(TAR_FILE): $(TAR_PKG_SOURCES) $(TAR_API_SOURCES)
	cd $(TMP_DIR) && tar zcf ../$(TAR_FILE) $(PKG_NAME)

$(TAR_API_SOURCES): $(TAR_DIR)/API/% : $(API_DIR)/%
	test -d $(@D) || $(INSTALL_DIR) $(@D)
	if test -x $< ; then $(INSTALL_BIN) $< $@ ; \
	else $(INSTALL_FILE) $< $@ ; fi

$(TAR_PKG_SOURCES): $(TAR_DIR)/% : %
	test -d $(@D) || $(INSTALL_DIR) $(@D)
	if test -x $< ; then $(INSTALL_BIN) $< $@ ; \
	else $(INSTALL_FILE) $< $@ ; fi

update:: $(TAR_FILE)
	test -d $(PKGDIR) || $(INSTALL_DIR) $(PKGDIR)
	$(INSTALL_FILE) $(TAR_FILE) $(PKGDIR)/

clean::
	-rm -rf $(TAR_FILE) $(TAR_DIR)

#=====================================================================
# ZIP DISTRIBUTIONS
#=====================================================================

ZIP_FILE= $(TAR_NAME).zip
zip: all
	-rm -rf $(TAR_NAME)
	$(MAKE) install PREFIX=$(TAR_NAME)
	cd $(TAR_NAME) && $(ZIP) -r -D ../$(TAR_NAME).zip *

#=====================================================================
# DEBUGGING
#	in order to build the package with debugging flags on
# execute `make debug-all'.  In fact `make debug-FOO' invokes
# `make FOO' with debugging flags added to the default settings.
#=====================================================================

debug-%:
	$(MAKE) $* CFLAGS="-g $(CFLAGS)" OZCFLAGS="-g $(OZCFLAGS)"

