#!/usr/bin/make -f

BUILDD_MEM := $(shell awk '/^MemTotal:/ {print $$2}' /proc/meminfo)
BUILDD_MEM_OK := $(strip $(shell test $(BUILDD_MEM) -gt 1800000 && echo yes))

BUILDD_SLOW_ARCH := $(strip $(shell dpkg-architecture -qDEB_BUILD_ARCH_CPU | \
  									grep -q -E '^(arm|mips|sh4|m68k|powerpc).*' && echo yes))

# powerpc moved to slow arch due to weird build failures

ifneq (yes,$(BUILDD_MEM_OK))
  OPT_CFLAGS = -O0 -g
else
  ifneq (yes,$(BUILDD_SLOW_ARCH))
    OPT_CFLAGS = -O3 -g
  else
    OPT_CFLAGS = -O1 -g -fno-tree-pre
  endif
endif

GMIC_CFLAGS="-fstack-protector-strong"
GMICLDFLAGS="-Wl,-z,relro"

%:
	dh $@ --sourcedirectory=src

clean:
	if [ -f zart/Makefile_old ]; then mv zart/Makefile_old zart/Makefile; fi
	rm -f src/gmic_use_lib
	dh $@ --sourcedirectory=src

override_dh_auto_configure:
	if [ -f Makefile ]; then cp zart/Makefile zart/Makefile_old; fi
	dh_auto_configure

override_dh_auto_test: 
	# no tests please.

override_dh_compress:
	dh_compress -X.shtml -X.rss -X.js -X.ppm

override_dh_auto_build:
	export NOSTRIP=1 ;\
	    export MANDATORY_LDFLAGS="-Wl,--as-needed $(GMIC_LDFLAGS)" ;\
	    export OPT_CFLAGS="$(OPT_CFLAGS)" ;\
		export MANDATORY_CFLAGS="$(GMIC_CFLAGS) -I. -I/usr/include/libavcodec/ -I/usr/include/libavformat/ -I/usr/include/libswscale/" ;\
		dh_auto_build --sourcedirectory=src


override_dh_install:
	chmod 755 debian/libgmic*.install
	dh_install --fail-missing
	# FIX CImg.h file name to avoid problems with cimg-dev
	sed -i 's,CImg.h,gmic_CImg.h,g' debian/libgmic-dev/usr/include/gmic.h
	mv debian/libgmic-dev/usr/include/CImg.h debian/libgmic-dev/usr/include/gmic_CImg.h

.PHONY: override_dh_auto_test override_dh_compress override_dh_auto_build override_dh_install

# vim:ts=4 sw=2 noet
