#=====================================================================
# DO NOT EDIT THIS FILE!
#   instead you should edit Makefile.vars and Makefile.rules
#   read the ozskel documentation if you forgot the details
#=====================================================================

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

VPATH		= @srcdir@
SRCDIR		= @srcdir@
CYGSRCDIR	= $(shell $(CYGPATH) $(SRCDIR))
CYGPATH		= $(SRCDIR)/cygpath.sh
SRCTOP		= @top_srcdir@
BUILDDIR	= $(shell pwd)
CYGBUILDDIR	= $(shell $(CYGPATH) $(BUILDDIR))
BUILDTOP	= $(BUILDDIR)
PREFIX		= $(shell $(CYGPATH) @prefix@)
OZHOME		= $(shell $(CYGPATH) @OZHOME@)
CACHE		= $(PREFIX)/cache
DOCDIR		= $(PREFIX)/doc
BINDIR		= $(PREFIX)/bin
PKGDIR		= $(PREFIX)/pkg
INCDIR		= $(PREFIX)/include

export OZHOME

# moved up because needed early for windows
OZTOOL		= @VAR_OZTOOL@
PLATFORM	= $(shell $(OZTOOL) platform)
ifeq ($(PLATFORM),win32-i486)
OZLOADSEP=;
else
OZLOADSEP=:
endif

# these additional 2 lines makes it possible to build in a directory
# different from the source directory
OZLOAD = prefix=$(CYGSRCDIR)=$(CYGBUILDDIR)$(OZLOADSEP)root=$(CYGBUILDDIR)$(OZLOADSEP)root=$(CYGSRCDIR)$(OZLOADSEP)cache=~/.oz/cache$(OZLOADSEP)cache=$(OZHOME)/cache
export OZLOAD

XSLTPROC	= @VAR_XSLTPROC@
LYNX		= @VAR_LYNX@
ELINKS		= @VAR_ELINKS@
HTML_TO_TEXT	= @VAR_HTML_TO_TEXT@

#=====================================================================
# FILL IN THE BLANKS!
#    if possible do not change this file directly, instead:
#	(1) create a file Makefile.vars where you override the default
#	    values of the variables below
#	(2) create a file Makefile.rules where you add new rules and
#	    dependencies specific to your package
#    this makes it easier to upgrade your package to a new ozskel
#    version
#
# 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_INSTALLS_INC
#	include files to be installed in $(INCDIR) i.e. typically
#	under ~/.oz/include/... using PKG_MOGUL_URI to derive the
#	proper subdir.
# 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_METHOD
#	can be set to `find' for forcing the use of find to automati-
#	cally compute the list of source files to include in your
#	source archive. or it can be set to `make' to let the makefile
#	and sub-makefiles decide what to put in the archive.  If not
#	set, its default value is find if PKG_SOURCES is empty and
#	make otherwise.
# PKG_UPDATE
#	a list of abstract update targets in the set `tgz', `tar', and
#	`zip'.  By default, just `tgz'.
#=====================================================================

ifndef PKG_NAME
PKG_NAME	= lewinsky-disaster
PKG_MOGUL_URI	= mogul:/clinton/lewinsky
PKG_URI		= x-ozlib://clinton/monica
PKG_URI_AUX	= $(PKG_URI)/sex-toys
endif

PKG_SOURCES	=
PKG_INSTALLS	=
PKG_INSTALLS_AUX=
PKG_INSTALLS_DOC=
PKG_INSTALLS_INC=
PKG_INSTALLS_BIN=
PKG_SUBDIRS	=
PKG_API		=
PKG_UPDATE	= tgz
PKG_CPPFLAGS	=
PKG_CFLAGS	=
PKG_LDFLAGS	=
PKG_OZCFLAGS	= -z 9
PKG_OZLFLAGS	= -z 9
PKG_OZHFLAGS	=

-include $(SRCDIR)/Makefile.vars

ifndef PKG_METHOD
PKG_METHOD	=
endif

export PKG_NAME
export PKG_MOGUL_URI
export PKG_URI
export PKG_URI_AUX
export PKG_METHOD

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

