#!/usr/bin/make -f

PN = apq
LIBDIR = debian/tmp/usr/lib/$(PN)/relocatable

include /usr/share/dpkg/buildflags.mk
include /usr/share/ada/debian_packaging.mk
export LDFLAGS
export ADAFLAGS

SOVERSION=3.2.0.1
export SOVERSION

POLICY_TARGETS := binary binary-arch binary-indep build build-arch \
	build-indep clean
.PHONY: $(POLICY_TARGETS)
$(POLICY_TARGETS):
	dh ${@}

# These empty targets let dh know that it should not call upstream
# Makefile by itself. Some of them are overriden below.
.PHONY: $(addprefix override_dh_auto_, \
	clean configure build-arch build-indep test install-arch install-indep)

override_dh_auto_clean:
	$(MAKE) distclean
	$(MAKE) -C manual clean

	# Restore original file mode
	chmod 755 samples/Makefile

	rm -f manual/*.out
	rm -f lib/*

	if test -e manual/floatflt.sty.backup; then \
		mv -f manual/floatflt.sty.backup manual/floatflt.sty; \
	fi

override_dh_auto_build-arch:
# -j in BUILDER_OPTIONS override the one in scripts/buildutils.sh.
	./configure --prefix=./debian/tmp/usr/ --gprbuild=gnatmake \
		--gprbuild-params="$(BUILDER_OPTIONS)"
	dh_auto_build

override_dh_auto_build-indep: manual/floatflt.sty.backup
	$(MAKE) -C manual

# Move embedded .sty out of the way so that TeX uses packaged version.
manual/floatflt.sty.backup: %.backup: %
	mv $* $@

override_dh_auto_install-arch:
	dh_auto_install
	rm -f $(LIBDIR)/lib$(PN).so

	chmod 644 $(LIBDIR)/*
	chmod 644 debian/tmp/usr/lib/$(PN)/static/lib$(PN).a

	ln -sf lib$(PN).so.`basename $(LIBDIR)/lib$(PN).so.*.* | sed -e 's/lib$(PN).so.//'` $(LIBDIR)/lib$(PN).so

	mv $(LIBDIR)/*.so* debian/tmp/usr/lib
	mv debian/tmp/usr/lib/$(PN)/static/lib$(PN).a debian/tmp/usr/lib

	mkdir -p debian/tmp/usr/share/ada/adainclude
	mv debian/tmp/usr/src/$(PN) debian/tmp/usr/share/ada/adainclude
	chmod -R 444 debian/tmp/usr/share/ada/adainclude/$(PN)/*

	cp debian/misc/*.gpr debian/tmp/usr/share/ada/adainclude

	mkdir -p debian/tmp/usr/lib/ada/adalib/$(PN)
	mv $(LIBDIR)/*.ali debian/tmp/usr/lib/ada/adalib/$(PN)
	chmod -R 444 debian/tmp/usr/lib/ada/adalib/$(PN)/*

	chmod 644 samples/Makefile

	rm -rf debian/tmp/usr/lib/apq
	rm -rf debian/tmp/usr/lib/gnat

.PHONY: override_dh_install
override_dh_install:
	dh_install --all --fail-missing

.PHONY: override_dh_strip
override_dh_strip:
	dh_strip --dbg-package=lib$(PN)-dbg
	dh_strip --remaining-packages
