#!/usr/bin/make -f

#export DH_VERBOSE:=1
ifeq (64,$(strip $(shell dpkg-architecture -qDEB_HOST_ARCH_BITS)))
   export EXTRA_EXTRA_CPPFLAGS:=-DP_64BIT=1
endif

DEBVERSION := $(shell head -n 1 debian/changelog \
		    | sed -e 's/^[^(]*(\([^)]*\)).*/\1/')
UPVERSION := $(shell echo $(DEBVERSION) | sed 's/~dfsg-.//')

FILENAME := opal_$(UPVERSION)~dfsg.orig.tar.xz
UPFILENAME := opal_$(UPVERSION).orig.tar.gz
URL := http://aarnet.dl.sourceforge.net/sourceforge/opalvoip/opal-$(UPVERSION).tar.bz2

# hack to get samples/simple compiled with new build system
# no := here for we need evaluation at runtime!
OPAL_LIBDIR=$(shell sed -n -e "/^OPAL_LIBDIR/s,^.*/,$(CURDIR)/,p" opal_defs.mak)

%:
	dh $@ --parallel --with autotools_dev


override_dh_auto_configure:
	dh_auto_configure -- --enable-samples --enable-spandsp

override_dh_auto_build:
	sed -e 's#^Libs: #Libs: -L$(OPAL_LIBDIR) #' opal.pc >samples/opal.pc
	dh_auto_build -- PKG_CONFIG_PATH=$(CURDIR)/samples opt docs

#override_dh_auto_test:
#	if (samples/simple/obj*/simpleopal --help >/dev/null);then \
#		echo shared simpleopal incorrectly compiled static; false;\
#	else \
#		echo shared simpleopal correctly compiled non-static; \
#	fi
#	LD_LIBRARY_PATH=$(OPAL_LIBDIR) samples/simple/obj*/simpleopal --help >/dev/null

override_dh_link:
	# replace jquery.js with Debian's version
	rm -f debian/libopal-doc/usr/share/doc/libopal-doc/html/jquery.js
	dh_link

override_dh_clean:
	dh_clean -- PKG_CONFIG_PATH=$(CURDIR)/samples
	rm -rf html
	rm -f samples/opal.pc
	rm -rf samples/simple/obj_*

override_dh_strip:
	dh_strip -a --dbg-package=libopal-dbg

print-version:
	@@echo "Debian version:          $(DEBVERSION)"
	@@echo "Upstream version:        $(UPVERSION)"
	@@#echo "svn REV:                 $(REV)"

get-orig-source:
	@@dh_testdir
	@@[ -d ../tarballs/. ]||mkdir -p ../tarballs
	@@#this is for stable versions
	@@echo Downloading $(UPFILENAME) from $(URL) ...
	@@wget -nv -T10 -t3 -O ../tarballs/$(UPFILENAME) $(URL)
	@@echo 'Repacking as DFSG-free (removing DLLs and EXEs)...'
	@@mkdir -p ../tarballs/opal-$(UPVERSION).tmp/
	@@cd ../tarballs/opal-$(UPVERSION).tmp ; tar xfj ../$(UPFILENAME)
	@@rm -f ../tarballs/opal-$(UPVERSION).tmp/opal*/plugins/LID/CM_HID/CM_HID.dll
	@@rm -f ../tarballs/opal-$(UPVERSION).tmp/opal*/plugins/LID/TigerJet/TjIpSys.dll
	@@rm -f ../tarballs/opal-$(UPVERSION).tmp/opal*/*.exe
	@@echo Writing archive ../tarballs/$(FILENAME) ...
	@@cd ../tarballs/opal-$(UPVERSION).tmp ; tar cfJ ../$(FILENAME) *
	@@echo Cleaning up...
	@@$(RM) -rf ../tarballs/opal-$(UPVERSION).tmp/ ../tarballs/$(UPFILENAME)
