#!/usr/bin/make -f
#
# Debian Makefile for OpenToken
# Copyright (c) 2009-2010 Stephen Leake <stephen_leake@stephe-leake.org>
# Copyright (c) 2013-2016 Nicolas Boulenguez <nicolas@debian.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
#

DEB_BUILD_MAINT_OPTIONS=hardening=+all,-pie
include /usr/share/dpkg/default.mk
include /usr/share/ada/debian_packaging*.mk

DEB_DATE := $(shell dpkg-parsechangelog -S date)

ADAFLAGS += -gnatfno -gnatwa -gnatVa

LIB_NAME := $(patsubst lib%,%,$(DEB_SOURCE))

SOVERSION := $(shell sed -n -e "s/^Package: lib$(LIB_NAME)\([[:digit:]]\+\)$$/\1/p" debian/control)
ifndef SOVERSION
  $(warning Could not guess SOVERSION from debian/control)
  # Not error. Policy defines targets that may be run from outside build dir.
endif

######################################################################
POLICY_TARGETS := binary binary-arch binary-indep build build-arch \
  build-indep clean
.PHONY: $(POLICY_TARGETS)
$(POLICY_TARGETS):
	dh $@ --with ada-library

.PHONY: override_dh_auto_clean

######################################################################
PROJECT := debian/$(LIB_NAME).gpr

.PHONY: override_dh_auto_build-arch
override_dh_auto_build-arch:
	gprbuild $(BUILDER_OPTIONS) $(PROJECT) -XKIND=static \
          $(foreach var,ADAFLAGS,-X$(var)="$($(var))")
 # Ensure deterministic timestamps in ALI files.
	find . -depth -name "*.ad[bs]" -a -newermt '$(DEB_DATE)' -print0 | \
	  xargs -0r touch --no-dereference --date='$(DEB_DATE)'
	gprbuild $(BUILDER_OPTIONS) $(PROJECT) -XKIND=dynamic \
          $(foreach var,ADAFLAGS LDFLAGS SOVERSION,-X$(var)="$($(var))")

override_dh_auto_clean::
	rm -f $(foreach k, dynamic static \
             ,$(foreach d, lib obj \
             ,debian/$(LIB_NAME)-$(d)-$(k)/*))

######################################################################
# Convert wisi user guide to html and txt, so the "docs" file in this
# directory installs it with hand-written documentations.

WISI_UG := Docs/wisi-user_guide

.PHONY: override_dh_auto_build-indep
override_dh_auto_build-indep: $(WISI_UG).html $(WISI_UG).txt
$(WISI_UG).html: %.html: %.texinfo
	texi2any --html --no-split $< -o $@
$(WISI_UG).txt: %.txt: %.texinfo
	texi2any --plaintext $< -o $@
override_dh_auto_clean::
	rm -f $(WISI_UG).html $(WISI_UG).txt

######################################################################

.PHONY: override_dh_compress
override_dh_compress:
	dh_compress --package=libopentoken-doc --exclude=-dev/examples/
	dh_compress --remaining-packages

######################################################################
.PHONY: override_dh_fixperms-arch override_dh_fixperms-indep
override_dh_fixperms-arch:
	dh_fixperms --arch
override_dh_fixperms-indep:
	dh_fixperms --indep
	find debian/libopentoken-doc/usr/share/doc/libopentoken-doc/examples \
          -type f -print0 | xargs -0 chmod 644
