#!/usr/bin/make -f
# -*- makefile -*-

# Copyrigth: 2005-2013 Instituto Superior Técnico
# Author: Jose M Calhariz <jose.calhariz@ist.utl.pt>

# License: GPL2+ with OpenSSL exception


# "based" on template Makefile for most debian packages version 0.24

PACKNAME=$(shell dh_listpackages)
SOURCENAME=$(shell dpkg-parsechangelog | grep "Source:" | cut -d ' ' -f 2)
DEBVER=$(shell dpkg-parsechangelog | grep "Version:" | cut -d ' ' -f 2)
VER=$(shell dpkg-parsechangelog | grep "Version:" | cut -d ' ' -f 2 | cut -f 1 -d -)
ifeq ($(DEBVER),$(VER))
NATIVE=1
else
NATIVE=0
endif
MDIST=wheezy
DIST=$(shell dpkg-parsechangelog | grep "Distribution" | cut -d ' ' -f 2)
SECTION=main
CATEG=my_sources
#CATEG=other_sources
BINDIR=/usr/bin
SBINDIR=/usr/sbin
USRSHAREBASE=/usr/share
USRLIBBASE=/usr/lib
VARLIBBASE=/var/lib
SHAREDIR=$(USRSHAREBASE)/$(PACKNAME)
LIBDIR=$(USRLIBBASE)/$(PACKNAME)
DOCDIR=/usr/share/doc/$(PACKNAME)
ETCBASE=/etc
ETCDIR=$(ETCBASE)/$(PACKNAME)
MANDIR=/usr/share/man/man8
LOGROTATEDIR=/etc/logrotate.d
ZIPFILE=file-2.0.0.zip
ZIPDIR=file-2.0.0
TARFILE=file-$(VER).tar.gz
TARDIR=untar/$(PACKNAME)-$(VER)
ifeq ($(NATIVE),0)
DISTFILE=$(SOURCENAME)_$(VER).orig.tar.gz
#DISTFILES=$(DISTFILE) $(SOURCENAME)_$(DEBVER).diff.gz
DISTFILES=$(DISTFILE) $(SOURCENAME)_$(DEBVER).debian.tar.gz
else
DISTFILE=$(SOURCENAME)_$(DEBVER).tar.gz
DISTFILES=$(DISTFILE)
endif


all:
	echo $(VER) $(DEBVER) $(MDIST) $(DIST) $(PACKNAME) $(SOURCENAME) $(NATIVE)
	echo $(DEB_HOST_GNU_TYPE) $(DEB_BUILD_GNU_TYPE)


include /usr/share/quilt/quilt.make

unpack:unpack-up-stamp
unpack-up-stamp:
	#unzip $(ZIPFILE)
	#unzip $(ZIPFILE) -d $(ZIPDIR)
	#mkdir untar
	#tar xzf $(TARFILE) -C untar
	#Prepare files for patching
	touch unpack-up-stamp


#configure:unpack debian/stamp-patched configure-up-stamp
configure:unpack configure-up-stamp
configure-up-stamp:
	touch configure-up-stamp


build-arch: configure build-arch-up-stamp
build-arch-up-stamp:
	touch build-arch-up-stamp


build-indep: configure build-indep-up-stamp
build-indep-up-stamp:
	touch build-indep-up-stamp


build: build-arch build-indep


install: build installconf-tp-conf-sasl2-cyrus install-tp-conf-ldap-srv


install-tp-conf-ldap-srv:
	install -d debian/tp-conf-ldap-srv/$(SBINDIR)
	install tp-conf-ldap-srv-config debian/tp-conf-ldap-srv/$(SBINDIR)
	install -d debian/tp-conf-ldap-srv/etc/tp-conf-ldap-srv
	install -d debian/tp-conf-ldap-srv/usr/lib/sasl2
	install -m 644 slapd_sasl2.conf debian/tp-conf-ldap-srv/etc/tp-conf-ldap-srv/slapd.conf
	install -d debian/tp-conf-ldap-srv/etc/ldap/sasl2
	install -m 644 slapd_sasl2.conf debian/tp-conf-ldap-srv/etc/ldap/sasl2/slapd.conf
	install -d debian/tp-conf-ldap-srv/etc/init.d
	install -m 644 slapd debian/tp-conf-ldap-srv/etc/init.d/slapd
	ln -s /etc/tp-conf-ldap-srv/slapd.conf debian/tp-conf-ldap-srv/usr/lib/sasl2/slapd.conf 

