#!/usr/bin/make -f

#export DH_VERBOSE:=1

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

FILENAME := opal_$(UPVERSION)~dfsg.orig.tar.gz
UPFILENAME := opal_$(UPVERSION).orig.tar.gz
URL := http://transact.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)

# to collect all symbols on all archs in experimental
# and set this to 1 or higher once we move to unstable
export DPKG_GENSYMBOLS_CHECK_LEVEL=0

%:
	dh $@ --parallel --with autotools_dev --with=pkgkde-symbolshelper


override_dh_auto_configure:
	if [ -d plugins/audio/iLBC ];then \
	   echo "DFSG ERROR: iLBC code must be removed from upstream sources" ; false ; \
	fi
	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 iLBC, DLLs and EXEs)...'
	@@mkdir -p ../tarballs/opal-$(UPVERSION).tmp/
	@@cd ../tarballs/opal-$(UPVERSION).tmp ; tar xfj ../$(UPFILENAME)
	@@rm -rf ../tarballs/opal-$(UPVERSION).tmp/opal*/plugins/audio/iLBC
	@@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 cfz ../$(FILENAME) *
	@@echo Cleaning up...
	@@$(RM) -rf ../tarballs/opal-$(UPVERSION).tmp/ ../tarballs/$(UPFILENAME)
