#!/usr/bin/make -f
# debian/rules for adacontrol.
# Copyright (c) 2006-2012 Ludovic Brenta <lbrenta@debian.org>
# Copyright (c) 2014-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 2 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.
#
# The full text of the GNU General Public License is in the file
# /usr/share/common-licenses/GPL on Debian systems.

DEB_BUILD_MAINT_OPTIONS := hardening=+all
include /usr/share/dpkg/default.mk
ifneq (,$(wildcard /usr/bin/gnatgcc)) # Build-Depends-Arch are installed.
  gnat_version := $(shell /usr/bin/gnatgcc --version | sed -n '1s/.* //p')
  gnat_base_version := $(basename $(basename $(gnat_version)))
  include /usr/share/ada/debian_packaging-$(gnat_base_version).mk
  ADAFLAGS += -gnatwa
endif

executables := adactl pfni ptree adactl_fix

%:
	dh $@

.PHONY: $(addprefix override_dh_auto_, \
  configure build-arch build-indep test-arch test-indep install clean)

override_dh_auto_build-arch:
	$(MAKE) build 'CARGS=$(ADAFLAGS)' \
	  'GARGS=$(BUILDER_OPTIONS) "-XLDFLAGS=$(LDFLAGS)"'
override_dh_auto_clean::
  # Rewrite the veryclean target without Build-Depends-Arch.
	rm -f src/*.ali src/*.o $(foreach e,$(executables),$(e) src/$(e).bexch src/b__$(e).ad[bs])

override_dh_auto_build-indep:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	$(MAKE) -C doc
endif
override_dh_auto_clean::
	rm -f \
	  doc/adacontrol_??.html \
	  doc/adacontrol_??.info \
	  doc/adacontrol_??.txt \
	  doc/adacontrol_??.pdf

override_dh_auto_test-arch:
  ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	cd test && ./run.sh
  endif
override_dh_auto_clean::
	rm -fr test/res
	rm -f $(addprefix test/,*.ali *.adt)

override_dh_auto_test-arch: test/ref/tfw_help.txt~
test/ref/tfw_help.txt~:
	sed -i~ \
	  's/^\(ADACTL v\. \).*\( with ASIS .* for GNAT \).*/\1$(DEB_VERSION_UPSTREAM)\2$(gnat_version)/' \
	  test/ref/tfw_help.txt
override_dh_auto_clean::
	if test -f test/ref/tfw_help.txt~; then \
	  mv test/ref/tfw_help.txt~ test/ref/tfw_help.txt; \
	fi

# Adactl cannot open compressed rules.
# PDF files are already internally compressed.
.PHONY: override_dh_compress
override_dh_compress:
	dh_compress --package=adacontrol --exclude=.aru
	dh_compress --package=adacontrol-doc --exclude=.pdf
	dh_compress --remaining-packages
