#!/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_HOST_ARCH      ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_ARCH_CPU  ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_CPU)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

configure_flags += \
  --prefix=/usr \
  --enable-pic \
  --enable-shared \
  --disable-docs \
  --disable-examples \
  --disable-install-bins \
  --disable-install-srcs

ifeq ($(DEB_HOST_ARCH_CPU),arm)
configure_flags_neon := $(configure_flags) --target=armv7-linux-gcc
BUILD_NEON=Yes
endif

ifeq ($(DEB_HOST_ARCH), armel)
ifeq ($(shell dpkg-vendor --derives-from Ubuntu && echo yes),yes)
configure_flags += --target=armv6-linux-gcc --enable-small
else
configure_flags += --target=generic-gnu
endif
else
ifeq ($(DEB_HOST_ARCH), armhf)
# now armhf is ARMv7, but ARMv7 in vpx means NEON, which is not mandatory on armhf
# thus we use ARMv6 and -marm (since no thumb2 on ARMv6) to ensure compatability
# with all ARMv7 cores we support.
CFLAGS += -marm
CXXFLAGS += -marm
configure_flags += --target=armv6-linux-gcc --enable-small
else
ifeq ($(DEB_HOST_ARCH), amd64)
configure_flags += --target=x86_64-linux-gcc
else
ifeq ($(DEB_HOST_ARCH), kfreebsd-amd64)
configure_flags += --target=x86_64-linux-gcc
else
ifeq ($(DEB_HOST_ARCH), i386)
configure_flags += --target=x86-linux-gcc
else
ifeq ($(DEB_HOST_ARCH), kfreebsd-i386)
configure_flags += --target=x86-linux-gcc
else
ifeq ($(DEB_HOST_ARCH), hurd-i386)
configure_flags += --target=x86-linux-gcc
else
ifeq ($(DEB_HOST_ARCH), powerpc)
configure_flags += --target=ppc32-linux-gcc
else
configure_flags += --target=generic-gnu
endif
endif
endif
endif
endif
endif
endif
endif

ifneq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
CROSS=$(DEB_HOST_GNU_TYPE)-
export CROSS
endif

# build/make/configure.sh defaults CROSS to arm-none-linux-gnueabi-, and that compiler
# is usually not available. The following setting works under ARM and Debian Jessie,
# but fails under Wheezy because of missing $(DEB_HOST_GNU_TYPE)-ar
ifeq ($(DEB_HOST_ARCH_CPU),arm)
CROSS=$(DEB_HOST_GNU_TYPE)-
export CROSS
endif


CFLAGS += -Wall $(shell dpkg-buildflags --get CPPFLAGS; dpkg-buildflags --get CFLAGS)
CXXFLAGS += -Wall $(shell dpkg-buildflags --get CPPFLAGS; dpkg-buildflags --get CXXFLAGS)
LDFLAGS += $(shell dpkg-buildflags --get LDFLAGS)

builddir := $(CURDIR)/builddir

configure: configure-stamp
configure-stamp:
	dh_testdir
	mkdir -p $(builddir); \
	cd $(builddir); \
	CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" LDFLAGS="$(LDFLAGS)" $(CURDIR)/configure \
		$(configure_flags)
	touch $@
ifeq ($(BUILD_NEON), Yes)
	mkdir -p $(builddir)-neon; \
	cd $(builddir)-neon; \
	CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" LDFLAGS="$(LDFLAGS)" $(CURDIR)/configure \
		$(configure_flags_neon)
	touch $@
endif

build-arch: build
build-indep: build
build: build-stamp

build-stamp: configure-stamp
	dh_testdir
	$(MAKE) -C $(builddir) verbose=yes
ifeq ($(BUILD_NEON), Yes)
	$(MAKE) -C $(builddir)-neon verbose=yes
endif
	touch $@

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

install: build-stamp
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	$(MAKE) -C $(builddir) verbose=yes dist
ifeq ($(BUILD_NEON), Yes)
	$(MAKE) -C $(builddir)-neon verbose=yes dist
endif
	# don't use stripped library...
	cp -v $(builddir)/libvpx2_g.a \
	  $(builddir)/vpx-vp8-*/lib/libvpx2.a

binary-indep: build install
	dh_testdir
	dh_testroot
	find debian/libvpx2-dev/
	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
	mkdir -p debian/libvpx2/usr/lib/$(DEB_HOST_MULTIARCH)
	cp -a builddir/vpx-vp8-*/lib/libvpx2.so.* debian/libvpx2/usr/lib/$(DEB_HOST_MULTIARCH)
#	mv debian/libvpx2/usr/lib/$(DEB_HOST_MULTIARCH)/libvpx.so.2.0.0 debian/libvpx2/usr/lib/$(DEB_HOST_MULTIARCH)/libvpx2.so.2.0.0
#	rm debian/libvpx2/usr/lib/$(DEB_HOST_MULTIARCH)/libvpx2.so.2
#	rm debian/libvpx2/usr/lib/$(DEB_HOST_MULTIARCH)/libvpx.so.2.0
#	ln -s libvpx2.so.2.0.0 debian/libvpx2/usr/lib/$(DEB_HOST_MULTIARCH)/libvpx2.so.2.0
#	ln -s libvpx2.so.2.0 debian/libvpx2/usr/lib/$(DEB_HOST_MULTIARCH)/libvpx2.so.2
	ls -halt debian/libvpx2/usr/lib/$(DEB_HOST_MULTIARCH)/
ifeq ($(BUILD_NEON), Yes)
	mkdir -p debian/libvpx2/usr/lib/$(DEB_HOST_MULTIARCH)/vfp/neon
	cp -a builddir-neon/vpx-vp8-*/lib/libvpx2.so.* debian/libvpx2/usr/lib/$(DEB_HOST_MULTIARCH)/vfp/neon
endif
	mkdir -p debian/libvpx2-dev/usr/lib/$(DEB_HOST_MULTIARCH)
	ln -s libvpx2.so.3 debian/libvpx2-dev/usr/lib/$(DEB_HOST_MULTIARCH)/libvpx2.so
	cp builddir/vpx-vp8-*/lib/libvpx2.a debian/libvpx2-dev/usr/lib/$(DEB_HOST_MULTIARCH)/
	ls -halt debian/libvpx2-dev/usr/lib/$(DEB_HOST_MULTIARCH)/
	mv debian/libvpx2-dev/usr/lib/pkgconfig/vpx.pc debian/libvpx2-dev/usr/lib/pkgconfig/vpx2.pc
	perl -pi -e 's/vpx/vpx2/' debian/libvpx2-dev/usr/lib/pkgconfig/vpx2.pc
	perl -pi -e 's/vpx22/vpx2/' debian/libvpx2-dev/usr/lib/pkgconfig/vpx2.pc
	perl -pi -e 's/\/include/\/include\/vpx2/'  debian/libvpx2-dev/usr/lib/pkgconfig/vpx2.pc
	cat debian/libvpx2-dev/usr/lib/pkgconfig/vpx2.pc
	dh_installdocs -s -A README AUTHORS
	dh_installchangelogs -s CHANGELOG
	dh_link -s
	dh_strip -s --dbg-package=libvpx2-dbg
	# Make sure not to conflict with past SONAMEs:
	rm -rf debian/libvpx2-dbg/usr/lib/debug/usr/bin
	dh_compress -s
	dh_fixperms -s
	dh_makeshlibs -plibvpx2 -V 'libvpx2 (>= 1.4.0)' -- -c4 -d
	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
