#!/usr/bin/make -f
#export DH_VERBOSE = 1
#export DEB_BUILD_MAINT_OPTIONS = hardening=+all
# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1
# Set debhelper options
DEB_BUILD_OPTIONS=nocheck
export pkgname=mediastreamer2-plugin-opencore-amr
export _topdir=/usr/src/packages
export _builddir=$(_topdir)/BUILD
export _sourcedir=$(_topdir)/SOURCES
export uploaddir=/usr/src/packages/SOURCES
export srcdir=/usr/src/packages/SOURCES
export pkgdir_head=/usr/src/packages/BUILD/debian
export pkgdir=$(pkgdir_head)/$(pkgname)
#export pkgdir=$(pkgdir_head)/tmp
#export buildroot=/usr/src/packages/BUILD/debian/tmp
export buildroot=/usr/src/packages/BUILD/debian/$(pkgname)
# Add support for different Debian based distributions
export VERSION_CODENAME = $(shell cat /etc/os-release | grep VERSION_CODENAME | awk -F= '{print $$2}')
export VERSION_ID = $(shell cat /etc/os-release | grep VERSION_ID | awk -F= '{print $$2}')
export PRETTY_NAME=$(shell cat /etc/*-release | egrep "PRETTY_NAME" | cut -d = -f 2)
export ARCHITECTURE=$(shell dpkg --print-architecture)
export MY_DEB_BUILD_ARCH=$(shell dpkg-architecture -q DEB_BUILD_ARCH)
ifeq ($(findstring arm64,$(ARCHITECTURE)),arm64)
    export ARCHTYPE=aarch64
    export BUILDDIR=obj-$(ARCHTYPE)-linux-gnu
    export LIBDIR=$(ARCHTYPE)-linux-gnu
endif
ifeq ($(findstring amd64,$(ARCHITECTURE)),amd64)
    export ARCHTYPE=x86_64
    export BUILDDIR=obj-$(ARCHTYPE)-linux-gnu
    export LIBDIR=$(ARCHTYPE)-linux-gnu
endif
ifeq ($(findstring armhf,$(ARCHITECTURE)),armhf)
    export ARCHTYPE=arm
    export BUILDDIR=obj-$(ARCHTYPE)-linux-gnueabihf
    export LIBDIR=$(ARCHTYPE)-linux-gnueabihf
endif
ifeq ($(findstring Raspbian,$(PRETTY_NAME)),Raspbian)
    # Found
    export PLATFORM=Raspbian
endif
ifeq ($(findstring Debian,$(PRETTY_NAME)),Debian)
    # Not found
    export PLATFORM=DebianOrUbuntu
endif
ifeq ($(findstring Ubuntu,$(PRETTY_NAME)),Ubuntu)
    # Not found
    export PLATFORM=DebianOrUbuntu
endif
export SOURCES_ARE_HERE=/usr/src/packages/BUILD/
export BUILD_IT_HERE=/usr/src/packages/BUILD/$(BUILDDIR)/

%:
	#dh $@ --buildsystem=cmake
	dh $@ --with autoreconf

#override_dh_auto_configure:
#	echo CURDIR=$(CURDIR)
#	echo PWD=$(PWD)
#	find .
#	#sed -i 's/find_package(ORTP/find_package(Ortp/g' src/CMakeLists.txt
#	#cat src/CMakeLists.txt
#	#export CFLAGS="$CFLAGS -fpic -ffat-lto-objects -fpermissive -fcommon -Wno-implicit-function-declaration -I$(includedir)/bcmatroska2 -I$(includedir)/corec"
#	#export CFLAGS="$CFLAGS -Wno-error=implicit-function-declaration -Wno-error=unused-parameter"
#	#export CXXFLAGS="$CXXFLAGS $CFLAGS"
#	dh_auto_configure --buildsystem=cmake -- \
#	  -DCMAKE_C_FLAGS="-Wno-error=implicit-function-declaration -Wno-error=unused-parameter -Wno-error=int-conversion" \
#	  -DCMAKE_CXX_FLAGS="-Wno-error=implicit-function-declaration -Wno-error=unused-parameter -Wno-error=int-conversion" \
#	  -DCMAKE_PREFIX_PATH=/usr/share/Ortp/cmake/ \
#	  -DCMAKE_SKIP_RPATH=YES \
#	  -DORTP_DIR=/usr/share/Ortp/cmake/ \
#	  -DBUILD_SHARED_LIBS=YES \
#	  -DENABLE_STATIC=OFF

# Disable dh_auto{reconf,tools_update_config} as we only use cmake.
#override_dh_autoreconf:
#override_dh_autotools_update_config:

override_dh_auto_test:

execute_after_dh_auto_install:
	echo "--- Start execute_after_dh_auto_install ---"
	echo "Different Debian based distros need different patches and adjustments."
	echo PRETTY_NAME=$(PRETTY_NAME)
	echo ARCHITECTURE=$(ARCHITECTURE)
	echo DEB_BUILD_ARCH=$(DEB_BUILD_ARCH)
	echo MY_DEB_BUILD_ARCH=$(MY_DEB_BUILD_ARCH)
	echo PLATFORM=$(PLATFORM)
	echo BUILDDIR=$(BUILDDIR)
	echo LIBDIR=$(LIBDIR)
	echo
	echo pwd=$(shell pwd)
	echo
	echo "Installs $(pkgname)"
	echo _topdir=$(_topdir)
	echo _builddir=$(_builddir)
	echo _sourcedir=$(_sourcedir)
	echo uploaddir=$(uploaddir)
	echo srcdir=$(srcdir)
	echo pkgdir_head=$(pkgdir_head)
	echo pkgdir=$(pkgdir)
	echo buildroot=$(buildroot)
	echo "----------------------------------------"
	echo
	echo pkgdir=${pkgdir}
	echo "--- Files that will be packaged ---"
	find $(buildroot) | sed "s|${pkgdir}||g" | sort
	echo "--- End files that will be packaged ---"
	echo "--- End execute_after_dh_auto_install ---"
