# GNU Makefile

# sourcefiles: all *.cpp files except ./plugins subdirectory
# objfiles: corresponding *.o files
sourcefiles:=$(shell find . -name "*.cpp" -not \( -path ./plugins/\* \))
objfiles:=$(patsubst %.cpp,%.o,$(sourcefiles))

# sourcefiles_no_main: all *.cpp files except ./plugins and ./main subdirectories
sourcefiles_no_main:=$(shell find . -name "*.cpp" -not \( -path ./plugins/\* \) -not \( -path ./main/\* \))
objfiles_no_main:=$(patsubst %.cpp,%.o,$(sourcefiles_no_main))

# sourcefiles_main: all *.cpp files from ./main subdirectory
sourcefiles_main:=$(shell find main -name "*.cpp")
objfiles_main:=$(patsubst %.cpp,%.o,$(sourcefiles_main))



WINIX_PLUGINS_MAKEDEPEND_INCLUDES = -I../../../../winix/winixd \
			-I../../../../pikotools/src \
			-I../../../../morm/src \
			-I../../../../ezc/src \
			-I../../../../tito/src

WINIX_NEEDED_MACROS = -DPT_HAS_MORM_LIBRARY \
			-DEZC_HAS_MORM_LIBRARY \
			-DMORM_HAS_EZC_LIBRARY



ifndef CXX
CXX = g++
endif

ifndef CXXFLAGS
# /usr/include/postgresql is in Linux distros (Alpine)
CXXFLAGS = -Wall -pedantic -O2 -std=c++20 -fPIC -pthread \
			-I/usr/local/include \
			-I/usr/include/postgresql \
			-I../../winix/winixd \
			-I../../pikotools/src \
			-I../../morm/src \
			-I../../ezc/src \
			-I../../tito/src \
			$(WINIX_PLUGINS_MAKEDEPEND_INCLUDES) \
			$(WINIX_NEEDED_MACROS)
endif

ifndef LDFLAGS
LDFLAGS = -L/usr/local/lib -s
endif


# for make install
ifndef WINIX_INSTALL_DIR
WINIX_INSTALL_DIR=/usr/local/winix
endif

ifeq ($(WINIX_INSTALL_DIR),)
$(error WINIX_INSTALL_DIR env variable cannot be empty)
endif


export CXX
export CXXFLAGS
export LDFLAGS
export WINIX_PLUGINS_MAKEDEPEND_INCLUDES
export WINIX_NEEDED_MACROS



all: winix winix.so plugins


winix: $(objfiles_main) winix.so
	$(CXX) -o winix $(CXXFLAGS) $(LDFLAGS) $(objfiles_main) winix.so -lcurl


winix.so: $(objfiles_no_main)
	@cd ../../pikotools ; $(MAKE) -e
	@cd ../../morm      ; $(MAKE) -e
	@cd ../../ezc       ; $(MAKE) -e
	@cd ../../tito      ; $(MAKE) -e
	$(CXX) -shared -rdynamic -Wl,-whole-archive -o winix.so $(CXXFLAGS) $(objfiles_no_main) ../../pikotools/src/pikotools.a ../../morm/src/morm.a ../../ezc/src/ezc.a ../../tito/src/tito.a $(LDFLAGS) -lfcgi -lpq -lz -lpthread -lmagic -Wl,-no-whole-archive


plugins: FORCE
	@cd plugins/stats     		; $(MAKE) -e
	@cd plugins/thread    		; $(MAKE) -e
	@cd plugins/ticket    		; $(MAKE) -e
	@cd plugins/gallery   		; $(MAKE) -e
	@cd plugins/group     		; $(MAKE) -e
	@cd plugins/menu      		; $(MAKE) -e
	@cd plugins/export    		; $(MAKE) -e
	@cd plugins/mailregister	; $(MAKE) -e
	@cd plugins/seo	            ; $(MAKE) -e


%.o: %.cpp
	$(CXX) -c $(CXXFLAGS) -o $@ $<


