#!/usr/bin/make -f
# debian/rules for adacontrol.
# Copyright (c) 2006, 2012 Ludovic Brenta <lbrenta@debian.org>
# Copyright (c) 2014-2015 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.

include /usr/share/dpkg/default.mk
include /usr/share/ada/debian_packaging.mk

ADAFLAGS += -gnatwa

BINARIES := adactl pfni ptree

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

.PHONY: override_dh_auto_build
override_dh_auto_build: project $(foreach s,pm ug,$(foreach f,html info pdf txt,doc/obj/adacontrol_$(s).$(f)))

.PHONY: project
project: | src/exe src/obj
	gnatmake $(BUILDER_OPTIONS) -Psrc/build.gpr -cargs $(ADAFLAGS) -largs $(LDFLAGS)

# Faketime allows reproducible builds, see
# https://wiki.debian.org/ReproducibleBuilds/TimestampsInPDFGeneratedByLaTeX
# In addition, this source uses @today so all formats should be tricked.
# Freeze clock so that timestamps do not depend on the time spent to
# generate previous documents.
DEB_DATE_RFC_2822 := $(shell dpkg-parsechangelog -S date)
DEB_DATE_RFC_3339 := $(shell date "+%F %T" -d "$(DEB_DATE_RFC_2822)")
TEXI2ANY          := faketime -f "$(DEB_DATE_RFC_3339)" texi2any

doc/obj/%.html: doc/%.texi | doc/obj
	$(TEXI2ANY) --html --no-split $< --output=$@
doc/obj/%.info: doc/%.texi | doc/obj
	$(TEXI2ANY) --info --no-split $< --output=$@
doc/obj/%.pdf : doc/%.texi | doc/obj
	cd doc/obj && \
	$(TEXI2ANY) --pdf --no-iftex --Xopt --quiet ../$*.texi --output=$*.pdf \
	  --Xopt --command=@afourpaper
doc/obj/%.txt : doc/%.texi | doc/obj
	$(TEXI2ANY) --plaintext $< --output=$@

doc/obj src/exe src/obj:
	mkdir $@

.PHONY: override_dh_auto_test
override_dh_auto_test:
	cd test && sh -Cefu run.sh -q

.PHONY: override_dh_auto_clean
override_dh_auto_clean:
	rm -fr doc/obj test/res
	rm -f $(addprefix src/,b~*.ad[bs] *.ali *.o *.adt $(BINARIES))
	rm -f $(addprefix test/,*.ali *.adt)

.PHONY: override_dh_install
override_dh_install:
	dh_install
	dh_install $(addprefix src/,$(BINARIES)) usr/bin
	dh_install rules/*.aru usr/share/adacontrol
	dh_install GPS/adacontrol.xml /usr/share/gprconfig
	dh_install $(filter-out GPS/adacontrol.xml,$(wildcard GPS/*)) \
	   /usr/share/gps/plug-ins


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