#! /usr/bin/make -f
# $Id: rules,v 1.7 2002/05/04 22:30:36 spa Exp $
# =============================================================================
# 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.
#

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

# This is the debhelper compatibility version to use.
export DH_COMPAT=3

variant=
package=gprolog$(variant)
SRCDIR=src/src

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

DOCDIR = /usr/share/doc/$(package)-doc

CPPFLAGS = \
	-DDEBIAN

CONFOPTIONS = \
	--prefix=/usr \
	--bindir=/usr/lib/$(package)/bin \
	--libdir=/usr/lib/$(package) \
	--includedir=/usr/include/$(package) \
	--with-doc-dir=$(DOCDIR) \
	--with-html-dir=$(DOCDIR)/gprolog.html \
	--with-examples-dir=$(DOCDIR)/examples \
	--with-c-flags="-O3 -fomit-frame-pointer -Wall -DDEBIAN" \
	--without-links \
	--enable-watermark

INSTALLDIRS = \
	  INSTALL_DIR=debian/$(package)/usr \
	  PROLOG_BINDIR=debian/$(package)/usr/lib/$(package)/bin \
	  PROLOG_LIBDIR=debian/$(package)/usr/lib/$(package) \
	  PROLOG_INCDIR=debian/$(package)/usr/include/$(package) \
	  DOC_DIR=debian/$(package)-doc$(DOCDIR) \
	  HTML_DIR=debian/$(package)-doc$(DOCDIR)/gprolog.html \
	  EXAMPLES_DIR=debian/$(package)-doc$(DOCDIR)/examples

PATH = /usr/bin:/bin

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

build: build-indep build-arch

build-indep: build-indep-stamp
build-indep-stamp:
	dh_testdir
# -- commands to configure and build the package ------------------------------
	( PATH=${PATH}; \
	  if [ ! -f $(SRCDIR)/Makefile ]; then \
	    cd $(SRCDIR); \
	    autoconf && \
	    CPPFLAGS="${CPPFLAGS}" ./configure $(CONFOPTIONS); fi )
	PATH=${PATH} make -C doc pdf html
# -----------------------------------------------------------------------------
	touch build-indep-stamp

build-arch: build-arch-stamp
build-arch-stamp:
	dh_testdir
# -- commands to configure and build the package ------------------------------
	( PATH=${PATH}; \
	  cd $(SRCDIR); \
	  if [ ! -f configure ]; then autoconf; fi; \
	  if [ ! -f config.status -o configure -nt config.status ]; then \
	    CPPFLAGS="${CPPFLAGS}" ./configure $(CONFOPTIONS); \
	  fi )
	PATH=${PATH} make -C $(SRCDIR)
# -----------------------------------------------------------------------------
	touch build-arch-stamp


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

clean:
	dh_testdir
	dh_testroot
	rm -f build-arch-stamp build-indep-stamp
# -- commands to clean up after the build process -----------------------------
	( PATH=${PATH}; \
	  cd $(SRCDIR); \
	  if [ ! -f configure ]; then autoconf; fi; \
	  if [ ! -f config.status -o configure -nt config.status ]; then \
	    CPPFLAGS="${CPPFLAGS}" ./configure $(CONFOPTIONS); \
	  fi )
	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 src/src/autom4te.cache
# -- clean up docs ------------------------------------------------------------
	make -C doc clean-full
	find doc -name \*.pdf | xargs -r rm -f
	find doc -name \*.hh[ck] | xargs -r rm -f
	find doc -name \*.fp | xargs -r rm -f
	find doc -name manual\*.html | xargs -r rm -f
	rm -f debian/doc-base
# -- clean up examples --------------------------------------------------------
	find examples -name Makefile | sed 's;/Makefile;;' | \
	  while read DIR; do make -C "$$DIR" clean; done
# -----------------------------------------------------------------------------
	dh_clean


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

install-arch:
install-arch: build-arch
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs -A
# -- commands to install the package into the staging area --------------------
	make -C $(SRCDIR) $(INSTALLDIRS) install-system
# 	find debian/$(package)/usr/bin | while read F; do \
# 	  mv $$F $$F-$(variant); \
# 	done
# -- Install manpages ---------------------------------------------------------
	mkdir -p debian/$(package)/usr/share/man/man1/
	gzip -9 < debian/manpage \
		> debian/$(package)/usr/share/man/man1/$(package).1.gz
	for CMD in gplc hexgplc pl2wam wam2ma ma2asm fd2c; do \
	  ln -sf $(package).1.gz \
		 debian/$(package)/usr/share/man/man1/$$CMD$(variant).1.gz; \
	done
# -- Install auxiliary stuff --------------------------------------------------
	mkdir -p debian/$(package)/usr/share/icons/
	install -c -m 444 debian/$(package).xpm \
		debian/$(package)/usr/share/icons/
# -----------------------------------------------------------------------------
#	dh_movefiles


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

install-indep:
install-indep: build-indep
	dh_testdir
	dh_testroot
	dh_clean -k
	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
# -- add doc-base support -----------------------------------------------------
#	mkdir -p debian/$(package)-doc/usr/share/doc-base
#	cp -a debian/autodocs/$(package)-doc \
#		debian/$(package)-doc/usr/share/doc-base
# -----------------------------------------------------------------------------
#	dh_movefiles


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

binary-indep: build-indep install-indep
	dh_testdir -i
	dh_testroot -i
#	dh_installdebconf -i
#	ln -sf autodocs/$(package)-doc debian/doc-base
	dh_installdocs -p$(package)-doc
	dh_installexamples -i
#	dh_installmenu -i
	dh_installemacsen -i
#	dh_installman -i
	dh_installinfo -i
	dh_installchangelogs -i
	dh_link -i
	dh_compress -i
	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
	ln -sf autodocs/$(package) debian/doc-base
	dh_installdocs -a
	dh_installexamples -a
	dh_installmenu -a
#	dh_installemacsen -a
	dh_installman -a
	dh_installinfo -a
	dh_installchangelogs -a src/ChangeLog
	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
#	dh_makeshlibs -a
	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
