DESTDIR		=
prefix		= /usr

bindir		= ${prefix}/bin
datadir		= ${prefix}/share/debhelper/dh-kpatches
docdir		= ${prefix}/share/doc/dh-kpatches
mandir		= ${prefix}/share/man
man1dir		= ${mandir}/man1

DHSCRIPT	= dh_installkpatches
OTHERSCRIPTS	= lskpatches
TEMPLATES	= apply.tmpl unpatch.tmpl register-patch
VERSION		= $(shell dpkg-parsechangelog | grep ^Version: | cut -d' ' -f2)

NOWEBSRC	= dh-kpatches.nw
NOWEBOUTSRC	= $(shell noroots ${NOWEBSRC} | sed 's/<<\(.*\)>>/\1/')


all: ${DHSCRIPT} ${TEMPLATES}
	make -C doc all

${DHSCRIPT}: ${DHSCRIPT}.in
	sed -e s=@TMPLDIR@=${datadir}= -e s=@DHKPVERS@=${VERSION}=<$< >$@
	chmod 755 $@

internals: ${NOWEBSRC:.nw=.ps} ${NOWEBSRC:.nw=.html}

${NOWEBOUTSRC}: % : ${NOWEBSRC}
	notangle -R$@ $< | cpif $@

%.html: %.nw
	noweave -html -delay \
	  -filter "noweb/guesslang ${NOWEBOUTSRC} | noweb/inheritlang" \
	  -filter "noweb/htmltoc | noweb/enscript-html --color" \
	  -x \
	  $< > $@

%.ps: dh-kpatches.book %.html
	htmldoc --batch $< || test $$? = 4

clean:
	rm -f ${DHSCRIPT} ${NOWEBOUTSRC}
	rm -rf ${NOWEBSRC:.nw=-html}
	rm -f *.aux *.log *.dvi *.ps *.latex
	make -C doc clean

install: all
	install -m755 -d ${DESTDIR}${datadir} ${DESTDIR}${bindir}
	install -m755 ${DHSCRIPT} ${OTHERSCRIPTS} ${DESTDIR}${bindir}
	install -m644 ${TEMPLATES} ${DESTDIR}${datadir}
	make -C doc install

uninstall:
	rm -f ${DESTDIR}${bindir}/${DHSCRIPT}
	-cd ${DESTDIR}${bindir} && rm -f ${OTHERSCRIPTS}
	rm -rf ${DESTDIR}${datadir}
	make -C doc uninstall
