Description: With source format 3.0 (quilt), the sequence
 debuild && debuild clean may result in a succession of
 patch -> build -> unpatch -> clean actions, so that some
 files generated by the patched build system or added cookies
 may not be removed.
 .
 This is worst: upstream build system renames and reencodes
 the cookies during the build...
 .
 We store all generated files in a specific "gen" directory
 to provide an easy work-around in debian/rules clean target, in the
 hope that upstream will copy the idea.
Author: Nicolas Boulenguez <nicolas.boulenguez@free.fr>

Index: b/datfiles/Makefile
===================================================================
--- a/datfiles/Makefile	2012-07-07 17:31:00.000000000 +0200
+++ b/datfiles/Makefile	2012-07-07 17:34:58.000000000 +0200
@@ -1,16 +1,47 @@
+# COOKIEDIR  must point to the installation directory.
+# If OFFENSIVE=1, offensive cookies are installed into OCOOKIEDIR.
+# If WCOOKIEDIR=1, html cookies are installed into WCOOKIEDIR.
 
-COOKIES=art ascii-art computers cookie definitions drugs education ethnic \
-	food fortunes goedel humorists kids law linuxcookie literature \
-	love magic medicine men-women miscellaneous news people pets \
-	platitudes politics riddles science songs-poems sports \
-	startrek translate-me wisdom work linux perl knghtbrd \
-	paradoxum zippy debian disclaimer tao
+COOKIES := $(filter-out gen html off Makefile,$(wildcard *))
+RECODED := $(addprefix gen/,$(COOKIES))
+DAT     := $(addsuffix .dat,$(RECODED))
 
-STRFILE=../util/strfile
+STRFILE := ../util/strfile
 
-.PHONY: all cookies o-cookies w-cookies install clean
+.PHONY: all check clean install
 
-all: cookies o-cookies w-cookies
+all:: $(RECODED) $(DAT)
+
+$(DAT): gen/%.dat: gen/%
+	$(STRFILE) $<
+
+$(RECODED): gen/%: % | gen
+	recode latin1..u8 < $< > $@
+
+gen:
+	mkdir $@
+
+install:: all
+	install -m 0755 -d $(COOKIEDIR)
+	install -m 0644 $(RECODED) $(DAT) $(COOKIEDIR)
+	for i in $(COOKIES) ; do \
+	  ln -sf $$i $(COOKIEDIR)/$$i.u8 ; \
+	done
+
+ifeq (1,$(OFFENSIVE))
+all install::
+	$(MAKE) $@ -C off OCOOKIEDIR=$(OCOOKIEDIR)
+endif
+
+ifeq (1,$(WEB))
+all install::
+	$(MAKE) $@ -C html WCOOKIEDIR=$(WCOOKIEDIR)
+endif
+
+clean::
+	rm -rf gen
+	$(MAKE) $@ -C off
+	$(MAKE) $@ -C html
 
 check:
 	for i in $(COOKIES) ; \
@@ -28,46 +59,3 @@
 			fi; \
 			echo " passed " ;\
 		done;
