#!/usr/bin/make -f

GBROWSE_BUILD_OPTIONS = --conf=/etc/gbrowse --htdocs=/usr/share/gbrowse/htdocs --tmp=/var/cache/gbrowse --databases=/var/lib/gbrowse/databases --cgibin=/usr/lib/cgi-bin/gbrowse --www-user=www-data --registration_done=1 --persistent=/var/lib/gbrowse
ARCHLIB := $(shell perl -MConfig -e 'print $$Config{vendorarch}')

%: 
	dh $@ --with apache2

override_dh_auto_configure:
	dh_auto_configure -- $(GBROWSE_BUILD_OPTIONS)

override_dh_installdirs:
	# svn(-buildpackage) seems to ignore the x bit
	chmod +x debian/gbrowse-calign.dirs
	dh_installdirs

override_dh_auto_install:
	./Build  --install_base=debian/gbrowse debianinstall
	#./Build   apache_conf > debian/gbrowse/etc/gbrowse/gbrowse_apache2.conf
	perl Build.PL $(GBROWSE_BUILD_OPTIONS) --installetc=y
	./Build  --install_base=debian/gbrowse install_slave
	mv debian/gbrowse/lib/perl5/* debian/gbrowse/usr/share/perl5
	# Remove arch dependant data
	mv debian/gbrowse/usr/share/perl5/auto/Bio/Graphics/Browser2/CAlign/* debian/gbrowse-calign$(ARCHLIB)/auto/Bio/Graphics/Browser2/CAlign/
	mv debian/gbrowse/usr/share/perl5/Bio/Graphics/Browser2/CAlign.pm debian/gbrowse-calign$(ARCHLIB)/Bio/Graphics/Browser2/CAlign.pm
	rm -rf debian/gbrowse/lib
	rm -f debian/gbrowse/usr/share/doc/gbrowse/INSTALL
	rm -f debian/gbrowse/usr/share/perl5/auto/GBrowse/.packlist
	rm -f debian/gbrowse/usr/share/gbrowse/htdocs/js/prototype.js
	rm -f debian/gbrowse/usr/share/gbrowse/htdocs/js/scriptaculous.js
	chmod 644 debian/gbrowse/etc/gbrowse/MobyServices/text_xml_renderer.pm
	chmod 644 debian/gbrowse/etc/gbrowse/synteny/oryza.synconf.disabled
	#Remove database (kept as separate package)
	rm -rf debian/gbrowse/var/lib/gbrowse/databases/*
	#Rename pl scripts in bin
	rename 's/\.pl//' debian/gbrowse/bin/*.pl
	mv debian/gbrowse/bin debian/gbrowse/usr/bin
	# Activate configuration files.
	find $(CURDIR)/debian/gbrowse/etc -name '*.new' | xargs rename 's/.new//' 
	rm -rf $(CURDIR)/debian/gbrowse/usr/share/perl5/auto
	# Update conf files
	sed -i 's/gbrowse2/gbrowse/g' $(CURDIR)/debian/gbrowse/etc/gbrowse/yeast*.conf
	sed -i 's/gbrowse2/gbrowse/g' $(CURDIR)/debian/gbrowse/etc/gbrowse/slave*.conf

override_dh_installman:
	#Rename due to perl extension seen as polish
	rename 's/\.pl\.1p/\.1p/' debian/gbrowse/man/man1/*.pl.1p
	mkdir -p debian/gbrowse-calign/man/man3/
	mv debian/gbrowse/man/man3/Bio::Graphics::Browser2::CAlign.3pm debian/gbrowse-calign/man/man3/Bio::Graphics::Browser2::CAlign.3pm
	dh_installman
	rm -rf debian/gbrowse/man
	rm -rf debian/gbrowse-calign/man

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