PROJECTNAME=unicode
VERSION=$(shell dpkg-parsechangelog --show-field Version)
ifeq ($(shell lsb_release -is),Debian)
ONDEBIAN=yes
DEBIANVERSION=$(shell lsb_release -rs)
else
ONDEBIAN=no
endif

# On Ubuntu 2104 and 2110, dh_strip / debugedit is broken, therefore different Non-Debian options in the following
DISTROS=base base-i386 debian10 debian11 ubuntu2004 ubuntu2104 ubuntu2110 ubuntu2204

ifeq ($(DEBIANVERSION),10)
CXX=g++-8
else

ifeq ($(wildcard $(shell which clang++-13)),)
ifeq ($(wildcard $(shell which clang++-12)),)
ifeq ($(wildcard $(shell which clang++-11)),)
ifeq ($(wildcard $(shell which clang++)),)
CXX=g++-11
else
CXX=clang++
endif
else
CXX=clang++-11
endif
else
CXX=clang++-12
endif
else
CXX=clang++-13
endif

endif

STANDARD=c++17

ifeq ($(CXXFLAGS),)
#CXXFLAGS=-O0 -g -D_DEBUG
CXXFLAGS=-O2 -DNDEBUG
endif

ifeq ($(CXX),clang++-13)
ifeq ($(ONDEBIAN),yes)
COMPILER_SUITE=clang
LIBS+=-fuse-ld=lld-13
STANDARD=c++20
endif
endif

ifeq ($(CXX),clang++)
ifeq ($(ONDEBIAN),yes)
COMPILER_SUITE=clang
LIBS+=-fuse-ld=lld
endif
endif

CXXFLAGS+=-Wall -Wno-deprecated-declarations -finput-charset=UTF-8 -fexec-charset=UTF-8 -Iinclude -std=$(STANDARD)

LDLIBS+=\
-lboost_context \
-lboost_filesystem \
-lboost_timer \
-lboost_system \

ifeq ($(COMPILER_SUITE),clang)
# libc++ is buggy for C++20: std::hash<char8_t> not implemented. Fixed in LLVM-12
CXXFLAGS+=-stdlib=libc++
LIBS+= \
-lc++ \
-lc++abi
#-lc++fs
#-lstdc++ \
#-lstdc++fs
else
LIBS+= \
-lstdc++ \
-lstdc++fs
endif

SRC=\
    src/file.cpp \
    src/recode.cpp \
    src/validate.cpp \
    src/test-unicode.cpp

all: src/recode src/test-unicode src/validate

test: src/test-unicode
	src/test-unicode

src/recode: src/recode.o src/file.o dep
	$(CXX) $(LDFLAGS) src/recode.o src/file.o $(LDLIBS) $(LIBS) -o $@

src/validate: src/validate.o src/file.o dep
	$(CXX) $(LDFLAGS) src/validate.o src/file.o $(LDLIBS) $(LIBS) -o $@

src/test-unicode: src/test-unicode.o dep
	$(CXX) $(LDFLAGS) src/test-unicode.o $(LDLIBS) $(LIBS) -o $@

dep: $(SRC:.cpp=.d)

%.d: %.cpp
	$(CXX) $(CXXFLAGS) -MM -MP -MF $@ -MT $(*D)/$(*F).o -c $<

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

clean:
	-rm -f src/recode src/test-unicode src/validate src/*.tmp
	-rm -rf result
	-find . -name '*.o' -o -name '*.d' -o -name '*.gcno' -o -name '*.gcda' | xargs rm -f

install:
	mkdir -p $(DESTDIR)/usr/include
	cp include/unicode.h $(DESTDIR)/usr/include
	mkdir -p $(DESTDIR)/usr/bin
	cp src/recode $(DESTDIR)/usr/bin/unicode-recode
	cp src/validate $(DESTDIR)/usr/bin/unicode-validate

deb:
	# build binary deb package
	dpkg-buildpackage -rfakeroot

deb-src:
	dh_clean
	dh_auto_clean
	dpkg-source -b -I.git -Iresult .

$(DISTROS): deb-src
	sudo pbuilder build --basetgz /var/cache/pbuilder/$@.tgz --buildresult result/$@ ../$(PROJECTNAME)_$(VERSION).dsc
	-debsign result/$@/$(PROJECTNAME)_$(VERSION)_amd64.changes

debs: $(DISTROS)

DISTFILES= \
	   $(SRC) \
	   src/file.h \
	   Makefile \
	   include/unicode.h \
	   include/unicode/endian.h \
	   include/unicode/iso.h \
	   include/unicode/predicate.h \
	   include/unicode/types.h \
	   include/unicode/type_traits.h \
	   include/unicode/utf.h \
	   include/unicode/utf_fwd.h \
           debian/control \
           debian/compat \
           debian/copyright \
           debian/source \
           debian/source/format \
           debian/changelog \
           debian/README.Debian \
           debian/rules \
           debian/unicode-tools.install \
           debian/libunicode-dev.install \
	   msbuild/compiler.props \
	   msbuild/libunicode.vcxproj \
	   msbuild/libunicode.vcxproj.filters \
	   msbuild/recode.vcxproj \
	   msbuild/recode.vcxproj.filters \
	   msbuild/test-unicode.vcxproj \
	   msbuild/test-unicode.vcxproj.filters \
	   msbuild/unicode.sln \
	   msbuild/validate.vcxproj \
	   msbuild/validate.vcxproj.filters

dist: clean
	rm -rf $(PROJECTNAME)-$(VERSION)
	mkdir $(PROJECTNAME)-$(VERSION)
	cp --parents -r $(DISTFILES) $(PROJECTNAME)-$(VERSION)
	tar cfJ ../$(PROJECTNAME)-$(VERSION).tar.xz $(PROJECTNAME)-$(VERSION)
	rm -rf $(PROJECTNAME)-$(VERSION)
	ls -l ../$(PROJECTNAME)-$(VERSION).tar.xz

.PHONY: all test clean install dep deb debs deb-src dist $(DISTROS)

-include $(wildcard $(SRC:.cpp=.d))