installconf-%:
	install -d debian/$*/$(SBINDIR)
	install $*-config debian/$*/$(SBINDIR)


clean:
	make unpatch || rm debian/stamp-patched
	find -name '*~' -exec rm {} \;
	rm -rf $(ZIPDIR) untar build-dir
	rm -f  configure-up-stamp unpack-up-stamp build-arch-up-stamp build-indep-up-stamp build-up-stamp install-up-stamp


# Create the files ready for distribution, need to be run in a dir
# without SVN control files

# Non-native package
#dist: clean
#	if test -e ../$(DISTFILE) ; then echo "$(DISTFILE) file should be generated only once" ; false ; fi
#	cd .. && GZIP=-9 tar --exclude='.svn' --exclude='debian' --exclude='tmp' --exclude='*~' -czf $(DISTFILE) $(SOURCENAME)
#	cd .. && svn add $(DISTFILE)

# Create the files ready for distribution
# Debian native package
#dist: clean
#	cd .. && GZIP=-9 tar --exclude='.svn' --exclude='tmp' --exclude='*~' -czf $(DISTFILE) $(SOURCENAME)
#	cd .. && svn add $(DISTFILE)


# Experimental rule to build archive for public distribution
# Non-native package
dist-exp: clean
ifeq ($(NATIVE),0)
	# Non native package
	if test -e ../$(DISTFILE) ; then echo "$(DISTFILE) file should be generated only once" ; false ; fi
	#cp -al ../$(SOURCENAME) ../$(SOURCENAME)-$(VER)
	cd .. && GZIP=-9 tar --exclude='.svn' --exclude='debian' --exclude='tmp' --exclude='*~' -czf $(DISTFILE) $(SOURCENAME)
	cd .. && svn add $(SOURCENAME)_$(VER).orig.tar.gz
else
	# native package
	cd .. && GZIP=-9 tar --exclude='.svn' --exclude='tmp' --exclude='*~' -czf $(DISTFILE) $(SOURCENAME)
	cd .. && svn add $(DISTFILE)
endif


# Create the deb file
deb:
	debuild -i
	debc

packages: deb


# Upload the packages and the distribution files into the rigth places
upload:	
	make -f ../upload upload

# Make tags of the sources as documented by subversion
tag:
	cd .. ; svn add $(DISTFILES) ;\
		echo $(PWD)
	cd .. ; svn add $(SOURCENAME)_$(DEBVER)_*.changes \
			$(SOURCENAME)_$(DEBVER)_*.build \
			$(SOURCENAME)_$(DEBVER).dsc
	cd .. ; svn add *_$(DEBVER)_*.deb
	cd .. ; svn st ; echo "enter to continue" ; read enter
	cd .. ; shopt -s nullglob ; svn ci -m "released v$(DEBVER)" \
		$(DISTFILES) \
		$(SOURCENAME)_$(DEBVER)_*.changes \
		$(SOURCENAME)_$(DEBVER)_*.build \
		$(SOURCENAME)_$(DEBVER).dsc \
		*_$(DEBVER)_*.deb
	svn mkdir --parents -m "Preparing tag directory for release v$(DEBVER)" $(shell svn info | grep "Repository Root" | cut -d ' ' -f 3)/tags/src/$(SOURCENAME)/$(SOURCENAME)-$(DEBVER)
	cd .. ; shopt -s nullglob ; svn copy -m "released v$(DEBVER)" \
		$(DISTFILES) \
		$(SOURCENAME)_$(DEBVER)_*.changes \
		$(SOURCENAME)_$(DEBVER)_*.build \
		$(SOURCENAME)_$(DEBVER).dsc \
		*_$(DEBVER)_*.deb \
		$(shell svn info | grep "Repository Root" | cut -d ' ' -f 3)/tags/src/$(SOURCENAME)/$(SOURCENAME)-$(DEBVER)
	debchange -i "New release"


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