all:

SHELL       := @SHELL@

# Support out-of-tree builds
srcdir      := @srcdir@
VPATH       := @srcdir@

prefix      := @prefix@
exec_prefix := @exec_prefix@
datarootdir := @datarootdir@
bindir      := @bindir@
datadir     := @datadir@
sysconfdir  := @sysconfdir@
libdir      := @libdir@
sbindir     := @sbindir@
localstatedir := @localstatedir@
CXX      := @CXX@
# CXXFLAGS are for the main binary, so don't use them here, use MODFLAGS instead
MODFLAGS := -I$(srcdir)/../include -I../include @CPPFLAGS@ @MODFLAGS@
MODLINK  := @MODLINK@
LDFLAGS  := @LDFLAGS@
ISCYGWIN := @ISCYGWIN@

# LIBS are not and should not be used in here.
# The znc binary links already against those.
# ...but not on cygwin!
LIBS     :=
ifeq "$(ISCYGWIN)" "1"
LIBS     += @LIBS@
endif

PERL_ON  := @PERL@
PERL     := @PERL_BINARY@
PYTHON_ON:= @PYTHON@
PY_CFLAGS:= @python_CFLAGS@
PY_LDFLAGS:=@python_LIBS@
SWIG     := @SWIG@
MODDIR   := @MODDIR@
DATADIR  := @DATADIR@
LIBZNC   := @LIBZNC@
LIBZNCDIR:= @LIBZNCDIR@
INSTALL         := @INSTALL@
INSTALL_PROGRAM := @INSTALL_PROGRAM@
INSTALL_SCRIPT  := @INSTALL_SCRIPT@
INSTALL_DATA    := @INSTALL_DATA@
SED := @SED@

TCL_FLAGS:= @TCL_FLAGS@

ifneq "$(V)" ""
VERBOSE=1
endif
ifeq "$(VERBOSE)" ""
Q=@
E=@echo
C=-s
else
Q=
E=@\#
C=
endif

ifneq "$(LIBZNC)" ""
LDFLAGS += -L.. -lznc -Wl,-rpath,$(LIBZNCDIR)
endif

CLEAN    :=

FILES    := $(notdir $(wildcard $(srcdir)/*.cpp))

include $(srcdir)/modperl/Makefile.inc
include $(srcdir)/modpython/Makefile.inc
include $(srcdir)/modtcl/Makefile.inc

FILES    := $(basename $(FILES))

ifeq "@NOSSL@" "1"
FILES    := $(foreach file, $(FILES), \
	$(if $(shell grep REQUIRESSL $(srcdir)/$(file).cpp), \
		, \
		$(basename $(file)) \
	))
endif

ifeq "@CYRUS@" ""
FILES    := $(shell echo $(FILES) | sed -e "s:cyrusauth::")
endif
cyrusauthLDFLAGS := -lsasl2

TARGETS  := $(addsuffix .so, $(FILES))

CLEAN    += *.so *.o

.PHONY: all clean install install_datadir uninstall
.SECONDARY:

all: $(TARGETS)

install: all install_datadir
	$(INSTALL_PROGRAM) $(TARGETS) $(DESTDIR)$(MODDIR)

install_datadir:
	rm -rf $(DESTDIR)$(DATADIR)/modules
	test -d $(DESTDIR)$(MODDIR) || $(INSTALL) -d $(DESTDIR)$(MODDIR)
	test -d $(DESTDIR)$(DATADIR)/modules || $(INSTALL) -d $(DESTDIR)$(DATADIR)/modules
	rm -rf $(DESTDIR)$(MODDIR)/*.so
	cp -R $(srcdir)/data/* $(DESTDIR)$(DATADIR)/modules
	find $(DESTDIR)$(DATADIR)/modules -type d -exec chmod 0755 '{}' \;
	find $(DESTDIR)$(DATADIR)/modules -type f -exec chmod 0644 '{}' \;

clean:
	rm -rf $(CLEAN)

%.o: %.cpp
	@mkdir -p .depend
	$(E) Building module $(notdir $(basename $@))...
	$(Q)$(CXX) $(MODFLAGS) -c -o $@ $< $($(notdir $(basename $@))CXXFLAGS) -MD -MF .depend/$(notdir $@).dep

%.so: %.o
	$(E) "Linking  module" $(notdir $(basename $@))...
	$(Q)$(CXX) $(MODFLAGS) $(LDFLAGS) $(MODLINK) -o $@ $< $($(notdir $(basename $@))LDFLAGS) $(LIBS)

uninstall:
	# Yes, we are lazy, just remove everything in there
	rm -rf $(DESTDIR)$(MODDIR)/*
	rm -rf $(DESTDIR)$(DATADIR)/*
	rmdir $(DESTDIR)$(MODDIR)
	rmdir $(DESTDIR)$(DATADIR)

-include $(wildcard .depend/*.dep)
