#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 by Joey Hess.
#
# Modified to use dbs by Federico Di Gregorio.
# Modified to change from dbs to dpatch by Norman Ramsey

DEB_BUILD_MAINT_OPTIONS := hardening=+all
DEB_CFLAGS_MAINT_APPEND := -Wall
include /usr/share/dpkg/buildflags.mk

ICONC=icont

MAKE_OPTIONS = LIBSRC=icon ICONC="${ICONC}" CFLAGS="${CFLAGS}"\
  CPPFLAGS='$(CPPFLAGS)' \
  LDFLAGS='$(LDFLAGS)' \
  BIN=/usr/bin LIB=/usr/lib/noweb \
  MAN=/usr/share/man \
  ELISP=/usr/share/emacs/site-lisp \
  TEXINPUTS=/usr/share/texmf/tex/plain/misc \
  PATH=$(PATH):$(CURDIR)/debian/bin

%:
	dh $@ --sourcedirectory=src --with=tex

patch-awk-stamp:
	cd src && ./awkname awk
	touch patch-awk-stamp

# Authors: <nr@eecs.harvard.edu>, <uhoreg@debian.org>
# Update timestamp to avoid needing noweb to build noweb

# earlier patches hit the noweb file from which these files are derived;
# by touching them, we make it possible to build noweb without already
# having noweb.
	sleep 1 # so that we have at least a second difference in timestamps
	touch \
	  src/shell/tmac.w \
	  src/shell/noweave \
	  src/shell/noroff \
	  src/shell/toroff \
	  src/tex/noweb.sty \
	  src/tex/nwmac.tex \
	  src/Makefile

.PHONY: override_dh_auto_clean
override_dh_auto_clean:
	dh_auto_clean
	if test -r patch-awk-stamp; then \
	  cd src && ./awkname nawk; \
	fi
	rm -f patch-awk-stamp
	rm -f contrib/norman/scopehack
	find . -name "*~" -name "core" -exec rm -f "{}" +

.PHONY: override_dh_auto_build
override_dh_auto_build: patch-awk-stamp
# build a version of notangle that we can use to build the man pages
	mkdir -p debian/bin
	sed "s@|LIBDIR|@$(CURDIR)/src/c@" src/shell/notangle > debian/bin/notangle
	chmod +x debian/bin/notangle

	dh_auto_build -- $(MAKE_OPTIONS)
	cd contrib/norman && $(ICONC) scopehack

ifeq (,$(filter nodoc,$(DEB_BUILD_OPTIONS)))
	cd src/xdoc && latex guide.tex && \
	  while grep -s 'Rerun to get cross-references right' guide.log; \
	  do latex guide.tex; done && \
	  dvips guide.dvi -o guide.ps
	cd src/xdoc && latex onepage.tex && \
	  while grep -s 'Rerun to get cross-references right' onepage.log; \
	  do latex onepage.tex; done && \
	  dvips onepage.dvi -o onepage.ps
	cd src/xdoc && ../icon/sl2h guide.tex | \
	  ../icon/htmltoc >guide.html
endif

.PHONY: override_dh_installexamples
override_dh_installexamples:
# removes because is in standard tetex (and other not-needed files)
	dh_installexamples -Xmulticol.sty -Xprimes.b examples/*

.PHONY: override_dh_auto_install
override_dh_auto_install:
	dh_auto_install -- $(MAKE_OPTIONS)
# gives tmac its name and move it to right directory
	mkdir -p debian/noweb/usr/share/groff/tmac
	mv debian/noweb/usr/lib/noweb/tmac.w \
	  debian/noweb/usr/share/groff/tmac/noweb.tmac
