#!/usr/bin/make -f

# Copyright (c) 2003-2014 Ludovic Brenta <lbrenta@debian.org>
# Copyright (c) 2014      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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
# USA

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

######################
# Set some variables #
######################

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

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

ADAFLAGS += -gnatVa -gnatafno -gnatwa
CFLAGS += -w
LDLIBS := -lrt -lpthread

soversion := $(shell sed -n -e "s/^Package: libflorist\([[:digit:]]\+\)$$/\1/p" debian/control)
ifndef soversion
  $(warning Could not guess soversion from debian/control)
  # Not error. Policy defines targets that may be run from outside build dir.
endif

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

override_dh_auto_configure-arch:
	mv -n configure configure.backup
	autoconf
	dh_auto_configure
override_dh_auto_clean::
	if test -f configure.backup; then mv configure.backup configure; fi

# Ignore install target in upstream Makefile.
override_dh_auto_install:

lib-shared obj-shared:
	mkdir $@

# Compile macros with -fPIC, store them where gnatmake will see them.
MACRO_SRC := posix-macros posix-macros-sockets
MACRO_OBJ := $(foreach m,$(MACRO_SRC),obj-shared/$(m).o)
$(MACRO_OBJ): obj-shared/%.o: %.c pconfig.h | obj-shared
	$(CC) $(CFLAGS) -fPIC $(CPPFLAGS) $(OUTPUT_OPTION) -c $<

override_dh_auto_build-arch: $(MACRO_OBJ) | lib-shared obj-shared

	# Build the static library as per the upstream Makefile
	$(MAKE) \
          GCCFLAGS="$(CPPFLAGS) $(CFLAGS) $(LDFLAGS)" \
          GNATMAKE="gnatmake $(BUILDER_OPTIONS) '-XADAFLAGS=$(ADAFLAGS)'"

	# Build the shared library
	gnatmake $(BUILDER_OPTIONS) -P debian/florist.gpr $(foreach v, \
          ADAFLAGS LDFLAGS LDLIBS soversion \
          ,"-X$(v)=$($(v))")

override_dh_auto_clean::
	rm -rf lib-shared obj-shared
 # Run upstream clean target.
	dh_auto_clean

override_dh_ada_library:
	dh_ada_library $(foreach v, \
          LDLIBS soversion \
          ,"$(v)=$($(v))") debian/florist.gpr
