#!/usr/bin/make -f
# debian/rules for arb using quilt

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

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

pkg := $(shell dpkg-parsechangelog | sed -n 's/^Source: //p')
pkgcom=$(pkg)-common
pkgdoc=$(pkg)-doc
common=debian/$(pkgcom)

ARBHOME=$(CURDIR)

BITARCH := $(shell dpkg-architecture -qDEB_BUILD_ARCH_BITS)
ifeq ($(BITARCH),64)
    ARB_64:=1
else
    ARB_64:=0
endif

LD_LIBRARY_PATH := $(ARBHOME)/lib:$(LD_LIBRARY_PATH)
PATH := $(ARBHOME)/bin:$(PATH)
LC_ALL := C

export ARBHOME LD_LIBRARY_PATH PATH LC_ALL
export DEB_CXXFLAGS_MAINT_APPEND=-std=c++98

%:
	dh $@

config.makefile: config.makefile.template
	# ARB's build system is configured with config.makefile. This file is
	# created on the first run of make from config.makefile.template. We
	# just use sed here to set the desired parameters:
	sed -e 's/DEVELOPER := ANY/DEVELOPER := RELEASE/;'\
            -e 's/ARB_64 := 1/ARB_64 := $(ARB_64)/;'\
            -e 's/# DEBIAN := 1/DEBIAN := 1/;' \
             config.makefile.template > config.makefile

override_dh_auto_build:
	$(MAKE) all

override_dh_auto_configure: config.makefile

override_dh_auto_clean: config.makefile
	# config.makefile is required to run make clean, hence the dependency
	# on ..._auto_configure.
	$(MAKE) clean
	rm config.makefile
	# ARB does not have "distclean" or "realclean". Remove some leftovers:
	rm -f UNIT_TESTER/Makefile.setup.local
	find -name \*.log -a ! -name phyml-manual.log -print0 | xargs -0 rm -f
	rm -f TEMPLATES/arb_build.h

override_dh_install:
	# ARB has no "install" target. It has "tarfile_quick", though, which
	# creates a tarball for distribution. We build this and unpack it
	# into debian/tmp so that dh_install can pick up the files.
	make tarfile_quick
	rm -rf debian/tmp
	mkdir debian/tmp
	tar -C debian/tmp -xzf arb.tgz
	rm -rf debian/tmp-dev
	mkdir debian/tmp-dev
	tar -C debian/tmp-dev -xzf arb-dev.tgz
	rm arb.tgz arb-dev.tgz

	# remove some files we don't need in the package:
	rm -rf debian/tmp/lib/addlibs # external libs are packages
	rm  -f debian/tmp/lib/arb_tcp_org.dat # managed in etc
	rm  -f debian/tmp/lib/GPL.txt # included in copyright
	rm  -f debian/tmp/lib/Makefile # spurious in upstream
	rm  -f debian/tmp/arb_install.sh # no installer needed
	rm  -f debian/tmp/arb_INSTALL.txt
	rm  -f debian/tmp/arb_OS_X_MacPorts.txt
	rm  -f debian/tmp/arb_OS_X.txt
	rm  -f debian/tmp/arb_UBUNTU.txt

	dh_install --list-missing

	# the check is needed to make sure it will not fail when doing binary-arch only builds
	if [ -e debian/$(pkgcom)/usr/lib/arb/SH/arb_create_debian_conffile ] ; then \
	    # Move SH/README :to docs \
	    cp -a SH/README debian/$(pkgcom)/usr/share/doc/arb/README_SH ; \
	    # Fix permission of scripts \
	    chmod a+x debian/$(pkgcom)/usr/lib/arb/SH/arb_create_debian_conffile ; \
	    chmod a+x debian/$(pkgcom)/usr/lib/arb/lib/macro.head ; \
	fi

override_dh_strip:
	# Fix rpath issue (once libarb is installed)
	if [ -e debian/libarb/usr/lib/arb/lib/ARB.so ] ; then \
	    chrpath --delete debian/libarb/usr/lib/arb/lib/ARB.so ; \
	fi
	dh_strip 
	find debian/libarb-dev -name \*.a -print0 | xargs -0 -n 1 strip --remove-section=.comment

override_dh_installchangelogs:
	dh_installchangelogs arb_CHANGES.txt

override_dh_shlibdeps:
	dh_shlibdeps -a -l debian/libarb/usr/lib

override_dh_fixperms:
	dh_fixperms
	-chmod 0644 debian/arb-common/etc/arb/submit/*
	-chmod 0644 debian/libarb-dev/usr/include/arb/xml.hxx
	-chmod 0755 debian/arb-common/usr/lib/arb/lib/macro.head

override_dh_link:
	dh_link
	rm -f debian/arb-common/etc/arb/inputMasks/format.readme
	dh_link -parb-common /usr/lib/arb/lib/help/input_mask_format.hlp /etc/arb/inputMasks/format.readme

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