.SILENT: help
VERSION = 1.0.5

-include Makefile.rules

OBJECTSSRV = gui.oo server.oo mbsconfig.oo modbussrv.oo guimodbusdata.oo guivalues.oo json.oo tcp.oo
DISTNAME=testmodbus-server-$(VERSION)

ifeq ($(CONFIGSET),)
noconfig: help
endif

all: Makefile.rules testmodbus-server$(TARGETEXT)

help:
	echo "set up configuration"
	echo "   make configwindows    to generate the windows build"
	echo "   make configcross      to generate the windows cross build"
	echo "   make configlinux      to generate the linux build"
	echo "   make buildwindows     to generate the build for windows (uses cross compiler)"
	
configlinux: clean
	cp -f Makefile.rules.linux Makefile.rules
	make config

configwindows: clean
	cp -f Makefile.rules.windows Makefile.rules
	make config

configcross: clean
	cp -f Makefile.rules.crosswindows Makefile.rules
	make config

config: Makefile.rules
	echo "#ifndef _CONFIG_H_" > config.h
	echo "#define _CONFIG_H_" >> config.h
	echo "" >> config.h
	echo "#define VERSION \"$(VERSION)\"" >> config.h
	echo "" >> config.h
	echo "#endif" >> config.h


testmodbus-server$(TARGETEXT): $(OBJECTSSRV)
	$(CPP) -o testmodbus-server$(TARGETEXT) $(OBJECTSSRV) $(LDFLAGS) $(LIBS)

.SUFFIXES:
.SUFFIXES:	.c .cc .C .cpp .oo

.cc.oo : $(INCLUDES)
	$(CPP) -o $@ -c $(CPPFLAGS) $<

clean:
	rm -rf TestModbus-Server-$(VERSION)/
	rm -f *.o *.oo *.c~ *.h~ *.cc~ *.ui~ testmodbus-server$(TARGETEXT) testmodbus-client$(TARGETEXT) Makefile~
	rm -rf *.dll
	rm -rf *.exe
	rm -rf Makefile.rules
	rm -rf test-fc16
	rm -rf test-fc15
	rm -rf config.h

dist: clean
	rm -rf $(DISTNAME)
	mkdir $(DISTNAME)
	cp Makefile* $(DISTNAME)
	cp README.md $(DISTNAME)
	cp LICENSE $(DISTNAME)
	cp Changelog $(DISTNAME)
	cp *.ui $(DISTNAME)
	cp -rf *.h $(DISTNAME)
	cp -rf *.cc $(DISTNAME) 
	tar cvzf $(DISTNAME).tgz $(DISTNAME)
	rm -rf $(DISTNAME)

dep:
	$(CXX) -MM `ls *.cc` $(CXXFLAGS) > $(DEPENDFILE)

test-fc16: test-fc16.cc
	 $(CPP) -o test-fc16 test-fc16.cc tcp.cc $(LDFLAGS)

test-fc15: test-fc15.cc
	 $(CPP) -o test-fc15 test-fc15.cc tcp.cc $(LDFLAGS)

buildwindows: clean
	make configcross
	make $(TARGET) -j 9
	make test-fc15
	make test-fc16
	mkdir TestModbus-Server-$(VERSION)
	cp *.exe TestModbus-Server-$(VERSION)/
	cp testmodbus-server.ui TestModbus-Server-$(VERSION)/
	cp testmodbus-server.png TestModbus-Server-$(VERSION)/
	cp README.md TestModbus-Server-$(VERSION)/
	cp Changelog TestModbus-Server-$(VERSION)/
	cp LICENSE TestModbus-Server-$(VERSION)/
	./copydlls.sh
	./copydlls.sh
	./copydlls.sh
	./copydlls.sh
	./copyshare.sh
	cp /usr/lib/gcc/x86_64-w64-mingw32/14-win32/*.dll TestModbus-Server-$(VERSION)/
	for i in `ldd TestModbus-Server.exe |grep -vi "/c/windows" | cut -d" " -f3`; do cp $$i TestModbus-Server-$(VERSION)/; done
	mv *.dll TestModbus-Server-$(VERSION)/
	mv share TestModbus-Server-$(VERSION)/
	zip -r TestModbus-Server-$(VERSION)-win.zip TestModbus-Server-$(VERSION)/
	

-include $(DEPENDFILE)

.PHONY: all
.PHONY: count
.PHONY: clean
