#!/usr/bin/make -f
# Build script for Templates_Parser in Debian.
# Copyright (c) 2003-2008 Ludovic Brenta <lbrenta@debian.org>
# Copyright (c) 2013-2016 Nicolas Boulenguez <nicolas@debian.org>

# This build script 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/>.

# On Debian systems, the full text of the GPL is in the file
# /usr/share/common-licenses/GPL-3.

include /usr/share/dpkg/default.mk
# gnat-4.9 provides /usr/share/ada/debian_packaging.mk
# gnat-5 provides /usr/share/ada/debian_packaging-5.mk
include /usr/share/ada/debian_packaging*.mk

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

SOVERSION := $(shell sed --quiet -r 's/^Package: $(DEB_SOURCE)([0-9.]+)$$/\1/p' debian/control)

# Some of these options are stored into makefile.setup, but not all.
MAKE_OPTS := \
  VERSION=$(SOVERSION) \
  HOST=$(DEB_HOST_GNU_TYPE) \
  TARGET=$(DEB_TARGET_GNU_TYPE) \
  prefix=/usr \
  DEFAULT_LIBRARY_TYPE=relocatable \
  ENABLE_SHARED=true \
  TP_XMLADA=Installed \
  GPRBUILD="gnatmake $(BUILDER_OPTIONS) $(foreach v,ADAFLAGS LDFLAGS,'-X$(v)=$($(v))')" \
  GPRCLEAN=gnatclean \
  DOC_FORMATS="html info latexpdf text"

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

# Avoid DebHelper default calls to usual Make targets.
.PHONY: $(addprefix override_dh_auto_, \
  configure build-arch build-indep test install clean)

override_dh_auto_configure: src/templates_parser.ads.backup
 # Before any build, change the AdaCore version (README.source).
sed_script_version := \
 s/^\( \{3\}Version : constant String := "\).*\(";\)$$/\1$(DEB_VERSION_UPSTREAM)\2/
 # Three consecutive spaces would be reduced by Make.
src/templates_parser.ads.backup: %.backup:
 # No prerequisite, else Make will attempt to update.
	mv $* $@
	sed -e '$(sed_script_version)' $@ > $*
 # Ensure deterministic timestamps in ALI files.
	touch $* --date='$(DEB_DATE)'
override_dh_auto_clean::
	if test -e src/templates_parser.ads.backup; then \
	  mv src/templates_parser.ads.backup src/templates_parser.ads; \
	fi

override_dh_auto_build-arch:
	$(MAKE) $(MAKE_OPTS)

# Freeze the clock for deterministic PDF timestamps. The faketime API
# does not allow an explicit time zone, introducing an implicit
# dependency on its local value.
DEB_DATE_FAKETIME := $(shell date "+%F %T" -d "$(DEB_DATE)")
override_dh_auto_build-indep:
	faketime -f "$(DEB_DATE_FAKETIME)" \
	  $(MAKE) setup build-doc $(MAKE_OPTS)

override_dh_auto_clean::
 # Avoid complaining about non existing dirs.
	mkdir -p .build/bin .build/lib .build/obj .build/tobj
	$(MAKE) setup clean $(MAKE_OPTS)

######################################################################
.PHONY: override_dh_ada_library-arch
override_dh_ada_library-arch:
 # There is no --subdirs option for dh_ada_library.
	mv .build/lib/*/release/relocatable/* .build/lib
	mv .build/obj/*/release/relocatable/* .build/obj
	dh_ada_library

.PHONY: override_dh_compress
override_dh_compress:
	dh_compress --package=libtemplates-parser-doc --exclude=examples/templates.tads
	dh_compress --remaining-packages

######################################################################
# Policy specifies that get-orig-source may be run outside the source
# tree, so we rely on a temporary directory and hard-code the version.

orig_version    := 11.8.2014
orig_archive    := libtemplates-parser_$(orig_version).orig.tar.xz
orig_tmp_dir    := /tmp/libtemplates-parser_get_orig_source_tmp_dir
# Set this to your own database on the command line if you already
# have a local clone of the upstream branch.
# By default, a temporary database is populated.
orig_mtn_db     := $(orig_tmp_dir)/tmpdb
orig_server     := www.ada-france.org
orig_server_key := f8a11727e8983cf9083c08c6a2acaa27e439dd39
orig_branch     := com.adacore.templates_parser.debian
orig_revision   := t:libtemplates-parser-$(orig_version)
orig_directory  := libtemplates-parser-$(orig_version).orig

.PHONY: get-orig-source
get-orig-source:
	test ! -e $(orig_archive) # Stop if the archive exists.
	mkdir $(orig_tmp_dir) # Stop if the temp dir exists.
	@echo
	@echo The server key below should be $(orig_server_key).
	@echo
	mtn clone --db=$(orig_mtn_db) mtn://$(orig_server)?$(orig_branch) \
          --branch=$(orig_branch) --revision=$(orig_revision) \
          $(orig_tmp_dir)/$(orig_directory)
	tar -c -J -f $(orig_archive) -C $(orig_tmp_dir) $(orig_directory) \
          --exclude=_MTN --exclude=.mtn-ignore
	rm -f -r $(orig_tmp_dir)
	@echo
	@echo Created $(orig_archive) in the current directory.