FORCE:


clean: FORCE
	rm -f $(objfiles)
	rm -f winix.so
	rm -f winix
	@cd plugins/stats        ; $(MAKE) -e clean
	@cd plugins/thread       ; $(MAKE) -e clean
	@cd plugins/ticket       ; $(MAKE) -e clean
	@cd plugins/gallery      ; $(MAKE) -e clean
	@cd plugins/group        ; $(MAKE) -e clean
	@cd plugins/menu         ; $(MAKE) -e clean
	@cd plugins/export       ; $(MAKE) -e clean
	@cd plugins/mailregister ; $(MAKE) -e clean
	@cd plugins/seo          ; $(MAKE) -e clean


cleanall: clean
	@cd ../../pikotools      ; $(MAKE) -e clean
	@cd ../../morm           ; $(MAKE) -e clean
	@cd ../../ezc            ; $(MAKE) -e clean
	@cd ../../tito           ; $(MAKE) -e clean


depend:
	makedepend -w 10 -Y. -I../../pikotools/src -I../../morm/src -I../../ezc/src -I../../tito/src $(WINIX_NEEDED_MACROS) -f- $(sourcefiles) | sort -u > Makefile.dep
	@cd plugins/stats          ; $(MAKE) -e depend
	@cd plugins/thread         ; $(MAKE) -e depend
	@cd plugins/ticket         ; $(MAKE) -e depend
	@cd plugins/gallery        ; $(MAKE) -e depend
	@cd plugins/group          ; $(MAKE) -e depend
	@cd plugins/menu           ; $(MAKE) -e depend
	@cd plugins/export         ; $(MAKE) -e depend
	@cd plugins/mailregister   ; $(MAKE) -e depend
	@cd plugins/seo            ; $(MAKE) -e depend
	@cd ../../pikotools        ; $(MAKE) -e depend
	@cd ../../morm             ; $(MAKE) -e depend
	@cd ../../ezc              ; $(MAKE) -e depend
	@cd ../../tito             ; $(MAKE) -e depend



install: all
	# installing the main binary
	rm -Rf $(WINIX_INSTALL_DIR)/bin
	mkdir -p $(WINIX_INSTALL_DIR)/bin
	cp winix $(WINIX_INSTALL_DIR)/bin
	# installing the winix.so library
	rm -Rf $(WINIX_INSTALL_DIR)/lib
	mkdir -p $(WINIX_INSTALL_DIR)/lib
	cp winix.so $(WINIX_INSTALL_DIR)/lib
	# etc configs
	rm -Rf $(WINIX_INSTALL_DIR)/etc
	mkdir -p $(WINIX_INSTALL_DIR)/etc
	cp -rf etc/* $(WINIX_INSTALL_DIR)/etc/
	# html templates
	rm -Rf $(WINIX_INSTALL_DIR)/html
	mkdir -p $(WINIX_INSTALL_DIR)/html
	cp -rf html/* $(WINIX_INSTALL_DIR)/html/
	# txt templates
	rm -Rf $(WINIX_INSTALL_DIR)/txt
	mkdir -p $(WINIX_INSTALL_DIR)/txt
	cp -rf txt/* $(WINIX_INSTALL_DIR)/txt/
	# locales
	rm -Rf $(WINIX_INSTALL_DIR)/locale
	mkdir -p $(WINIX_INSTALL_DIR)/locale
	cp -rf locale/* $(WINIX_INSTALL_DIR)/locale/
	# plugins
	rm -Rf $(WINIX_INSTALL_DIR)/plugins
	mkdir -p $(WINIX_INSTALL_DIR)/plugins
	find plugins/ -name "*.so" | xargs -I foo cp foo $(WINIX_INSTALL_DIR)/plugins/
	# removing privileges for others
	find $(WINIX_INSTALL_DIR) -exec chmod o-r,o-x,o-w "{}" "+"


-include Makefile.dep


