#!/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))

ifneq (,$(findstring arm,$(shell dpkg-architecture -qDEB_BUILD_GNU_CPU)))
  GMIC_CFLAGS = -g -O0
else
  ifneq (,$(findstring mips,$(shell dpkg-architecture -qDEB_BUILD_GNU_CPU)))
    GMIC_CFLAGS = -g -O0
  else
    ifeq (yes,$(BUILDD_MEM_OK))
      GMIC_CFLAGS = $(CFLAGS)
	else
	  GMIC_CFLAGS = -g -O0
	endif
  endif
endif

.PHONY: override_dh_auto_test
override_dh_auto_test: ;

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

.PHONY: override_dh_installchangelogs
override_dh_installchangelogs:
	dh_installchangelogs ChangeLog

config.status:
	dh_testdir
	dh_auto_configure

.PHONY: build
build: build-stamp

build-stamp: config.status
	dh_auto_build -- \
		LDFLAGS="-Wl,--as-needed" \
		CFLAGS="$(GMIC_CFLAGS) -I. -I/usr/include/libavcodec/ -I/usr/include/libavformat/ -I/usr/include/libswscale/"
	touch $(@)

.PHONY: clean
clean:
	rm -f src/gmic_use_lib
	dh clean

.PHONY: install
install: build
	dh install
	install -d $(CURDIR)/debian/gmic/etc/bash_completion.d
	install -D -m644 src/gmic_bashcompletion.sh \
	                 $(CURDIR)/debian/gmic/etc/bash_completion.d/gmic

.PHONY: binary-indep
binary-indep: install
	dh binary-indep

.PHONY: binary-arch
binary-arch: install
	dh binary-arch

.PHONY: binary
binary: binary-indep binary-arch

# vim:ts=4 sw=2 noet
