#! /usr/bin/make -f
# =============================================================================
# debian/rules for the GNU Prolog packages:
#   architecture-dependant package (gprolog-doc),
#   architecture-independent package (gprolog).
#
# Written by Salvador Abreu <spa@debian.org> based on the template in
# /usr/share/doc/debhelper/examples/rules.multi, with the following header:
# =============================================================================
#
# Sample debian/rules that uses debhelper. 
# This file is public domain software, originally written by Joey Hess.
#

# -----------------------------------------------------------------------------

SHELL=/bin/sh

PACKAGE = gprolog

LIBDIR = /usr/lib/$(PACKAGE)
BINDIR = /usr/lib/$(PACKAGE)/bin
DOCDIR = /usr/share/doc/$(PACKAGE)-doc

SRCDIR=src
DBGFLAGS=-g -O3 -fomit-frame-pointer -no-pie
BCFLAGS=-funsigned-char
export LDFLAGS=-no-pie

REGS = --enable-regs
# FTBFS workaround (#852008)
ifneq ($(filter $(DEB_TARGET_ARCH), i386),)
  REGS = --disable-regs
endif

# =============================================================================

CPPFLAGS = \
	-DDEBIAN

CONFOPTIONS =								\
	--prefix=/usr							\
	--bindir=$(BINDIR)						\
	--libdir=$(LIBDIR)						\
	--includedir=$(LIBDIR)/include					\
	--with-install-dir=$(LIBDIR)					\
	--with-doc-dir=$(DOCDIR)					\
	--with-html-dir=$(DOCDIR)/gprolog.html				\
	--with-examples-dir=$(DOCDIR)/examples				\
	--without-links							\
	--disable-ebp							\
	$(REGS)								\
	--with-c-flags="$(DBGFLAGS) $(BCFLAGS) -Wall $(CPPFLAGS)"


INSTALLDIRS =								\
	INSTALL_DIR=$(CURDIR)/debian/$(PACKAGE)$(LIBDIR)		\
	INSTALL_BIN=$(CURDIR)/debian/$(PACKAGE)$(BINDIR)		\
	INSTALL_LIB=$(CURDIR)/debian/$(PACKAGE)$(LIBDIR)		\
	INSTALL_INC=$(CURDIR)/debian/$(PACKAGE)$(LIBDIR)/include	\
	DOC_DIR=$(CURDIR)/debian/$(PACKAGE)-doc$(DOCDIR)		\
	HTML_DIR=$(CURDIR)/debian/$(PACKAGE)-doc$(DOCDIR)/gprolog.html	\
	EXAMPLES_DIR=$(CURDIR)/debian/$(PACKAGE)-doc$(DOCDIR)/examples

ROOT = $(shell pwd)
SRCPATH = $(ROOT)/src
SRCXDIRS = TopComp EnginePl Pl2Wam Wam2Ma Ma2Asm Fd2C DevUtils
SRCXPATH := $(foreach dir,$(SRCXDIRS),$(SRCPATH)/$(dir):)
SRCXPATH := $(shell echo $(SRCXPATH) | tr -d ' ')
PATH = $(SRCXPATH):/usr/bin:/bin
PL_PATH = ${CURDIR}/src
CC = gcc

# == build binaries and documentation =========================================

build: build-indep build-arch

stamp-config: config
config:
	dh_testdir
# -----------------------------------------------------------------------------
	( PATH=${PATH};							     \
	  if [ ! -f $(SRCDIR)/Makefile -o				     \
	       ! -f $(SRCDIR)/configure -o				     \
	       $(SRCDIR)/Makefile.in -nt $(SRCDIR)/Makefile ]; then	     \
	    cd $(SRCDIR);						     \
	    [ -f ./configure ] || autoconf;				     \
	    CC=$(CC) CPPFLAGS="${CPPFLAGS}" ./configure $(CONFOPTIONS); fi )
# -----------------------------------------------------------------------------
	touch stamp-config

build-indep: stamp-build-indep
stamp-build-indep: stamp-config
	dh_testdir
# -- commands to configure and build the package ------------------------------
	PATH=${PATH} make -C doc pdf html
# -----------------------------------------------------------------------------
	touch stamp-build-indep

build-arch: stamp-build-arch
stamp-build-arch: stamp-config
#	env > ENV
	dh_testdir
# -- commands to configure and build the package ------------------------------
	## fix up the time stamp on generated files in the Debian diff,
	## which can't be regenerated during build w/o a tool that depends
	## on them!
	if [ -f src/BipsPl/arith_inl.wam ]; then	\
		touch src/BipsPl/arith_inl.wam;	\
	fi
	PATH=${PATH} PL_PATH=${PL_PATH} make -C $(SRCDIR)
