#!/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.

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

DESTDIR=$(CURDIR)/debian/ensembl

configure: configure-stamp
configure-stamp:
	# nothing to be configured
	touch configure-stamp

# Ignoring direct call to build
build: #build-indep-stamp build-arch
	echo "Build command ignored. Ask for a binary."

build-arch: ensembl-variation/C_code/calc_genotypes
ensembl-variation/C_code/calc_genotypes:
	$(MAKE) -C ensembl-variation/C_code calc_genotypes

build-indep: build-indep-stamp
build-indep-stamp:
	# nothing to build for the Perl packages or the database
	# well, those bits should already have been fixed in the source tree, but, who knows...
	find . -name CVS -a -type d | xargs -r rm -r
	find . -name "*.pm" -o -name "*.png" -o -name "*.gif" -o -name README \
                 -o -name "*.sql" -o -name "*.pdf" -o -name "*.html" \
		-o -name "*.sxw" -o -name "*.conf*" -o -name "*.guess" -o -name "*.sub" \
		-o -name "*.h" -o -name "*.cpp" -o -name "*.txt" \
				| grep -v "/.pc/" | xargs -r --max-args 10 chmod 644
	#find . -name "*.pl" | xargs -r chmod 755
	find . -name ".cvsignore" | xargs -r rm

	# First round of updates
	find . -name "*.pl" -o -name "*.sh" -o -name "*.t" -o -name "*.pm" -o \
		-name "do_flatfile_dump_registry" -o -name "parse_snp_to_ensembl_mouse" \
				| grep -v "/.pc/" | xargs -r --max-args 1 debian/adjust_shebang_line.sh

	#d=$(DESTDIR)/usr/share/ensembl
	echo "Adjusting path to Perl wherever it may be found" ; \
	egrep -rl "^#!\s*/.*/perl" . | grep -v "/.pc/" | xargs -r --max-args 1 sed -i '1s%^#!\s*/\(usr\|localsw\)/.*/perl%#!/usr/bin/perl%' ; \
	egrep -rl "^#!\s*/.*/perl" . | grep -v "/.pc/" | xargs -r --max-args 1 sed -i '1s%^#!\s*/\(usr\|localsw\|software\)/.*/perl%#!/usr/bin/perl%' ; \
	echo "Adjusting path to Bash wherever it may be found" ; \
	egrep -rl "^#!\s*/.*/bash" . | grep -v "/.pc/" | xargs -r --max-args 1 sed -i '1s%^#!\s*/\(usr\|localsw\)/.*/bash%#!/bin/bash%' ; \
	egrep -rl "^#!.*ENV.*PERL" . | grep -v "/.pc/" | xargs -r --max-args 1 sed -i '1s%^#!.*%#!/usr/bin/perl%' ; \

	# First round of updates
	#find . -name "*.pl" -o -name "*.sh" | xargs -r debian/adjust_chmod.sh

	touch $@

install-arch: build-arch
	dh_testdir
	dh_testroot
	dh_prep -a
	dd="$(DESTDIR)-bin/usr/bin" ; \
	if [ ! -d "$$$d" ]; then \
		echo "Creating directory '$$dd'." ; \
		mkdir -p "$$dd" ; \
	fi ; \
	cp ensembl-variation/C_code/calc_genotypes $$dd

install-indep: build-indep install-indep-copying
	# copying files that are considered too different from the original
	# and/or the original too variable across releases to be patched
	if [ ! -d "$(DESTDIR)/etc/default/ensembl.d" ]; then mkdir -p "$(DESTDIR)/etc/default/ensembl.d"; fi
	cp debian/ensembl.overwrites/Plugins.pm        "$(DESTDIR)/etc/default/ensembl.d/"
	cp debian/ensembl.overwrites/confSiteDefs.pm   "$(DESTDIR)/etc/default/ensembl.d/"
	cp debian/ensembl.overwrites/pluginSiteDefs.pm "$(DESTDIR)/etc/default/ensembl.d/"
	find debian/ensembl -name config.guess -o -name config.sub -o -name configure -o \
		-name depcomp -o -name install-sh -o -name missing -o -name Makefile.PL | xargs -r rm

install-indep-copying: install-indep-copying-stamp
install-indep-copying-stamp:
	dh_testdir
	dh_testroot
	dh_prep -i
	d="$(DESTDIR)/usr/share/ensembl" ; \
	if [ ! -d "$$d" ]; then \
		echo "Creating directory '$$d'." ; \
		mkdir -p "$$d" ; \
	fi ; \
	echo "Copying to '$$d'"; \
	cp -r conf ctrl_scripts \
		htdocs modules perl public-plugins utils \
		ensembl ensembl-compara ensembl-draw \
		ensembl-external \
		ensembl-functgenomics ensembl-variation \
	    $$d ;

	m="$(DESTDIR)-doc/usr/share/doc/ensembl-doc"; if [ ! -d "$$m" ]; then mkdir -p "$$m"; fi ; \
	for doc in $(DESTDIR)/usr/share/ensembl/*/docs ; do \
		docdir=`dirname $$doc` ; \
		docdirbase=`basename $$docdir` ; \
		from="$(DESTDIR)/usr/share/ensembl/$$docdirbase/docs" ; \
		to="$$m/$$docdirbase" ; \
		echo "Moving from '$$from' to '$$to'.\n" ; \
		mv "$$from" "$$to" ; \
	done

	for p in core compara draw external functgenomics variation; do \
		destdir="$(CURDIR)/debian/libensembl-$${p}-perl/usr/share/ensembl" ; \
		if [ ! -d "$$destdir" ]; then mkdir -p "$$destdir"; fi ; \
	done

	from="debian/ensembl/usr/share/ensembl/ensembl" ; \
	to="debian/libensembl-core-perl/usr/share/ensembl/" ; \
	echo "Renaming '$$from' to '$$to'.\n" ; \
	mv "$$from" "$$to"

	# move the remaining directories
	sourcedir="$(CURDIR)/debian/ensembl/usr/share/ensembl" ; \
	for p in compara draw external functgenomics variation; do \
		destdir="$(CURDIR)/debian/libensembl-$${p}-perl/usr/share/ensembl/" ; \
		echo "Moving '$$sourcedir/*-$$p' to '$$destdir'" ; \
		mv $${sourcedir}/*-$$p $$destdir ; \
	done

	# some cleanup
	find debian -name Makefile.PL -o -name "*.tar.gz" | xargs -r rm -f
	rm -rf debian/libensembl-variation-perl/usr/share/ensembl/ensembl-variation/C_code

clean:
	rm -f ensembl-variation/C_code/calc_genotypes.o ensembl-variation/C_code/calc_genotypes
	debconf-updatepo
	dh_clean install-indep-copying-stamp \
		build-stamp build-indep-stamp build-arch-stamp \
		configure-stamp 

binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdebconf
	dh_installdocs
	dh_installexamples
	dh_install
	dh_installman
	dh_installinit
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_perl
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-indep: install-indep
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

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

binary: binary-arch binary-indep

get-orig-source:
	./debian/prepare_ensembl_source_packages.sh

# To update po files as recommended in po-debconf(7)
debian/po/templates.pot: debian/ensembl.templates
	@debconf-updatepo

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