#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright © 2014 Jonas Smedegaard <dr@jones.dk>
# Description: Main Debian packaging script for Marpa::R2
#
# This program 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, 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/>.

DEB_AUTO_UPDATE_LIBTOOL = pre
DEB_AUTO_UPDATE_ACLOCAL = ,
DEB_AUTO_UPDATE_AUTOCONF = ,
DEB_AUTO_UPDATE_AUTOMAKE = ,
include /usr/share/cdbs/1/rules/upstream-tarball.mk
include /usr/share/cdbs/1/rules/utils.mk
include /usr/share/cdbs/1/class/autotools.mk
include /usr/share/cdbs/1/class/perl-build.mk
include /usr/share/cdbs/1/rules/debhelper.mk

pkg = $(DEB_SOURCE_PACKAGE)

DEB_UPSTREAM_PACKAGE = Marpa-R2
DEB_UPSTREAM_URL = http://www.cpan.org/CPAN/authors/id/J/JK/JKEGL
DEB_UPSTREAM_TARBALL_MD5 = 1fe355bb35ddb679cc8553a71ae5e678

DEB_UPSTREAM_REPACKAGE_EXCLUDES += \
 ./t/etc/wall_proof.txt

# needed by upstream build
bdeps = libcpan-meta-perl
bdeps +=, libconfig-autoconf-perl
bdeps +=, libextutils-cbuilder-perl
bdeps +=, libmodule-build-perl

# needed by upstream build and (always) at runtime
deps +=, perl
deps +=, libhtml-parser-perl

# Needed (always/optionally) by upstream testsuite
deps-test = libtest-simple-perl
deps-test-opt = libppi-perl

CDBS_BUILD_DEPENDS +=, $(bdeps), $(deps), $(deps-test), $(deps-test-opt)
CDBS_DEPENDS_$(pkg) = $(deps)

# put aside upstream-shipped autogenerated files and (re)generate during build
basedir = libmarpa_dist
makefiledirs = .
files_autoconf_modern = $(auxdir)/compile
DEB_UPSTREAM_CRUFT_MOVE = $(filter-out $(files_autoconf_modern),\
	$(files_autotools))
DEB_UPSTREAM_CRUFT_COPY = libmarpa_dist/LIB_VERSION
makefile-clean::
	rm -f $(files_autoconf_modern)
# TODO: move below to CDBS autotools.mk snippet (if indeed generic)
auxdir = $(basedir)
macrodir = $(basedir)/m4
autom4tedir = $(basedir)/autom4te.cache
files_autotools_core = $(auxdir)/config.guess $(auxdir)/config.sub
stems_m4_libtool = libtool ltoptions ltsugar ltversion lt~obsolete
files_libtool = $(auxdir)/ltmain.sh $(stems_m4_libtool:%=$(macrodir)/%.m4)
files_aclocal = $(basedir)/aclocal.m4
stems_aux_autoconf = ar-lib compile install-sh missing
files_autoconf = $(basedir)/configure $(stems_aux_autoconf:%=$(auxdir)/%)
files_automake = $(auxdir)/depcomp $(makefiledirs:%=$(basedir)/%/Makefile.in)
files_autotools = $(files_autotools_core) $(files_libtool) $(files_aclocal)
files_autotools += $(files_autoconf) $(files_automake)
files_configure = $(DEB_BUILDDIR)/config.log
files_make = $(makefiledirs:%=$(DEB_BUILDDIR)/%/Makefile)
DEB_ACLOCAL_ARGS = -Im4 --install --force
DEB_AUTOMAKE_ARGS = --add-missing --copy --foreign --force
DEB_MAKE_CLEAN_TARGET = distclean
makefile-clean::
	rm -rf $(dirs_autotools)
	rm -f $(filter-out $(DEB_UPSTREAM_CRUFT_MOVE),\
		$(files_autotools) $(files_configure) $(files_make))
clean::
	rm -f $(filter-out $(DEB_UPSTREAM_CRUFT_MOVE),\
		$(files_autotools_core))
	test ! -d $(auxdir) || rmdir --ignore-fail-on-non-empty $(auxdir)

# Use custom sourcedir for autotools (re)generation and make build
DEB_SRCDIR = $(basedir)
DEB_PERL_SRCDIR = .
DEB_BUILDDIR = .

DEB_CONFIGURE_EXTRA_FLAGS = --with-pic --disable-shared

# install demos and their pod files only as example files
perlpath := $(shell perl -MConfig -e 'print $$Config{vendorarch};' )
binary-post-install/$(pkg)::
	mkdir -p debian/$(pkg)/usr/share/doc/$(pkg)/examples
	mv -t debian/$(pkg)/usr/share/doc/$(pkg)/examples \
		debian/$(pkg)/usr/bin/marpa_r2_html_* \
		debian/$(pkg)/$(perlpath)/Marpa/R2/html_* \
		debian/$(pkg)/usr/share/man/man3/Marpa::R2::html_*

# avoid headers or library files getting installed
binary-post-install/$(pkg)::
	rm -rf debian/$(pkg)/usr/lib/lib* debian/$(pkg)/usr/include

binary-fixup/$(pkg)::
	chmod -x debian/$(pkg)/usr/share/doc/$(pkg)/examples/*.pod*

# cleanup left behind custom avoid-changing-automade-files tracking file
clean::
	rm -f lib/Marpa/general_pattern.xsh
