#!/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
# TODO: remove, document or replace with command line arg:
export SHELL=/bin/bash

ADAFLAGS += -gnatVa -gnatafno -gnatwa
CFLAGS += -w

soversion := $(shell sed -n -r 's/^Package: libflorist([^-]*)$$/\1/p' debian/control)

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

# Ignore install target in upstream Makefile.
override_dh_auto_install:

libflorist.a:
	: # Build the static library as per the upstream Makefile
	$(MAKE) \
          GCCFLAGS="$(CPPFLAGS) $(CFLAGS) $(LDFLAGS)" \
          GNATMAKE="gnatmake $(BUILDER_OPTIONS) '-XADAFLAGS=$(ADAFLAGS)'"
	rm *.o *.ali
	mv floristlib/libflorist.a $@
	mv floristlib obj-static

libflorist.so.$(soversion): CFLAGS += -fPIC
libflorist.so.$(soversion): ADAFLAGS += -fPIC
libflorist.so.$(soversion):
	: # Build the shared library
	$(MAKE) \
          GCCFLAGS="$(CPPFLAGS) $(CFLAGS) $(LDFLAGS)" \
          GNATMAKE="gnatmake $(BUILDER_OPTIONS) '-XADAFLAGS=$(ADAFLAGS)'"
	rm floristlib/libflorist.a deps.o *.ali
	mv *.o floristlib
	mv floristlib obj-shared
	gnatgcc -shared obj-shared/*.o \
		-o libflorist.so.$(soversion) \
		-Wl,--soname,libflorist.so.$(soversion) \
                $(LDFLAGS) \
		-lgnat -lgnarl -lrt -lpthread

override_dh_auto_build-arch: libflorist.a libflorist.so.$(soversion)

override_dh_auto_clean::
	rm -rf obj-static obj-shared
	if [ -f Makefile ] ; then $(MAKE) distclean; fi
	rm -f config.{h,log,status} gnatprep.config Makefile pconfig.h \
		pconfig.h.in stamp-h c-posix-signals c-posix-signals.log \
		libflorist* debian/florist.gpr

LIB_PKG := libflorist$(soversion)
DEV_PKG := libflorist$(soversion)-dev
SOURCES := \
 ada_*.ads \
 posix*.ad[bs] \
 posix*.[ch] \
 system_*.ads

debian/florist.gpr: debian/florist.gpr.sed
	sed -e s/@DEB_HOST_MULTIARCH@/$(DEB_HOST_MULTIARCH)/g $< > $@

override_dh_install: debian/florist.gpr
	dh_install -p$(LIB_PKG) libflorist.so.$(soversion) usr/lib/$(DEB_HOST_MULTIARCH)
	dh_install -p$(DEV_PKG) libflorist.a usr/lib/$(DEB_HOST_MULTIARCH)
	dh_install -p$(DEV_PKG) $(SOURCES) usr/share/ada/adainclude/florist
	dh_install -p$(DEV_PKG) debian/florist.gpr usr/share/ada/adainclude
	dh_install -p$(DEV_PKG) obj-shared/*.ali usr/lib/$(DEB_HOST_MULTIARCH)/ada/adalib/florist
	dh_install --remaining-packages

override_dh_strip:
	dh_strip -p$(LIB_PKG) --dbg-package=libflorist-dbg
# Do not strip static archive.

override_dh_link:
	dh_link -p$(DEV_PKG) \
		usr/lib/$(DEB_HOST_MULTIARCH)/libflorist.so.$(soversion) \
		usr/lib/$(DEB_HOST_MULTIARCH)/libflorist.so
	dh_link --remaining-packages