# -----------------------------------------------------------------------------
	touch stamp-build-arch


# == clean up everything ======================================================

clean:
clean: stamp-config
	dh_testdir
	dh_testroot
	rm -f stamp-build-arch stamp-build-indep stamp-config
# -- commands to clean up after the build process -----------------------------
	set -e; PATH=${PATH} make -C $(SRCDIR) distclean
# -- now, get rid of some more generated files --------------------------------
	set -e;								\
	  find -name Makefile -o -name configure |			\
	    while read FILE; do						\
	      if [ -f "$$FILE.in" ]; then rm -f "$$FILE"; fi; done
	find src -name VERSION | xargs -r rm -f
	rm -rf $(SRCDIR)/autom4te.cache
	rm -f $(SRCDIR)/EnginePl/gprolog_cst.h
	rm -f $(SRCDIR)/EnginePl/wam_stacks.h
# -- make sure all *.wam files are more recent than *.pl ----------------------
	find $(SRCDIR) -name \*.wam | xargs touch
# -- clean up docs ------------------------------------------------------------
	make -C doc clean-all distclean
# -- clean up examples --------------------------------------------------------
	ls -d examples/Examples* | while read DIR; do	\
	  if [ -f $$DIR/Makefile ]; then		\
	    make -C "$$DIR" clean;			\
	  fi;						\
	done
# -----------------------------------------------------------------------------
	dh_clean


# == arch-dependent installation ==============================================

install-arch:
install-arch: build-arch
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs -A
# -- install the package into the staging area --------------------------------
	make -C $(SRCDIR) $(INSTALLDIRS) install-system
# -- create gprolog etc, links in /usr/bin ------------------------------------
#    (this is now done by gprolog.postinst)
# -- Install manpages ---------------------------------------------------------
	mkdir -p debian/$(PACKAGE)/usr/share/man/man1/
	cat debian/manpage.1 |						 \
	  sed -e "s/@PACKAGE@/$(PACKAGE)/g" |				 \
	  gzip -9 > debian/$(PACKAGE)/usr/share/man/man1/$(PACKAGE).1.gz
	for CMD in gplc hexgplc; do					\
	  ln -sf $(PACKAGE).1.gz					\
		 debian/$(PACKAGE)/usr/share/man/man1/$$CMD.1.gz;	\
	done
# -- Install auxiliary stuff --------------------------------------------------
	mkdir -p debian/$(PACKAGE)/usr/share/icons/
	install -c -m 444						\
		doc/logo.png						\
		debian/$(PACKAGE)/usr/share/icons/$(PACKAGE).png
	mkdir -p debian/$(PACKAGE)/usr/share/doc/$(PACKAGE)/
	install -c -m 444					\
		NEWS README					\
		debian/$(PACKAGE)/usr/share/doc/$(PACKAGE)/
# -----------------------------------------------------------------------------
#	dh_movefiles


# == arch-independent installation ============================================

install-indep:
install-indep: build-indep
	dh_testdir
	dh_testroot
	dh_clean
	dh_installdirs -A
# -- commands to install the package into the staging area --------------------
	make -C $(SRCDIR) $(INSTALLDIRS) \
	  install-doc install-html install-examples
# -- remove stray PDFs --------------------------------------------------------
	find debian/$(PACKAGE)-doc -name compil-scheme.pdf\* | xargs -r rm -f
	find debian/$(PACKAGE)-doc -name debug-box.pdf\* | xargs -r rm -f
# -----------------------------------------------------------------------------
#	dh_movefiles


# == build architecture-independent files =====================================

binary-indep: build-indep install-indep
	dh_testdir -i
	dh_testroot -i
#	dh_installdebconf -i
	dh_installdocs -i # -pgprolog-doc
#	dh_installexamples -i
	dh_installemacsen -i
#	dh_installman -i
	dh_installinfo -i
	dh_installchangelogs -i
	dh_link -i
	dh_compress -i -X.pdf -X.pl
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build-arch install-arch
	dh_testdir -a
	dh_testroot -a
#	dh_installdebconf -a
	dh_installdocs -a
	dh_installexamples -a
#	dh_installmenu -a
	dh_installman -a
#	dh_installinfo -a
	dh_installchangelogs ChangeLog
	dh_link -a
	dh_strip -a
	dh_compress -a -X.pl -X.html
	dh_fixperms -a
#	dh_makeshlibs -a
	for CTLFILE in postinst prerm; do			\
	  ( echo "# -- Prolog variant ---------";		\
	    echo LIB=/usr/lib/gprolog;				\
	    echo "# ---------------------------" )		\
		>> debian/$(PACKAGE).$$CTLFILE.debhelper;	\
	done
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch
.PHONY: build build-arch build-indep clean binary-indep binary-arch binary install
