#!/usr/bin/make -f

#export DH_VERBOSE=1
export MONO_SHARED_DIR=$(CURDIR)

DEB_CLI_ABI_VERSION=0.2
DEB_CLI_API_VERSION=0.4

MAKEFILE = $(firstword $(MAKEFILE_LIST))
DEBIAN_DIR = $(dir $(MAKEFILE))
SOURCE_DIR = $(DEBIAN_DIR)/..

DEB_VERSION = $(shell dpkg-parsechangelog -l$(DEBIAN_DIR)/changelog | grep ^Version | cut -d" " -f2)
DEB_SOURCE_NAME = $(shell dpkg-parsechangelog -l$(DEBIAN_DIR)/changelog | grep ^Source | cut -d" " -f2)
VERSION = $(shell echo $(DEB_VERSION) | cut -d"-" -f1)

include /usr/share/quilt/quilt.make

configure: configure-stamp
configure-stamp: $(QUILT_STAMPFN)
	dh_testdir
	

	./configure --prefix=/usr \
	  --mandir=\$${prefix}/share/man \
	  --infodir=\$${prefix}/share/info \
	  --enable-tests=no \
	  MCS=/usr/bin/gmcs
	touch configure-stamp

build: build-stamp
build-stamp: configure-stamp
	dh_testdir
	$(MAKE)
	touch build-stamp

clean: clean-patched unpatch
clean-patched:
	dh_testdir
	dh_testroot
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -rf $(CURDIR)/.wapi
	rm -f configure-stamp build-stamp


	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp

	perl -pe 's/#PACKAGE_ABI_VERSION#/$(DEB_CLI_ABI_VERSION)/g' -i \
	  $(CURDIR)/debian/tmp/usr/lib/pkgconfig/*.pc

binary-indep: build install
	dh_testdir
	dh_testroot
	dh_install -i --sourcedir=debian/tmp
	dh_installchangelogs -i
	dh_installdocs -i
	dh_installexamples -i
	dh_installman -i
	dh_installcligac -i
	dh_link -i
	dh_strip -i
	dh_clistrip -i
	dh_compress -i
	dh_fixperms -i
	dh_clifixperms -i
	dh_installdeb -i
	dh_makeclilibs -i -m $(DEB_CLI_API_VERSION)
	dh_clideps -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch:

binary: binary-arch binary-indep

get-orig-source:
	uscan \
		--package $(DEB_SOURCE_NAME) \
		--watchfile $(DEBIAN_DIR)/watch \
		--upstream-version $(VERSION) \
		--download-version $(VERSION) \
		--destdir . \
		--force-download \
		--rename \
		--repack

.PHONY: build clean binary-arch binary-indep binary install configure get-orig-source