-
-cookies: cookies-stamp
-
-o-cookies:
-	if [ $(OFFENSIVE) = 1 ] ; then \
-	    cd off && $(MAKE) OCOOKIEDIR=$(OCOOKIEDIR) ; fi
-
-w-cookies:
-	if [ $(WEB) = 1 ] ; then \
-	    cd html && $(MAKE) WCOOKIEDIR=$(WCOOKIEDIR); fi
-
-cookies-stamp:
-	rm -f *.dat
-	for i in $(COOKIES) ; \
-	    do \
-	        if [ ! -f $$i.old ] ; then \
-	        	cp $$i $$i.old; \
-		fi; \
-	        recode latin1..u8 $$i ; \
-		$(STRFILE) $$i ; \
-		ln -s $$i $$i.u8 || exit $? ; \
-	    done
-	touch cookies-stamp
-
-install: cookies-stamp
-	install -m 0755 -d $(COOKIEDIR)
-	if [ $(OFFENSIVE) = 1 ] ; then cd off && $(MAKE) install ; fi
-	if [ $(WEB) = 1 ] ; then cd html && $(MAKE) install ; fi
-	for i in $(COOKIES) ; do \
-		install -m 0644 $$i $$i.dat $(COOKIEDIR) || exit $? ; \
-		cp -d $$i.u8 $(COOKIEDIR) ; \
-		done
-
-clean:
-	rm -f cookies-stamp *.dat
-	rm -f *.u8
-	cd off && $(MAKE) clean
-	cd html && $(MAKE) clean
-	for i in $(COOKIES) ; do \
-		if [ -f $$i.old ] ; then \
-			mv $$i.old $$i; \
-		fi; \
-	done
Index: b/datfiles/off/Makefile
===================================================================
--- a/datfiles/off/Makefile	2012-07-07 17:31:00.000000000 +0200
+++ b/datfiles/off/Makefile	2012-07-07 17:31:06.000000000 +0200
@@ -1,52 +1,31 @@
+# OCOOKIEDIR must point to the installation directory.
 
-OCOOKIES_UNROTATED=drugs misandry privates sex astrology ethnic \
-	miscellaneous racism songs-poems black-humor hphobia misogyny \
-	religion vulgarity definitions limerick politics riddles zippy \
-	cookie fortunes linux art atheism debian knghtbrd
-
-OCOOKIES_ROTATED=
-
-OCOOKIES=$(OCOOKIES_UNROTATED) $(OCOOKIES_ROTATED)
-
-STRFILE=../../util/strfile
-ROT=../../util/rot
-
-
-all:	ocookies-stamp
-
-ocookies-stamp:	rotated-stamp
-	rm -f *.dat
-	for i in $(OCOOKIES) ; do $(STRFILE) -x $$i || exit $$? ; done
-	touch ocookies-stamp
-
-rotated-stamp: recoded-stamp
-	for i in $(OCOOKIES_UNROTATED) ; \
-	    do $(ROT) < unrotated/$$i > $$i || exit $$? ; ln -s $$i $$i.u8; done
-	touch rotated-stamp
-
-recoded-stamp:
-	for i in $(OCOOKIES_UNROTATED) ; \
-	    do \
-	        if [ ! -f unrotated/$$i.old ]; then \
-	    	    cp unrotated/$$i unrotated/$$i.old ; \
-		fi; \
-		recode latin1..u8 unrotated/$$i; \
-	    done
-	touch recoded-stamp
-	
-install:
+COOKIES := $(subst unrotated/,,$(wildcard unrotated/*))
+RECODED := $(addprefix gen/,$(COOKIES))
+DAT     := $(addsuffix .dat,$(RECODED))
+
+STRFILE := ../../util/strfile
+ROT     := ../../util/rot
+
+.PHONY: all clean install
+
+all: $(RECODED) $(DAT)
+
+$(DAT): gen/%.dat: gen/%
+	$(STRFILE) -x $<
+
+$(RECODED): gen/%: unrotated/% | gen
+	recode latin1..u8 < $< | $(ROT) > $@
+
+gen:
+	mkdir $@
+
+install: all
 	install -m 0755 -d $(OCOOKIEDIR)
-	for i in $(OCOOKIES) ; \
-	    do install -m 0644 $$i $$i.dat $(OCOOKIEDIR) || exit $$? ; \
-	    cp -d $$i.u8 $(OCOOKIEDIR) ; \
+	install -m 0644 $(RECODED) $(DAT) $(OCOOKIEDIR)
+	for i in $(COOKIES) ; do \
+	  ln -sf $$i $(OCOOKIEDIR)/$$i.u8 ; \
 	done
 
 clean:
-	rm -f ocookies-stamp rotated-stamp recoded-stamp *.dat $(OCOOKIES_UNROTATED)
-	rm -f *u8
-	for i in $(OCOOKIES_UNROTATED) ; do \
-		if [ -f unrotated/$$i.old ] ; then \
-			mv unrotated/$$i.old unrotated/$$i; \
-		fi; \
-	done
-									
+	rm -rf gen
