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)/.. -I.. @CPPFLAGS@ @MODFLAGS@
MODLINK  := @MODLINK@
LDFLAGS  := @LDFLAGS@
# LIBS are not and should not be used in here.
# The znc binary links already against those.
# LIBS     := @LIBS@
PERL_ON  := @PERL@
PERL     := @PERL_BINARY@
PYTHON_ON:= @PYTHON@
PY_CFLAGS:= @python_CFLAGS@
PY_LDFLAGS:=@python_LIBS@
SWIG     := @SWIG@
SWIG_ON  := @USESWIG@
MODDIR   := @MODDIR@
DATADIR  := @DATADIR@
LIBZNC   := @LIBZNC@
LIBZNCDIR:= @LIBZNCDIR@
INSTALL         := @INSTALL@
INSTALL_PROGRAM := @INSTALL_PROGRAM@
INSTALL_SCRIPT  := @INSTALL_SCRIPT@
INSTALL_DATA    := @INSTALL_DATA@

TCL_FLAGS:= @TCL_FLAGS@

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

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

CLEAN    :=

FILES    := $(notdir $(wildcard $(srcdir)/*.cpp))
# If extra is enabled
ifeq "@EXTRA@" "yes"
FILES    += $(addprefix extra/, $(notdir $(wildcard $(srcdir)/extra/*.cpp)))
endif

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 "@SASL@" ""
FILES    := $(shell echo $(FILES) | sed -e "s:extra/saslauth::")
endif
saslauthFLAGS := -lsasl2

ifeq "@CHARSET@" ""
FILES    := $(shell echo $(FILES) | sed -e "s:extra/charset::")
endif
charsetFLAGS := @LIBICONV@

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

CLEAN    += *.so extra/*.so

.PHONY: all clean install install_datadir uninstall

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
	if test "@EXTRA@" = "yes" ; then \
		cp -R $(srcdir)/extra/data/* $(DESTDIR)$(DATADIR)/modules ; \
	fi
	find $(DESTDIR)$(DATADIR)/modules -type d -exec chmod 0755 '{}' \;
	find $(DESTDIR)$(DATADIR)/modules -type f -exec chmod 0644 '{}' \;

clean:
	rm -rf $(CLEAN)

%.so: %.cpp Makefile
	@mkdir -p .depend
	@mkdir -p extra
	$(E) Building $(if $(filter %extra/,$(dir $<)),extra )module $(notdir $(basename $@))...
	$(Q)$(CXX) $(MODFLAGS) $(LDFLAGS) $(MODLINK) -o $@ $< $($(notdir $(basename $@))FLAGS) -MMD -MF .depend/$(notdir $@).dep

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)
