#!/usr/bin/make -f

PACKAGE=libpt-1.10.10

ifneq (,$(shell test -f version.h&&grep MAJOR_VERSION version.h))
VER_MAJOR := $(shell awk '/MAJOR_VERSION/ { print $$3 }' version.h 2>/dev/null)
VER_MINOR := $(shell awk '/MINOR_VERSION/ { print $$3 }' version.h 2>/dev/null)
VER_SUB   := $(shell awk '/BUILD_NUMBER/  { print $$3 }' version.h 2>/dev/null)
endif

SHLIBSVER := $(VER_MAJOR).$(VER_MINOR).$(VER_SUB)

CFLAGS := -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
       CFLAGS += -O0
endif


# used for sed-ating out host specific data
export UNAME_R=`uname -r`

export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
export DEB_HOST_ARCH_OS   ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)

ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
  confflags += --build $(DEB_HOST_GNU_TYPE)
else
  confflags += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

# Ensure the build aborts when there are still references to undefined
# symbols.
LDFLAGS += -Wl,-z,def

DEBVERSION := $(shell head -n 1 debian/changelog \
		    | sed -e 's/^[^(]*(\([^)]*\)).*/\1/')
UPVERSION := $(shell echo $(DEBVERSION) | sed -r -e 's/^.*://' -e 's/-[0-9.]*(\+b[0-9])?$$//' -e 's/.dfsg[0-9]*$$//')
URLVERSION := $(shell echo $(DEBVERSION) | sed -r -e 's/^.*://' -e 's/-[0-9.]*(\+b[0-9])?$$//' -e 's/.dfsg[0-9]*$$//' -e 's/\./_/g')

FILENAME := pwlib_$(UPVERSION)~dfsg1.orig.tar.gz
UPFILENAME := pwlib_$(UPVERSION).orig.tar.gz
URL := http://www.voxgratia.org/releases/pwlib-v$(URLVERSION)-src-tar.gz



build: build-arch build-indep

build-arch: config.status build-arch-stamp

config.status: autotools configure
	./configure $(confflags) --enable-plugins --enable-v4l2 --enable-oss --disable-v4l --disable-avc --disable-dc --prefix=/usr

autotools:
	rm -f config.sub config.guess
	cp /usr/share/misc/config.sub config.sub
	cp /usr/share/misc/config.guess config.guess
	rm -f plugins/config.sub plugins/config.guess
	cp /usr/share/misc/config.sub plugins/config.sub
	cp /usr/share/misc/config.guess plugins/config.guess
	touch autotools

