#!/usr/bin/make -f
#
# Debian Makefile for OpenToken
# Copyright (c) 2009-2010 Stephen Leake <stephen_leake@stephe-leake.org>
# Copyright (c) 2013-2017 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/>.
#

define new_line :=


endef
$(eval $(addsuffix $(new_line),$(shell sed -n '\
  s/^ gnat, gnat-\([0-9.]\+\),$$/ \
    GNAT_VERSION:=\1 \
  /p;\
  s/^Package: lib[a-z-]\+\([0-9.]\+\)$$/ \
    SOVERSION:=\1 \
  /p;\
  ' debian/control)))

DEB_BUILD_MAINT_OPTIONS := hardening=+all
include /usr/share/dpkg/default.mk
include /usr/share/ada/debian_packaging-$(GNAT_VERSION).mk

ADAFLAGS += -gnatfno -gnatwa -gnatVa

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

%:
	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))")
	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
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
override_dh_auto_build-indep: $(WISI_UG).html $(WISI_UG).txt
endif
$(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
