#!/usr/bin/make -f

DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_BUILD_ARCH ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)

configure_flags += \
  --enable-pic \
  --disable-install-bins \
  --disable-install-srcs

# TODO: Special cases for ARM and PPC
ifeq ($(DEB_BUILD_ARCH), amd64)
configure_flags += --target=x86_64-linux-gcc
else
ifeq ($(DEB_BUILD_ARCH), x86)
configure_flags += --target=x86-linux-gcc
else
ifeq ($(DEB_BUILD_ARCH), armel)
configure_flags += --target=armv7-linux-gcc
else
configure_flags += --target=generic-gnu
endif
endif
endif

ifneq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
configure_flags += --host=$(DEB_HOST_GNU_TYPE)
endif

CFLAGS += -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
CFLAGS += -O0
else
CFLAGS += -O2
endif

builddir := $(CURDIR)/builddir

configure: configure-stamp
configure-stamp:
	dh_testdir
	mkdir -p $(builddir); \
	cd $(builddir); \
	CFLAGS="$(CFLAGS)" $(CURDIR)/configure \
		$(configure_flags)
	touch $@

build: build-stamp

build-stamp: configure-stamp
	dh_testdir
	$(MAKE) -C $(builddir) verbose=yes
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f *-stamp
	rm -rf $(builddir)
	dh_clean

install: build-stamp
	dh_testdir
	dh_testroot
	dh_installdirs
	$(MAKE) -C $(builddir) verbose=yes install
	# don't use stripped library...
	cp -v $(builddir)/libvpx_g.a \
	  $(builddir)/vpx-vp8-*/lib/libvpx.a
	cd $(builddir)/vpx-vp8-*/lib && \
	  mkdir tmp && \
	  cd tmp && \
	  ar x ../libvpx.a && \
	  cd .. && \
	  gcc -shared -fPIC -pthread -lm -Wl,--no-undefined -Wl,-soname,libvpx.so.0 -Wl,--version-script,$(CURDIR)/debian/libvpx.ver -o libvpx.so.0.0.0 tmp/*.o && \
	  rm -rf tmp && \
	  ln -sf libvpx.so.0.0.0 libvpx.so.0 && \
	  ln -sf libvpx.so.0.0.0 libvpx.so && \
	  /usr/sbin/execstack -c libvpx.so.0.0.0
	cd $(builddir)/vpx-vp8-* && \
	  patch -Np0 < $(CURDIR)/debian/vpx_codec.h.patch

binary-indep: build install
	dh_testdir
	dh_testroot
	dh_install -i
	dh_installchangelogs -i CHANGELOG
	dh_installdocs -i -A README AUTHORS
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: build install
	dh_testdir
	dh_testroot
	dh_install -s
	dh_installdocs -s -A README AUTHORS
	dh_installchangelogs -s CHANGELOG
	dh_link -s
	dh_strip -s --dbg-package=libvpx0-dbg
	dh_compress -s
	dh_fixperms -s
	dh_makeshlibs -plibvpx0 -V 'libvpx0 (>= 0.9.0)' -- -c4
	dh_installdeb -s
	dh_shlibdeps -s
	dh_gencontrol -s
	dh_md5sums -s
	dh_builddeb -s

binary: binary-indep binary-arch

.PHONY: build clean binary-indep binary-arch binary install clean
