#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

#  "based" on template debian/rules for most debian packages version 0.31

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

#For overriding debian/compat
#export DH_COMPAT=7

#This rule file have a bashism
SHELL:=/bin/bash

# From dpkg-dev
include /usr/share/dpkg/pkg-info.mk

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

# Personal Variables
PACKNAME=$(shell dh_listpackages)
PACKAGE=$(shell dh_listpackages)

# Variables used by the Debian Maintainer exclusively for package
# development.
#DEB_SOURCE is replacing SOURCENAME
#SOURCENAME=$(shell dpkg-parsechangelog | grep "Source:" | cut -d ' ' -f 2)
#DEB_VERSION is replacing DEBVER
#DEBVER=$(shell dpkg-parsechangelog | grep "Version:" | cut -d ' ' -f 2)
#DEB_VERSION_UPSTREAM is replacing VER
VER=$(shell dpkg-parsechangelog | grep "Version:" | cut -d ' ' -f 2 | cut -f 1 -d -)
ifeq ($(DEB_VERSION),$(DEB_VERSION_UPSTREAM))
NATIVE=1
else
NATIVE=0
endif
DIST=$(shell dpkg-parsechangelog | grep "Distribution" | cut -d ' ' -f 2)
SECTION=non-free
CATEG=my_sources
#CATEG=other_sources
ifeq ($(NATIVE),0)
DISTFILE=$(DEB_SOURCE)_$(DEB_VERSION_UPSTREAM).orig.tar.gz
#DISTFILES=$(DISTFILE) $(DEB_SOURCE)_$(DEB_VERSION).diff.gz
DISTFILES=$(DISTFILE) $(DEB_SOURCE)_$(DEB_VERSION).debian.tar.gz
else
DISTFILE=$(DEB_SOURCE)_$(DEB_VERSION).tar.gz
DISTFILES=$(DISTFILE)
endif




CFLAGS = -Wall -g

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



configure: configure-stamp
configure-stamp:
	dh_testdir
	# Add here commands to configure the package.

	touch configure-stamp


build: build-stamp
build-stamp: configure-stamp
	dh_testdir

	# Add here commands to compile the package.
	$(MAKE) build
	#docbook-to-man debian/$(PACKAGE).sgml > $(PACKAGE).1

	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

	# Add here commands to clean up after the build process.
	$(MAKE) clean

	dh_clean 

install: build
	dh_testdir
	dh_testroot
	#dh_clean -k 
	dh_prep
	dh_installdirs

	# Add here commands to install the package into debian/$(PACKAGE).
	$(MAKE) "DESTDIR=$(CURDIR)/debian/$(PACKAGE)" install 


# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs
	dh_installexamples
#	dh_install
#	dh_installmenu
#	dh_installdebconf	
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
	dh_installinit
	dh_installcron
	dh_installcron --name=cal-scripts-postgres
#	dh_installinfo
	dh_installman
	dh_lintian
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
#	dh_perl
#	dh_python
#	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb


# 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 ../$(DEB_SOURCE) ../$(DEB_SOURCE)-$(DEB_VERSION_VERSION)
	cd .. && tar --exclude='.svn' --exclude='debian' --exclude='tmp' --exclude='*~' -cf - $(DEB_SOURCE) | gzip -v9 -c > $(DISTFILE)
	cd .. && svn add $(DISTFILE)
else
	# native package
	cd .. && tar --exclude='.svn' --exclude='tmp' --exclude='*~' -czf - $(DEB_SOURCE) | gzip -v9 -c > $(DISTFILE)
	cd .. && svn add $(DISTFILE)
endif


# Check variabels used by the Debian Maintainer exclusively for
# package development.
vars:
	echo $(DEB_VERSION_UPSTREAM) $(DEB_VERSION) $(DIST) $(PACKNAME) $(DEB_SOURCE)
	echo $(REPOSTARGET)
	echo $(REPOSARCH)
	echo $(DEB_HOST_GNU_TYPE) $(DEB_BUILD_GNU_TYPE)
	echo $(DEB_SOURCE) $(DEB_VERSION) $(DEB_VERSION_EPOCH_UPSTREAM)
	echo $(DEB_VERSION_UPSTREAM_REVISION) $(DEB_VERSION_UPSTREAM)
	echo $(DEB_DISTRIBUTION) $(SOURCE_DATE_EPOCH)


# 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 $(CURDIR)
	cd .. ; svn add $(DEB_SOURCE)_$(DEB_VERSION)_*.changes \
			$(DEB_SOURCE)_$(DEB_VERSION)_*.build \
			$(DEB_SOURCE)_$(DEB_VERSION).dsc
	cd .. ; svn add *_$(DEB_VERSION)_*.deb
	cd .. ; svn st ; echo "enter to continue" ; read enter
	cd .. ; shopt -s nullglob ; svn ci -m "released v$(DEB_VERSION)" \
		$(DISTFILES) \
		$(DEB_SOURCE)_$(DEB_VERSION)_*.changes \
		$(DEB_SOURCE)_$(DEB_VERSION)_*.build \
		$(DEB_SOURCE)_$(DEB_VERSION).dsc \
		*_$(DEB_VERSION)_*.deb
	svn mkdir --parents -m "Preparing tag directory for release v$(DEB_VERSION)" $(shell svn info | grep "Repository Root" | cut -d ' ' -f 3)/tags/src/$(DEB_SOURCE)/$(DEB_SOURCE)-$(DEB_VERSION)
	cd .. ; shopt -s nullglob ; svn copy -m "released v$(DEB_VERSION)" \
		$(DISTFILES) \
		$(DEB_SOURCE)_$(DEB_VERSION)_*.changes \
		$(DEB_SOURCE)_$(DEB_VERSION)_*.build \
		$(DEB_SOURCE)_$(DEB_VERSION).dsc \
		*_$(DEB_VERSION)_*.deb \
		$(shell svn info | grep "Repository Root" | cut -d ' ' -f 3)/tags/src/$(DEB_SOURCE)/$(DEB_SOURCE)-$(DEB_VERSION)
	debchange -i "New release"


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