OZE		= @VAR_OZE@
ifeq ($(PLATFORM),win32-i486)
WITH_OZE=
else
WITH_OZE=$(OZE)
endif
OZC		= $(WITH_OZE) @VAR_OZC@
OZL		= $(WITH_OZE) @VAR_OZL@
OZH		= $(WITH_OZE) @VAR_OZH@
INSTALL		= @VAR_OZINSTALL@
INSTALL_BIN	= $(INSTALL) -x
INSTALL_FILE	= $(INSTALL) -f
INSTALL_DIR	= $(INSTALL) -d
CFLAGS		= @CFLAGS@
OZCFLAGS	= $(PKG_OZCFLAGS)
OZLFLAGS	= $(PKG_OZLFLAGS)
OZHFLAGS	= $(PKG_OZHFLAGS)

#=====================================================================
# 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

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

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	install.src		\
	tgz update zip tar api update-tgz update-tar	\
	update-zip install.inc

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 $@ $(PKG_LDFLAGS)
%.o: %.cc
	$(OZTOOL) c++ $(CFLAGS) $(PKG_CPPFLAGS) $(PKG_CFLAGS) -c $< -o $@
%.ozf: %.oz
	$(OZC) $(OZCFLAGS) -c $(shell $(CYGPATH) $<) -o $@
%.exe: %.ozf
	$(OZL) $(OZLFLAGS) -x $(shell $(CYGPATH) $<) -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_INC	= $(INCDIR)/$(LIBPKG_MOGUL_DIR)
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))
LIBFILES_INC	= $(addprefix $(LIBDIR_INC)/,$(PKG_INSTALLS_INC))

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

$(LIBFILES_TOP): $(LIBDIR_TOP)/% : %
	$(INSTALL_FILE) $< $@
$(LIBFILES_AUX): $(LIBDIR_AUX)/% : %
	$(INSTALL_FILE) $< $@
$(LIBFILES_BIN): $(LIBDIR_BIN)/% : %
	$(INSTALL_BIN) $< $@
$(LIBFILES_DOC): $(LIBDIR_DOC)/% : %
	$(INSTALL_FILE) $< $@
$(LIBFILES_INC): $(LIBDIR_INC)/% : %
	$(INSTALL_FILE) $< $@

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

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

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

TMP_DIR  = $(BUILDTOP)/ozskel_tmpdir
TAR_DIR  = $(TMP_DIR)/$(PKG_NAME)
TGZ_FILE = $(BUILDTOP)/$(PKG_NAME).tgz
ZIP_FILE = $(BUILDTOP)/$(PKG_NAME).zip
TAR_FILE = $(BUILDTOP)/$(PKG_NAME).tar
PKG_FILE = $(BUILDTOP)/$(PKG_NAME).pkg

ifndef PKG_METHOD
ifdef PKG_SOURCES
PKG_METHOD	= make
else
PKG_METHOD	= find
endif
endif

ifeq ($(PKG_METHOD),make)
install.src::
	@dirs="$(PKG_SUBDIRS)"; \
	for i in $$dirs; do \
	  if (cd $$i && $(MAKE) $@); \
          then true; \
          else exit 1; \
          fi; \
        done
endif

ADMIN_SOURCES	= Makefile.in Makefile.vars Makefile.rules \
	ozinstall install-sh configure config.sub config.guess

ifeq ($(PKG_METHOD),make)
ADMIN_SOURCES1	= $(addprefix $(SRCDIR)/,$(ADMIN_SOURCES))
ADMIN_SOURCES2	= $(wildcard $(ADMIN_SOURCES1))
ADMIN_SOURCES3	= $(subst $(SRCDIR)/,,$(ADMIN_SOURCES2))
PKG_SOURCES1	= $(addprefix $(SRCDIR)/,$(PKG_SOURCES))
PKG_SOURCES2	= $(subst $(SRCTOP)/,,$(PKG_SOURCES1))
ALL_PKG_SOURCES	= $(ADMIN_SOURCES3) $(PKG_SOURCES2)
else
FIND_DIRS	= $(SRCDIR)
FIND_SKIP_DIRS	= API CVS SCCS RCS CVS.adm RCSLOG ozskel_tmpdir
FIND_SKIP_FILES	= aclocal.m4 configure.in config.status config.cache	\
	config.log Makefile .cvsignore					\
	*.ozf *.exe *.o *.so-* *~ tags TAGS .make.state .nse_depinfo	\
	\#* .\#* ,* _$$* *$$ *.old *.bak *.BAK *.orig *.rej .del-*	\
	*.a *.old *.obj *.so *.Z *.elc *.ln cvslog.* core CVS.adm RCSLOG\
	$(PKG_NAME).*
