#!/usr/bin/make -f

export DEB_BUILD_OPTIONS += notrimdch
export DEB_BUILD_MAINT_OPTIONS = hardening=+all
include /usr/share/dpkg/buildflags.mk

SRC_DIR_C = C/Util/Lzma
SRC_DIR_CPP = CPP/7zip/Bundles/LzmaCon

# This has to be exported to make some magic below work.
export DH_OPTIONS
export O = .
export FILE_IO = FileIO
export FILE_IO_2 = Windows/FileIO

override_dh_auto_build:
	dh_auto_build -- all -C $(SRC_DIR_C)   -f makefile.gcc LOCAL_FLAGS="$(CXXFLAGS) $(CPPFLAGS)"      MY_LIBS="$(LDFLAGS)" LFLAGS_STRIP=
	dh_auto_build -- all -C $(SRC_DIR_CPP) -f makefile.gcc LOCAL_FLAGS_SYS="$(CXXFLAGS) $(CPPFLAGS)"  MY_LIBS="$(LDFLAGS)" LFLAGS_STRIP=
	mv $(SRC_DIR_CPP)/lzma $(SRC_DIR_CPP)/lzma_alone
	ln debian/lzmp.cpp $(SRC_DIR_CPP)
	sed -e '1s/lzma/lzmp/' -e 's/LzmaAlone\.o/lzmp.o/' < $(SRC_DIR_CPP)/makefile.gcc > $(SRC_DIR_CPP)/makefile.lzmp
	dh_auto_build -- all -C $(SRC_DIR_CPP) -f makefile.lzmp LOCAL_FLAGS_SYS="$(CXXFLAGS) $(CPPFLAGS)" MY_LIBS="$(LDFLAGS)" LFLAGS_STRIP=

override_dh_auto_clean:
	$(MAKE) clean -C $(SRC_DIR_C) -f makefile.gcc
	$(MAKE) clean -C $(SRC_DIR_CPP) -f makefile.gcc
	! [ -f $(SRC_DIR_CPP)/makefile.lzmp ] || $(MAKE) clean -C $(SRC_DIR_CPP) -f makefile.lzmp
	rm -f $(SRC_DIR_C)/*.o $(SRC_DIR_C)/*.a \
		$(SRC_DIR_CPP)/*.o $(SRC_DIR_CPP)/*.a \
		$(SRC_DIR_CPP)/lzmp.cpp $(SRC_DIR_CPP)/makefile.lzmp

%:
	dh $@ --buildsystem=makefile