build-arch-stamp: config.status
	# first build static and shared...
	# ### internal settings and variables structure:
	# make/ptlib.mak
	# -> make/unix.mak
	#  (P_SHAREDLIB = 0|1) - build shared library
	#  (OBJ_SUFFIX = r|d)  - what object suffix will we use (removed with Debian patch)
	#  (OBJDIR_SUFFIX = $(OBJ_SUFFIX)) - what the target namespace for all the objects will be
	#  (LIB_TYPE = _s) # for static lib (P_SHAREDLIB = 0)
	#  (PW_LIBDIR       = $(PWLIBDIR)/lib) - target where libpt.so.* will be built into
	## PTLIB_BASE      = pt_$(PLATFORM_TYPE)_$(OBJ_SUFFIX)  --- this one is just "pt" for Debian
	## PTLIB_FILE      = lib$(PTLIB_BASE)$(LIB_TYPE).$(LIB_SUFFIX)
	## PT_OBJBASE      = obj_$(PLATFORM_TYPE)_$(OBJDIR_SUFFIX)
	#  (if not debian-patched)
	#  --> make/ptbuildopts.mak
	# -> make/common.mak
	#  PTLIB_BASE=pt
	#  CFLAGS += -DPTRACING=1
	#  1. shared, with ptrace but opt: DEBUG=0 P_SHAREDLIB=1 OBJDIR_SUFFIX=r optshared
	#  2. static, with ptrace but opt: DEBUG=0 P_SHAREDLIB=0 OBJDIR_SUFFIX=r optnoshared
	
	# put CFLAGS to build with PTRACING code
	$(MAKE) PWLIBDIR=$(CURDIR) CFLAGS="$(CFLAGS) -DPTRACING=1" optshared
	$(MAKE) PWLIBDIR=$(CURDIR) CFLAGS="$(CFLAGS) -DPTRACING=1" optnoshared
	
	# then we ensure that the fresh build works with hello world.
	# ...in static
	$(MAKE) PWLIBDIR=$(CURDIR) -C samples/hello_world/ clean
	$(RM) -rf samples/hello_world/obj_*
	
	# put CFLAGS to build with PTRACING code
	$(MAKE) PWLIBDIR=$(CURDIR) CFLAGS="$(CFLAGS) -DPTRACING=1" -C samples/hello_world/ optnoshared
	
	samples/hello_world/obj_*_r/hello
	
	# ...and in shared..
	$(MAKE) PWLIBDIR=$(CURDIR) -C samples/hello_world/ clean
	$(RM) -rf samples/hello_world/obj_*
	$(MAKE) PWLIBDIR=$(CURDIR) CFLAGS="$(CFLAGS) -DPTRACING=1" -C samples/hello_world/ optshared
	if (samples/hello_world/obj_*_r/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
	LD_LIBRARY_PATH=$(CURDIR)/lib samples/hello_world/obj_*_r/hello
	# if this won't work, we needn't even try going any further...
	# cleanup to make sure we don't install any of the samples we just compiled
	$(MAKE) PWLIBDIR=$(CURDIR) -C samples/hello_world/ clean
	$(RM) -rf samples/hello_world/obj_*
	
	touch build-arch-stamp

build-indep: build-indep-stamp
build-indep-stamp: config.status
	$(MAKE) docs
	touch build-indep-stamp

debian/control: debian/control.in
	sed -e 's#@@PACKAGE@@#$(PACKAGE)#g' debian/control.in >debian/control

clean:
	$(checkdir)
	rm -f build-arch-stamp build-indep-stamp
	if [ -f Makefile ];then \
		$(MAKE) PWLIBDIR=$(CURDIR) clean || true ; \
		$(MAKE) PWLIBDIR=$(CURDIR) -C samples/hello_world/ clean||true ; \
	fi
	rm -rf samples/hello_world/obj_*
	rm -f include/ptbuildopts.h
	rm -f Makefile config.log config.status autotools
	rm -f config.sub config.guess plugins/config.sub plugins/config.guess
	rm -f plugins/Makefile plugins/config.status plugins/config.log
	rm -rf lib
	rm -f make/ptbuildopts.mak make/ptlib-config include/ptbuildopts.h
	rm -rf plugins/pwlib*
	rm -f  plugins/Makefile plugins/config.status plugins/config.log
	rm -f `find . -name "*~"`
	rm -rf `find debian/* -type d -name "libpt-*"`
	rm -rf  debian/files* core
	rm -f debian/*substvars
	rm -f debian/*.debhelper
	rm -rf html
#	rm -f debian/libpt-*.*.docs debian/libpt-*.*.manpages debian/libpt-*.*.dirs
	rm -f core a.out
	dh_clean

install: install-indep install-arch
install-indep:
	dh_testdir
	dh_testroot
	dh_clean -k -i
	dh_installdirs -i

install-arch:
	dh_testdir
	dh_testroot
	dh_clean -k -a
	dh_installdirs -a
	
	install -d -m 755 $(CURDIR)/debian/$(PACKAGE)/usr/lib
	install -m 755 lib/libpt.so.*.*.* $(CURDIR)/debian/$(PACKAGE)/usr/lib

#	$(PACKAGE)-dbg (will be filled by dh_strip)
	cp debian/libpt-dbg.dirs debian/$(PACKAGE)-dbg.dirs

#	$(PACKAGE)-dev
	ln -s libpt.so.$(SHLIBSVER) debian/$(PACKAGE)-dev/usr/lib/libpt.so

	cp -vr include/pt* debian/$(PACKAGE)-dev/usr/include
	cp -vr make `pwd`/debian/$(PACKAGE)-dev/usr/share/pwlib
	sed -i -e 's#$$(PWLIBDIR)/include/ptlib/unix#/usr/include/ptlib/unix#g' \
	    -e 's#$$(PWLIBDIR)/lib#/usr/lib#g' debian/$(PACKAGE)-dev/usr/share/pwlib/make/unix.mak
	# copy static opt version to $(PACKAGE)-dev
	install -m 755 lib/libpt_s.a debian/$(PACKAGE)-dev/usr/lib/
	find debian/$(PACKAGE)-dev/usr/include -type f -exec chmod 644 {} \;
	# ptlib-config in $(PACKAGE)-dev
	# changing build/machine dependent data in ptlib-config
	-rm -f `find debian/$(PACKAGE)-dev/ -name ptlib-config`
	sed -e 's#^OSRELEASE=.*#OSRELEASE=\"\"#g' \
	    -e 's#^LIBDIR=.*#LIBDIR=\"/usr/lib\"#g' \
	    -e 's#^\(CCFLAGS=.*-DP_LINUX\)='$(UNAME_R)'\(.*\)\"#\1 \2 -DPHAS_TEMPLATES -I/usr/include/ptlib -I/usr/include/ptlib/unix\"#g' \
	    -e 's#^\(LIBS=.*\)\"#\1 -lpt\"#g' \
	    make/ptlib-config > debian/$(PACKAGE)-dev/usr/bin/ptlib-config
	chmod 0755 debian/$(PACKAGE)-dev/usr/bin/ptlib-config
	ln -s ../../../bin/ptlib-config debian/$(PACKAGE)-dev/usr/share/pwlib/make/
	# erasing build host specific && unnecessary stuff off ptbuildopts.mak
	-rm -f `find debian/$(PACKAGE)-dev/ -name ptbuildopts.mak`
	sed -e 's#^PWLIBDIR.*#PWLIBDIR = /usr/share/pwlib#g' \
	    -e 's#^OSRELEASE.*#OSRELEASE =#g' \
	    -e 's#^\(STDCCFLAGS.*-DP_LINUX\)='$(UNAME_R)'\(.*\)#\1 \2#g' \
	    make/ptbuildopts.mak > debian/$(PACKAGE)-dev/usr/share/pwlib/make/ptbuildopts.mak
	# for using /usr/share/pwlib as a single PWLIBDIR
	ln -s ../../lib debian/$(PACKAGE)-dev/usr/share/pwlib
	
	cp version.h debian/$(PACKAGE)-dev/usr/include/ptlib/
	ln -s ../../include/ptlib/version.h debian/$(PACKAGE)-dev/usr/share/pwlib/
	ln -s ../../../include/ptbuildopts.h debian/$(PACKAGE)-dev/usr/share/pwlib/include
	ln -s ../../../include/ptlib.h debian/$(PACKAGE)-dev/usr/share/pwlib/include
	ln -s ../../../include/ptlib debian/$(PACKAGE)-dev/usr/share/pwlib/include
	# should not be part of a distribution package:
	-rm -f `find debian/$(PACKAGE)-dev/ -name "*.in"`

# plugins

	#$(PACKAGE)-plugins-v4l2
	install -d -m 755 debian/$(PACKAGE)-plugins-v4l2/usr/lib/pwlib/$(SHLIBSVER)/device/videoinput/
	cp plugins/pwlib/device/videoinput/v4l2_pwplugin.so \
		debian/$(PACKAGE)-plugins-v4l2/usr/lib/pwlib/$(SHLIBSVER)/device/videoinput/

	#$(PACKAGE)-plugins-oss
	install -d -m 755 debian/$(PACKAGE)-plugins-oss/usr/lib/pwlib/$(SHLIBSVER)/device/sound/
	cp plugins/pwlib/device/sound/oss_pwplugin.so \
		debian/$(PACKAGE)-plugins-oss/usr/lib/pwlib/$(SHLIBSVER)/device/sound/

ifeq ($(DEB_HOST_ARCH_OS), linux)
	#$(PACKAGE)-plugins-alsa
	install -d -m 755 debian/$(PACKAGE)-plugins-alsa/usr/lib/pwlib/$(SHLIBSVER)/device/sound/
	cp plugins/pwlib/device/sound/alsa_pwplugin.so \
		debian/$(PACKAGE)-plugins-alsa/usr/lib/pwlib/$(SHLIBSVER)/device/sound/
endif

# Must not depend on anything. This is to be called by
# binary-arch/binary-multi
# in another 'make' thread.
binary-common:
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installman
	if [ ! -f ChangeLog ];then \
		dh_installchangelogs ;\
	else \
		dh_installchangelogs ChangeLog; \
	fi
	
#	Remove all CVS sub-directories
	find debian -name "CVS" -type d|xargs rm -rf
	find debian -name .cvsignore -exec rm -f {} \;
	
	dh_link
	dh_compress
	dh_fixperms
	# a new variable will be needed if an epoch is introduced to our versioning
	dh_makeshlibs -V '$(PACKAGE) (>= $(UPVERSION)~)'
	dh_installdeb
	dh_shlibdeps -L $(PACKAGE) -l debian/$(PACKAGE)/usr/lib
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-strip:
	dh_testdir
	dh_testroot

ifneq (4,$(DH_COMPAT))
	dh_strip -N$(PACKAGE)-dbg --dbg-package=$(PACKAGE)-dbg
else
	# As dh_strip does not work fine for multiple binary package
	# we need to workaround that manually
	dh_strip --exclude=libpt_d --keep-debug

	for pack in $(PACKAGE) $(PACKAGE)-plugins-alsa $(PACKAGE)-plugins-oss \
		$(PACKAGE)-plugins-v4l2 $(PACKAGE)-plugins-avc \
		$(PACKAGE)-plugins-dc ; \
	do \
		cp -af debian/$$pack/usr/lib/debug debian/$(PACKAGE)-dbg/usr/lib && \
			rm -rf debian/$$pack/usr/lib/debug; \
	done
endif

# Build architecture independant packages using the common target.
binary-indep: build-indep install-indep
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture dependant packages using the common target.
binary-arch: build-arch install-arch
	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-strip
	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common

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

get-orig-source.disabled:
	@@dh_testdir
	@@[ -d ../tarballs/. ]||mkdir -p ../tarballs
ifneq (,$(findstring dfsg,$(DEBVERSION)))
	@@echo Downloading $(UPFILENAME) from $(URL) for repacking ...
	@@wget -N -nv -T10 -t3 -O ../tarballs/$(UPFILENAME) $(URL)
	@@echo Repacking as DFSG-free...
	@@mkdir -p ../tarballs/pwlib-$(UPVERSION).tmp/
	@@cd ../tarballs/pwlib-$(UPVERSION).tmp ; \
	tar xfz ../$(UPFILENAME) 
	@@rm -f ../tarballs/pwlib-$(UPVERSION).tmp/pwlib*/samples/xmpptest/draft-ietf-*
	@@echo Writing archive ../tarballs/$(FILENAME)
	@@cd ../tarballs/pwlib-$(UPVERSION).tmp ; \
	tar cfz ../$(FILENAME) *
	@@echo Cleaning up...
	@@$(RM) -rf ../tarballs/pwlib-$(UPVERSION).tmp/ ../tarballs/$(UPFILENAME)
else
	@@echo Downloading $(UPFILENAME) from $(URL) ...
	@@wget -N -nv -T10 -t3 -O ../tarballs/$(UPFILENAME) $(URL)
	@@echo Checking for DFSG-readiness:
	@@if ( tar tfvz ../tarballs/$(UPFILENAME)|grep draft-ietf- );then \
	echo Upstream release not DFSG-clean, removing.; \
	$(RM) ../tarballs/$(UPFILENAME);false; \
	else \
	echo Ready for packaging.;\
	fi
endif

binary: binary-arch binary-indep
.PHONY: build clean binary-indep binary-arch binary install install-indep install-arch
