#!/usr/bin/make -f

##############
# Legal stuff
##############

# Copyright (c) 2003-2006, 2011-2014 Ludovic Brenta <lbrenta@debian.org>
# Copyright (c) 2010-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.

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

.SUFFIXES:

########################
# Guess some variables.
########################

DEB_BUILD_MAINT_OPTIONS := hardening=+all
include /usr/share/dpkg/default.mk
# Set CFLAGS (handling noopt from DEB_BUILD_OPTIONS), LDFLAGS,
# DEB_HOST_GNU_TYPE, DEB_HOST_ARCH, DEB_VERSION...

include /usr/share/ada/debian_packaging*.mk

PKG_CONFIG_LIBRARIES := gtk+-3.0 glib-2.0

DEB_DATE_RFC_2822 := $(shell dpkg-parsechangelog -S date)
DEB_DATE_RFC_3339 := $(shell date "+%F %T" -d "$(DEB_DATE_RFC_2822)")

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

# Do not try to call an upstream configure or Makefile.
.PHONY: $(addprefix override_dh_auto_, \
  clean configure build-arch build-indep test install)

######################################################################
# See gnat-gps-doc.TODO and the share/* part of gnat-gps-common.install.
# distrib/gnat_switches.awk processes the non DFSG gnat_ug.texi.
GNAT_SWITCHES_PY := share/support/core/gnat_switches.py
override_dh_auto_build-indep: $(GNAT_SWITCHES_PY)
$(GNAT_SWITCHES_PY):
	touch $@
override_dh_auto_clean::
	rm -f $(GNAT_SWITCHES_PY)

GNAT_RUNTIME_PLUG_IN := share/support/core/runtime.py
override_dh_auto_build-indep: $(GNAT_RUNTIME_PLUG_IN)
share/support/core/runtime.py:
	cd distrib && perl from_impunit.pl
override_dh_auto_clean::
	rm -f  $(GNAT_RUNTIME_PLUG_IN)

LIGHT_ICONS := share/icons/light/svg
override_dh_auto_build-indep: $(LIGHT_ICONS)
$(LIGHT_ICONS):
	mkdir $@
	cd distrib && python iconsets.py
override_dh_auto_clean::
	rm -fr $(LIGHT_ICONS)

######################################################################
override_dh_auto_build-arch:
	gprbuild $(BUILDER_OPTIONS) -k -Pdebian/build_gps.gpr \
	  $(foreach v,ADAFLAGS CFLAGS CPPFLAGS LDFLAGS,"-X$(v)=$($(v))") \
	  "-XPKG_CONFIG_CFLAGS=`pkg-config --cflags $(PKG_CONFIG_LIBRARIES)`" \
	  "-XPKG_CONFIG_LIBS=`pkg-config --libs $(PKG_CONFIG_LIBRARIES)`"
override_dh_auto_clean::
	rm -f obj/*

GPR_CONFIGURATION := default.cgpr
override_dh_auto_build-arch: $(GPR_CONFIGURATION)
$(GPR_CONFIGURATION):
	gprconfig --batch --config=Ada --config=C,,,,gnatgcc
override_dh_auto_clean::
	rm -f $(GPR_CONFIGURATION)

CONFIG_ADS := common/core/src/config.ads
override_dh_auto_build-arch: $(CONFIG_ADS)
$(CONFIG_ADS): %: %.in
	sed -e 's/@CURRENT_YEAR@/2015/' \
	    -e 's/@GPS_VERSION@/$(DEB_VERSION_UPSTREAM)/' \
	    -e 's/@TARGET@/$(DEB_HOST_GNU_TYPE))/' \
	    -e 's/@TARGET_IS_DARWIN@/False/' \
	    -e 's!@prefix@!/usr!' \
	    -e 's/@HOST_TYPE@/Unix/' \
	    -e 's/@STRIP_CR@/False/' \
	    -e 's/@CAN_OUTPUT@/True/' \
	    -e 's!@EXEC_COMMAND@!/bin/sh -c!' \
	    -e 's/@DEFAULT_FONT@/Sans 9/' \
	    -e 's/@DEFAULT_FIXED_FONT@/Mono 9/' \
	    -e 's/@DEFAULT_PS@/ps/' \
	    -e 's/@PRINT_CMD@/lpr/' \
	    -e 's/@OPAQUE_MDI@/True/' \
	    -e 's/@DIFF_CMD@/diff -u/' \
	    -e 's/@DIFF3_CMD@/diff3/' \
	    -e 's/@PATCH_CMD@/patch/' \
	    -e 's/@EXECUTION_WINDOW@/True/' \
	    -e 's/@CHARSET@/UTF-8/' $< > $@
override_dh_auto_clean::
	rm -f $(CONFIG_ADS)

######################################################################
DOCS := \
 docs/users_guide/_build \
 docs/tutorial/_build \
 docs/programmers_guide/_build \
 docs/release_notes/build \
 gnatdoc/docs/users_guide/_build
override_dh_auto_build-indep: $(DOCS)
$(DOCS):
 # Freeze the clock for deterministic PDF timestamps.
	faketime -f "$(DEB_DATE_RFC_3339)" \
	$(MAKE) -C $(dir $@) SPHINXOPTS=-j$(BUILDER_JOBS) html latexpdf
	# Creates Python precompiled files in every imported module,
	# we do not want them installed.
	find . -name "*.pyc" -delete
override_dh_auto_clean::
	rm -fr $(DOCS)
	find . -name "*.pyc" -delete

######################################################################
# Replacing documentation directories with symbolic links would spare
# 10Kb but creates too much maintainance, see #684194 and #655383.
# From 4.3-1 to 5.0-12, every binary package has created at least a
# link, that dpkg may never have removed since (policy 6.6.4).
# Generated preinst scripts will remove them.
PREINST_ARCH  := debian/gnat-gps.preinst
PREINST_INDEP := debian/gnat-gps-common.preinst \
                 debian/gnat-gps-doc.preinst
binary-arch: $(PREINST_ARCH)
binary-indep: $(PREINST_INDEP)
clean: clean_preinst
$(PREINST_ARCH) $(PREINST_INDEP): debian/%.preinst: debian/rm_doc_symlink.preinst.sed
	sed s/PKGNAME/$*/ $< > $@
.PHONY: clean_preinst
clean_preinst:
	rm -f $(PREINST_ARCH) $(PREINST_INDEP)

######################################################################
# Mimic dh --with sphinxdoc with sphinx-common in Build-Depends-Indep.
.PHONY: override_dh_installdocs-indep
override_dh_installdocs-indep:
	dh_installdocs -i
	dh_sphinxdoc -i

# Mimic dh --with linktree, with dh-linktree in Build-Depends-Indep.
.PHONY: override_dh_link-indep
override_dh_link-indep:
	dh_link -i
	dh_linktree -i

.PHONY: override_dh_compress
override_dh_compress:
	dh_compress --all -X.xml -X.ads -X.adb -X.c -X.py
