#!/usr/bin/make -f
# Build script for Ada Web Server in Debian.
# Copyright (c) 2003-2013 Ludovic Brenta <lbrenta@debian.org>
# Copyright (c) 2014-2020 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.

SSL := dummy
$(foreach line,$(shell sed -n '\
  s/^ gnat, gnat-\([0-9.]\+\),$$/ \
    GNAT_VERSION:=\1 \
  /p;\
  s/^Package: libaws\([0-9.]\+\)$$/ \
    soname:=libaws.so.\1 \
  /p;\
  s/^ libgnutls[0-9.]*-dev,$$/ \
    SSL:=gnutls \
  /p;\
  s/^ libssl-dev,$$/ \
    SSL:=openssl \
  /p;\
  ' debian/control),$(eval $(line)))

DEB_BUILD_MAINT_OPTIONS := hardening=+all
DEB_ADAFLAGS_MAINT_APPEND := -gnatafno -gnatVa -gnatwa
DEB_CPPFLAGS_MAINT_APPEND := `pkg-config --cflags $(filter-out dummy,$(SSL)) zlib`
DEB_LDFLAGS_MAINT_APPEND := -Wl,--no-undefined \
  -Wl,--no-copy-dt-needed-entries -Wl,--no-allow-shlib-undefined
include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/buildflags.mk
include /usr/share/ada/debian_packaging-$(GNAT_VERSION).mk

LDLIBS := `pkg-config --libs   $(filter-out dummy,$(SSL)) zlib` -llber -lldap

# Circumvent #760211. Since LDFLAGS contains --as-needed, this setting
# only takes effect on architectures triggering the bug.
LDLIBS += -lpthread

######################################################################
%:
	dh $@

# Ignore upstream makefile (and ./configure, if ever).
.PHONY: $(addprefix override_dh_auto_,configure build-arch build-indep test install clean)
# Configure test install exist and are empty so that dh_auto_* does
# nothing.  We will replace upstream makefiles with build-* and clean.

######################################################################
# See comments in aws-os_lib-tmplt.c.
build-aws-dynamic build-aws-static: src/core/aws-os_lib.ads
# Tell Make that the source must be created but never updated, even if
# its timestamp is tricked later for deterministic ALI files.
src/core/aws-os_lib.ads:|src/core/xoscons \
                         src/core/aws-os_lib-tmplt.i \
                         src/core/aws-os_lib-tmplt.s
	cd src/core && ./xoscons aws-os_lib
src/core/xoscons: config/setup/xoscons.adb
	gnatmake $(BUILDER_OPTIONS) $< -D src/core -o $@ -cargs $(ADAFLAGS) -largs $(LDFLAGS)
src/core/aws-os_lib-tmplt.s: %.s: %.i
	$(CC) -S $(CFLAGS) $< -o $@
src/core/aws-os_lib-tmplt.i: config/setup/aws-os_lib-tmplt.c
	$(CC) -C -E $(CPPFLAGS) -DTARGET=\"$(DEB_HOST_MULTIARCH)\" $< -o $@

override_dh_auto_clean::
	rm -f src/core/aws-os_lib* src/core/xoscons* src/core/xutil.*

######################################################################
# Build both versions of the library.

override_dh_auto_build-arch: build-aws-static build-tools
.PHONY: build-aws-dynamic build-tools
build-aws-static:
	gprbuild -p $(BUILDER_OPTIONS) debian/build_aws.gpr \
	  $(foreach v,ADAFLAGS CFLAGS CPPFLAGS SSL,"-X$(v)=$($(v))")
build-tools:
	gprbuild -p $(BUILDER_OPTIONS) debian/tools.gpr \
	  $(foreach v,ADAFLAGS CFLAGS CPPFLAGS LDFLAGS LDLIBS soname SSL,"-X$(v)=$($(v))")
override_dh_auto_clean::
	rm -fr $(foreach d,lib obj,$(foreach k,dynamic static, \
          debian/build_aws_$(d)_$(k))) debian/tools_obj debian/tools_exe

# An override is necessary because LDLIBS contains a shell escape.
# The projet should import directly xmlada.gpr and
# templates-parser.gpr (both are used by specs).
.PHONY: override_dh_ada_library-arch
override_dh_ada_library-arch:
	dh_ada_library \
          $(foreach v,LDLIBS soname SSL,"$(v)=$($(v))") \
          debian/build_aws.gpr

######################################################################
INSTALLED_EXAMPLES := debian/libaws-doc/usr/share/doc/libaws-doc/examples
check_link = \
  diff -q debian/libaws-doc/usr/share/doc/libaws-doc/examples/$(1) \
          debian/libaws-doc/usr/share/doc/libaws-doc/examples/$(2) \
  && dh_link -plibaws-doc   usr/share/doc/libaws-doc/examples/$(1) \
                            usr/share/doc/libaws-doc/examples/$(2)
.PHONY: override_dh_installexamples-indep
override_dh_installexamples-indep:
  # An explicit main package silents a warning.
	dh_installexamples --package=libaws-doc --doc-main-package=libaws-doc

 # Fix permissions.
	find $(INSTALLED_EXAMPLES) -type f -print0 | xargs -0r chmod 644

 # Replace duplicate files with symlinks.
 # (rdfind and symlinks are confused on pbuilder with /build->/tmp/build)
 # lintian detects things to add, diff detects thing to remove.
	$(call check_link,runme/adains.png,res_demo/adains.png)
	$(call check_link,runme/cert.pem,agent/cert.pem)
	$(call check_link,soap_disp/soap_client.adb,soap_demo/soap_client.adb)
	$(call check_link,upload/aws.ini,https/aws.ini)
	$(call check_link,web_block_ajax_templates/r_widget_counter.txml,web_block_ajax/r_widget_counter.txml)
	$(call check_link,web_block_ajax_templates/web_callbacks.ads,web_block_ajax/web_callbacks.ads)
	$(call check_link,ws/page1.html,web_elements/page1.html)
	$(call check_link,ws/ws.ini,wps/wps.ini)

######################################################################
# Documentation.
# ./makefile would recompute all makefile.conf variables, assume only
# HTML output format, and assume the existence of templates_parser/.
override_dh_auto_build-indep:
ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	$(MAKE) -C docs html info latexpdf text style-guide.pdf \
	  SPHINXOPTS=-j$(BUILDER_JOBS) \
	  $(foreach v,ADAFLAGS LDFLAGS,"$(v)=$($(v))")
endif

override_dh_auto_clean::
	$(MAKE) -C docs clean
	rm -f docs/source/*.pyc docs/source/confvars.py
	rm -fr docs/source/__pycache__

######################################################################
.PHONY: override_dh_installdocs
override_dh_installdocs:
  # An explicit main package silents a warning.
	dh_installdocs --package=libaws-doc --doc-main-package=libaws-doc
	dh_installdocs --remaining-packages
