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

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

# This is for the .wapi directory for Mono.
export MONO_SHARED_DIR=$(CURDIR)

DEB_VERSION = $(shell dpkg-parsechangelog | grep ^Vers | cut -d\  -f2)
VERSION = $(shell echo $(DEB_VERSION) | sed 's,-.*,,' | sed 's,+dfsg.*,,')
ABIVERSION = 0.1

CFLAGS = -Wall -g

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

configure: configure-stamp
configure-stamp: 
	dh_testdir

	touch configure-stamp

build: build-arch build-indep

build-arch:

build-indep: build-indep-stamp

build-indep-stamp: configure-stamp 
	dh_testdir
	
	nant -v -buildfile:default.build -D:"Mono.Cecil.dll"="$(shell pkg-config --variable=Libraries mono-cecil | cut -d' ' -f1)" \
		-D:"keyfile"="debian/mono.snk"

	touch $@

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

	nant -v clean

	dh_clean 

install: install-arch install-indep

install-arch: build-arch

install-indep: build-indep
	dh_testdir
	dh_testroot
	dh_prep

	dh_installdirs
	
	mkdir debian/tmp
	mkdir -p debian/tmp/usr/lib/pkgconfig
	cp -a $(CURDIR)/bin debian/tmp
	cp debian/cecil-flowanalysis.pc debian/tmp/usr/lib/pkgconfig

	# Files are yet installed. We only need to deleted unwanted ones.
	rm -f $(CURDIR)/debian/tmp/bin/*Test*

ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	find debian/tmp -type f -name "*.mdb" -delete
endif

binary: binary-indep binary-arch

binary-arch: build-arch install-arch

binary-indep: build-indep install-indep
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_install --sourcedir=debian/tmp/
	dh_installcligac
	dh_compress
	dh_fixperms
	dh_clifixperms
	dh_clideps -d
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

BASE_NAME = cecil-flowanalysis
TARBALL_NAME = $(BASE_NAME).tar.gz
SVN_REV = $(shell echo $(DEB_VERSION) | sed 's,-.*,,' | sed 's,.*\.,,')
TARBALL_VERSION = 0.1~svn.$(SVN_REV)
$(TARBALL_NAME):
	svn export http://anonsvn.mono-project.com/source/trunk/cecil/flowanalysis/ $(BASE_NAME)-$(TARBALL_VERSION) -r$(SVN_REV)
	tar -czf $(TARBALL_NAME) $(BASE_NAME)-$(TARBALL_VERSION)

get-orig-source: $(TARBALL_NAME)
	mv $(TARBALL_NAME) $(BASE_NAME)_$(TARBALL_VERSION).orig.tar.gz
	rm -rf $(BASE_NAME)-$(TARBALL_VERSION)

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