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

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

# This has to be exported to make some magic below work.
export DH_OPTIONS

DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
include /usr/share/quilt/quilt.make
DEB_BUILD_ARCH_CPU ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH_CPU)

ifneq (,$(filter $(DEB_HOST_ARCH_CPU), amd64 alpha ia64 s390x ppc64 sparc64))
export ACEDB_MACHINE=LINUX_4
else
export ACEDB_MACHINE=LINUX_64
endif

configure-stamp: patch
	ln -sf bin.$(ACEDB_MACHINE) bin
	touch $@

#Architecture
build: build-arch build-indep

build-arch: build-arch-stamp
build-arch-stamp: configure-stamp  

	# Add here commands to compile the arch part of the package.
	$(MAKE) tools other
	touch $@

build-indep: build-indep-stamp
build-indep-stamp: configure-stamp  
	# Nothing to be done for indep - for now.
	#$(MAKE) doc
	touch $@

clean:: unpatch
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp build-indep-stamp configure-stamp
	rm -fr bin.$(ACEDB_MACHINE)
	#$(MAKE) clean
	dh_clean bin

install: install-indep install-arch
install-indep:
	# Nothing to be done for indep - for now.
	#dh_testdir
	#dh_testroot
	#dh_prep -i 
	#dh_installdirs -i
	#dh_install --autodest -i

install-arch:
	dh_testdir
	dh_testroot
	dh_prep -s 
	dh_installdirs -s
	if [ ! -d debian/tmp/usr/bin ]; then mkdir -p debian/tmp/usr/bin; fi
	cp -d `find bin/ -type f -a -executable` debian/tmp/usr/bin
	dh_install --autodest -s

# Must not depend on anything. This is to be called by
# binary-arch/binary-indep
# in another 'make' thread.
binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs
	dh_installexamples
	dh_installmenu
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture independant packages using the common target.
binary-indep: build-indep install-indep
	# Nothing to do for indep
	#$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture dependant packages using the common target.
binary-arch: build-arch install-arch
	$(MAKE) -f debian/rules DH_OPTIONS=-s binary-common

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

UPSTREAMVERSION=4.9.39
ATTEMPTNO=01
get-orig-source:
	mkdir -p ../tarballs && \
	cd ../tarballs && \
	fname=ACEDB-source.$(UPSTREAMVERSION).tar.gz && \
	dname=acedb_$(UPSTREAMVERSION)+dfsg.$(ATTEMPTNO) && \
	if [ -r "$$fname" ]; then \
		echo "File '$$fname' already exist, not downloading again." ; \
	else \
		wget ftp://ftp.sanger.ac.uk/pub/acedb/SUPPORTED/$${fname} ; \
	fi && \
	mkdir $$dname && \
	( \
		cd $$dname && \
		tar -xzvf ../$$fname \
	) && \
	rm -f $$dname/wmd5/rfc* && \
	find $$dname -name CVS | xargs -r rm -rf && \
	tar czvf $${dname}.orig.tar.gz $$dname && \
	rm -rf $$dname