FIND_OPTIONS	= \
	$(patsubst %,\( ! -name '%' -o -prune \),$(FIND_SKIP_DIRS)) \
	$(patsubst %,! -name '%',$(FIND_SKIP_FILES)) \
	! -type d
FIND_SOURCES	= $(shell find $(FIND_DIRS) $(FIND_OPTIONS) -print)
ALL_PKG_SOURCES	= $(subst $(SRCDIR)/,,$(FIND_SOURCES))
endif

TAR_PKG_SOURCES	= $(addprefix $(TAR_DIR)/,$(ALL_PKG_SOURCES))

$(TGZ_FILE): tgz
tgz::
	-rm -rf $(TMP_DIR)
	$(MAKE) install.src
	cd $(TMP_DIR) && tar zcf $(TGZ_FILE) $(PKG_NAME)

$(TAR_FILE): tar
tar::
	-rm -rf $(TMP_DIR)
	$(MAKE) install.src
	cd $(TMP_DIR) && tar cf $(TAR_FILE) $(PKG_NAME)
tarz:: $(TAR_FILE).Z

%.Z: %
	$(COMPRESS) -c $< > $@

install.src:: $(TAR_PKG_SOURCES)

$(TAR_PKG_SOURCES): $(TAR_DIR)/% : $(SRCTOP)/%
	$(INSTALL) -M $< $@

update:: $(addprefix update-,$(PKG_UPDATE))

clean::
	-rm -rf $(TGZ_FILE) $(TAR_FILE).Z $(TMP_DIR)

TGZ_FILE_DST	= $(subst $(BUILDTOP)/,$(PKGDIR)/,$(TGZ_FILE))
TAR_FILE_DST	= $(subst $(BUILDTOP)/,$(PKGDIR)/,$(TAR_FILE).Z)
ZIP_FILE_DST	= $(subst $(BUILDTOP)/,$(PKGDIR)/,$(ZIP_FILE))
PKG_FILE_DST	= $(subst $(BUILDTOP)/,$(PKGDIR)/,$(PKG_FILE))

INSTALL_FILES_DST = $(TGZ_FILE_DST) $(TAR_FILE_DST) $(ZIP_FILE_DST) $(PKG_FILE_DST)

$(INSTALL_FILES_DST): $(PKGDIR)/% : $(BUILDTOP)/%
	$(INSTALL_FILE) $< $@

.PHONY: $(INSTALL_FILES_DST)
update-tgz :: $(TGZ_FILE_DST)
update-tarz:: $(TAR_FILE_DST)
update-zip :: $(ZIP_FILE_DST)

#=====================================================================
# OZPM SUPPORT - THIS WAS EXPERIMENTAL AND DOES NOT WORK AT PRESENT!!!
#=====================================================================

OZPMINFO	= $(TMP_DIR)/OZPMINFO

ifndef PKG_INFO
define PKG_INFO
echo "id:      $(PKG_MOGUL_URI)" >> $(OZPMINFO)
echo "version: $(PKG_VERSION)"   >> $(OZPMINFO)
echo "uri:     $(PKG_URI)"       >> $(OZPMINFO)
echo "title:   $(PKG_TITLE)"     >> $(OZPMINFO)
echo "blurb:   $(PKG_BLURB)"     >> $(OZPMINFO)
endef
endif

clean::
	-rm -f $(PKG_FILE)

#=====================================================================
# 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)"

#=====================================================================
# CREATING TAGS FILES
#=====================================================================

ETAGS_PATTERNS	= *.c *.h *.cc *.hh *.el
ETAGS_OPTIONS	= -false $(patsubst %, -o -name '%',$(ETAGS_PATTERNS))

tags:
	cd $(BUILDTOP) && \
	etags -l none \
	--regex='/[ 	]*\(proc\|fun\)[ 	]*{[ 	]*\([a-zA-Z0-9_]+\)/\2/' \
	--regex='/.*[ 	]class[ 	]\([a-zA-Z0-9_]+\)/\1/' \
	`find . -name '*.oz' -print` && \
	etags -a -l auto \
	--regex='/OZ_BI_define([ 	]*\([^ 	,)]+\)/\1/' \
	`find . \( $(ETAGS_OPTIONS) \) -print`

-include $(SRCDIR)/Makefile.rules

