#!/usr/bin/make -f

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

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

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

override_dh_clean:
	if [ -f Makefile ];then \
		$(MAKE) PTLIBDIR=$(CURDIR) -C samples/hello_world clean||true ; \
		$(MAKE) distclean || true ; \
	fi
	rm -rf html
	dh_clean

override_dh_auto_configure:
	dh_auto_configure -- --enable-oss --enable-v4l --disable-avc --disable-dc --enable-exceptions

override_dh_auto_build:
	dh_auto_build -- optshared docs

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

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

override_dh_auto_test:
	$(MAKE) PTLIBDIR=$(CURDIR) -C samples/hello_world clean
	$(MAKE) PTLIBDIR=$(CURDIR) -C samples/hello_world opt
	#if (samples/hello_world/obj*/hello 2>/dev/null);then \
	#   echo opt hello_world sample incorrectly compiled static; false ; \
	#else \
	#   echo opt hello_world sample correctly compiled non-static; \
	#fi
	ln -s lib* lib ; LD_LIBRARY_PATH=$(CURDIR)/lib samples/hello_world/obj*/hello
	# if this won't work, we needn't even try going any further...
	rm lib
	# make sure samples are clean before we install them into libpt-doc
	$(MAKE) PTLIBDIR=$(CURDIR) -C samples/hello_world clean
	rm -rf samples/hello_world/obj_*

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 windows EXEs)...'
	@@mkdir -p ../tarballs/ptlib-$(UPVERSION).tmp/
	@@cd ../tarballs/ptlib-$(UPVERSION).tmp ; tar xfj ../$(UPFILENAME)
	@@find ../tarballs/ptlib-$(UPVERSION).tmp/ -iname '*.exe' -delete
	@@echo Writing archive ../tarballs/$(FILENAME) ...
	@@cd ../tarballs/ptlib-$(UPVERSION).tmp ; tar cfz ../$(FILENAME) *
	@@echo Cleaning up...
	@@$(RM) -rf ../tarballs/ptlib-$(UPVERSION).tmp/ ../tarballs/$(UPFILENAME)
