#!/usr/bin/make -f

PACKAGE := gnugk
DEBVERSION := $(shell head -n 1 debian/changelog \
		    | sed -e 's/^[^(]*(\([^)]*\)).*/\1/')
DEB_NOEPOCH_VERSION:=$(shell echo $(DEBVERSION) | cut -d':' -f 2)
DEB_SRC_VERSION:=$(shell echo $(DEB_NOEPOCH_VERSION) | sed -e 's/-[^-]\+$$//')
UPVERSION := $(shell echo $(DEB_SRC_VERSION) | sed 's/~dfsg//')
FILENAME := $(PACKAGE)_$(DEB_SRC_VERSION).orig.tar.gz
UPFILENAME := $(PACKAGE)-$(UPVERSION).tar.gz
URL := http://qa.debian.org/watch/sf.php/openh323gk/$(UPFILENAME)


## pwlib_compat.h does not set this on OpenH323 >= 1.19 by mistake
export STDCCFLAGS+= -DhasSRV=1

export PREFIX=/usr/
export OPENH323DIR=/usr/share/openh323
export OPENH323MAKEDIR=/usr/share/openh323
export OH323_LIBDIR=/usr/lib
export PTLIB_CONFIG=/usr/bin/ptlib-config
export PTLIBDIR=$(shell [ -x /usr/bin/ptlib-config ]&&/usr/bin/ptlib-config --ptlibdir)

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

override_dh_auto_configure:
	dh_auto_configure -- --with-pgsql-include-dir=`/usr/bin/pg_config --includedir` --disable-unixodbc

override_dh_auto_build:
	dh_auto_build -- opt doc
	$(MAKE) -C addpasswd opt

override_dh_auto_test:
	obj*/gnugk -h > /dev/null

override_dh_installchangelogs:
	dh_installchangelogs changes.txt

override_dh_install_init:
	dh_install_init -- defaults 90 10

override_dh_installdocs:
	dh_installdocs -X.cvsignore

override_dh_auto_install:
	$(MAKE) install DESTDIR=$(CURDIR)/debian/gnugk INSTALL_DIR=$(CURDIR)/debian/gnugk/usr INSTALLBIN_DIR=$(CURDIR)/debian/gnugk/usr/sbin
	install --mode=640 --owner=root --group=root \
		etc/complete.ini $(CURDIR)/debian/gnugk/etc/gatekeeper.ini

override_dh_fixperms:
	dh_fixperms
	chmod -x debian/gnugk/usr/share/doc/gnugk/examples/odbc.ini

get-orig-source:
	@@if [ -x /usr/bin/wget ];then \
		echo 'Grabbing original upstream tarball...' ; \
		wget -nv --content-disposition $(URL) ; \
		echo 'Repacking as DFSG-free (removing configure.exe)' ; \
		mkdir gnugk-tmp ; cd gnugk-tmp ; tar xf "../$(UPFILENAME)" ; \
		rm -f gnugk*/configure.exe ../$(UPFILENAME) ; \
		echo "Writing new archive $(FILENAME)" ; \
		tar cfz ../$(FILENAME) gnugk-* ; \
		echo "Cleaning up..." ; \
		cd .. ; rm -rf gnugk-tmp ; \
	else \
		echo "Please install wget and run again."; \
	fi
