#!/usr/bin/make -f

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

# Copyright (c) 2003-2009 Ludovic Brenta <lbrenta@debian.org>
# Copyright (c) 2009-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 #
######################

DPKG_EXPORT_BUILDFLAGS=True
include /usr/share/dpkg/default.mk
include /usr/share/ada/debian_packaging.mk

LIB_NAME := $(patsubst lib%,%,$(DEB_SOURCE))

SOVERSION := $(shell sed -n -e "s/^Package: lib$(LIB_NAME)\([[:digit:]]\+\)$$/\1/p" debian/control)
ifndef SOVERSION
  $(warning Could not guess SOVERSION from debian/control)
endif
LIB_PKG := lib$(LIB_NAME)$(SOVERSION)

DEV_PKG := $(shell sed -n -e "s/^Package: \(lib$(LIB_NAME)[[:digit:]]\+-dev\)$$/\1/p" debian/control)
ifndef DEV_PKG
  $(warning Could not guess DEV_PKG from debian/control)
endif

######################
# debhelper overrides
######################

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

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

######################################################################
# Upstream uses its own autoreconf, packaged in Debian.
override_dh_autoreconf-indep:
override_dh_autoreconf-arch:
	dh_autoreconf -a autoreconf-dickey -- --force --install

override_dh_auto_configure-indep:
override_dh_auto_configure-arch:
	dh_auto_configure -a -- CC=gnatgcc

LDLIBS := `pkg-config --libs form menu panel ncurses`
PROJECT := debian/$(LIB_NAME).gpr
gnatmake = gnatmake -P$(PROJECT) $(BUILDER_OPTIONS) \
  -XKIND=$(1) $(foreach var,$(2),-X$(var)="$($(var))")
override_dh_auto_build-indep:
override_dh_auto_build-arch:
	dh_auto_build -a -- -C include
	dh_auto_build -a -- -C gen

	# The src/ build system would need lots of patching to respect
	# our flags and build two versions of the library, so we only
	# call it to generate a needed file.
	dh_auto_build -a -- -C src terminal_interface-curses-trace.adb
	$(call gnatmake,static,ADAFLAGS CFLAGS CPPFLAGS)
	$(call gnatmake,dynamic,ADAFLAGS CFLAGS CPPFLAGS LDFLAGS LDLIBS SOVERSION)

############################################################
# See comments in PROJECT.
C_SRC := $(wildcard src/*.c)

C_OBJ_DYNAMIC := $(patsubst src/%.c,debian/build-obj-dynamic/%.o,$(C_SRC))
override_dh_auto_build-arch: $(C_OBJ_DYNAMIC)
$(C_OBJ_DYNAMIC): debian/build-obj-dynamic/%.o: src/%.c
	$(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@ -Isrc -fPIC

C_OBJ_STATIC  := $(patsubst src/%.c,debian/build-obj-static/%.o, $(C_SRC))
override_dh_auto_build-arch: $(C_OBJ_STATIC)
$(C_OBJ_STATIC): debian/build-obj-static/%.o: src/%.c
	$(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@ -Isrc

	# doc/ provides a -config script giving build flags for
	# reverse dependencies. We ignore it as per Debian Ada Policy
	# we prefer project files. It also contains HTML pages
	# generated from the sources (make -C gen html). We do not
	# regenerate them either because we do not want to install
	# arch-dependant files into the -doc package. The
	# arch-specific parts are identified in the comments anyway.

	# samples/* will be installed into the -doc package, and used
	# by autopkgtest, but there is no point in compiling them now.

override_dh_auto_clean::
	dh_auto_clean
	rm -f debian/build-lib-dynamic/* \
              debian/build-obj-dynamic/* \
              debian/build-lib-static/* \
              debian/build-obj-static/*

override_dh_ada_library:
	dh_ada_library KIND=dynamic SOVERSION=ignored LDLIBS="$(LDLIBS)" $(PROJECT)

######################################################################

override_dh_auto_install: # Ignore upstream install target.

override_dh_installchangelogs:
	dh_installchangelogs --all NEWS

# samples/explain.txt is read by samples/sample-explanation.adb.
override_dh_compress:
	dh_compress --package=$(DEV_PKG) \
          --exclude=.ads \
          --exclude=.adb
	dh_compress --package=lib$(LIB_NAME)-doc \
          --exclude=.ads \
          --exclude=.adb \
          --exclude=explain.txt
	dh_compress --remaining-packages

######################################################################

GET_ORIG_FTP := ftp://invisible-island.net/AdaCurses/current
GET_ORIG_TGZ := AdaCurses.tgz

get-orig-source:
	wget $(GET_ORIG_FTP)/$(GET_ORIG_TGZ)
	@echo "The file $(GET_ORIG_TGZ) contains the latest snapshot of upstream sources."
