#
# Copyright (C) 2004-2020 ZNC, see the NOTICE file for details.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
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)" ""
LIBS += -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, $(sort $(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 Makefile
	@mkdir -p .depend
	$(E) Building module $(notdir $(basename $@))...
	$(Q)$(CXX) $(MODFLAGS) -c -o $@ $< $($(notdir $(basename $@))CXXFLAGS) -MD -MF .depend/$(notdir $@).dep

%.so: %.o Makefile
	$(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)
