# ~jhs/public_html/dots/Makefile by Julian H. Stacey
SUBDIR =
SUBDIR += .xmodmap
SUBDIR += .screenlayout

PEOPLE_INC	= ${HOME}/.procmailrc_people_inc
PHRASES_INC	= ${HOME}/.procmailrc_phrases_inc

PROCMAILRC_DOMAINS2BLOCK_SRC = .procmailrc_domains2block_src

# Domains to block master is in /site linked from /etc for sendmail access:
#	cd ~.DOTS/.PUBLIC ; ln -s /site/etc/mail/domains2block \
#		 ${PROCMAILRC_DOMAINS2BLOCK_SRC}
#	cd ~.DOTS ; ln -s .PUBLIC/${PROCMAILRC_DOMAINS2BLOCK_SRC}
#	cd ~ ; ln -s .DOTS/${PROCMAILRC_DOMAINS2BLOCK_SRC}
# A copy is here for humans to browse if mail to @berklix gets rejected.
#	~/.procmailrc_domains2block_txt
# the _txt copy is referenced by:
#	public_html/bin/.sh/grepspam
#	public_html/dots/.procmailrc
#	public_html/dots/Makefile
#	public_html/fax/main/index.lmth
#	public_html/mail/index.lmth
#	public_html/phone/main/index.lmth
#	public_html/src/index.lmth
# The name might later need to change from _txt to .txt as some
# apache web servers or is it some browsers, dont know how to display
# random names but do know how to display .txt

XX != hostname -s

all:	index.html
	make ${XX}
	@echo "Suggestion: make all_test"

LOCK_BASE = /home/jhs/tmp/.public_html_dots_make
# To avoid a double invocation of make vi zaping source ${PHRASES_SRC_ROOTED}
# which it did do occasionaly before I added locks.

# ../../.DOTS/

PEOPLE_SRC	= ${HOME}/.procmailrc_people_src
#		${HOME} as these are private files not web exported.

PHRASES_SRC_RELATIVE	= .procmailrc_phrases_src
PHRASES_SRC_ROOTED	= ${HOME}/${PHRASES_SRC_RELATIVE}
# Spammer phrases, using grep expressions,
# Sample for global edits:
#	(dont|do not|don't)			do((| )no|n(|\\'))t
#	(you're|you are)			you( a|(|\\'))re

PHRASES_SHRUNK	= ${HOME}/.procmailrc_phrases_shrunk


MASTERS= 
MASTERS += ${PEOPLE_SRC} 
MASTERS += ${PHRASES_SRC_ROOTED} 
MASTERS += ${PROCMAILRC_DOMAINS2BLOCK_SRC}

DEPENDENCIES= ${PEOPLE_INC} ${PHRASES_INC}

# Run make inspect manually to visually inspect, if anything might have got
# shortened or truncated, if you are in a panic, after eg:
#	an abort on an attempted double lock
#	a system crash or file system damage
#	an edit screw up, especially complex global edits in ${PHRASES_SRC_ROOTED}
panic inspect:
	ps -laxww | grep vi
	@echo "Valuable Sources seen with ls -l -H ${MASTERS}"
	@ls -l -H ${MASTERS}
	@echo "Valuable Sources seen with wc -l ${MASTERS}"
	@wc -l ${MASTERS} | grep -v total
	@echo "Dependencies that can be regenerated:"
	@-ls -l -H ${DEPENDENCIES}
	@-wc -l ${DEPENDENCIES} | grep -v total
	@echo "Locks (if any)"
	@-ls -l ${LOCK_BASE}*
	@echo "Compare with:"
	@-ls -l \
	 /site/etc/mail/domains2block \
	 /crypt/fs/1700m/home/jhs/._dir/.DOTS/.procmailrc_people_src\
	 /crypt/fs/1700m/home/jhs/._dir/.DOTS/${PHRASES_SRC_RELATIVE}
	@echo "Compare with backup sizes in /media/500g/backup/home/jhs/base/.DOTS"
	@(cd /media/500g/backup/home/jhs/base/.DOTS ; ls -l \
		.procmailrc_people_src \
		.procmailrc_phrases_src \
		.procmailrc_domains2block_src )
	@echo "Suggestion: Inspect in read-only mode: vi -R ${MASTERS}"
	@echo "Consider if or not you want to manually remove any of:"
	-ls -l ${LOCK_BASE}_*.lockfile

fire blak lapr dell: ${PHRASES_INC} ${PEOPLE_INC}
	@# Not using default html_links .procmailrc_domains2block_txt

land slim test:
	@echo "Skipped as ${PEOPLE_INC} & ${PHRASES_INC} not exported."

.procmailrc_domains2block_txt:		${PROCMAILRC_DOMAINS2BLOCK_SRC}
	cp ${PROCMAILRC_DOMAINS2BLOCK_SRC} .procmailrc_domains2block_txt

warn:
	-ls -l .kde/share/apps/knotes/notes/knote*
	@# file "knote 1" has a space in the name, which is a bloody nuisance !

.rhosts.for_export:	../../.rhosts.master
	@echo "Starting $@ {"
	echo "Dummy" > ${.CURDIR}/$@
	chmod 600 ${.CURDIR}/$@
	#grep -v ^# ../../.rhosts.master > ${.CURDIR}/$@
	@echo "Finishing $@ }"

BLA!= cd ${.CURDIR} ; find . -type f -print0 \
	\( ! -name index.html -and ! -name index.lmth.tmp \)

# .if target(index.lmth)
# JJLATER also need to add several other target(...) so for now dont use if.
NO_OBJ= true
# .endif

index.lmth:	${BLA} Makefile
	@echo "Starting $@ {"
	echo "<HTML><HEAD><TITLE>" > ${.CURDIR}/$@
	echo "Julian H. Stacey's ~/dots/</TITLE></HEAD>" >> ${.CURDIR}/$@
	echo "<BODY><H1><a href=../>Julian H. Stacey</a>'s" >> ${.CURDIR}/$@
	echo "<BR>Index of ~jhs/dots/</H1>" >> ${.CURDIR}/$@
	echo "<P><B>Generated `date` by " >> ${.CURDIR}/$@
	echo "<a href=Makefile>Makefile</a></B><BR>" >> ${.CURDIR}/$@
	echo "(Generated " >> ${.CURDIR}/$@
	echo "because web servers do not list dot files.) <P>" >> ${.CURDIR}/$@
	echo "<ul>" >> ${.CURDIR}/$@
	@# Can not use ${BLA} in next line as need filenames one per line.
	(cd ${.CURDIR}; find . -type f \(	\
		! -name index.lmth -and \
		! -name index.lmth.tmp \
		\) \
		-print ) | grep -v .no_customise | sort > ${.CURDIR}/$@.tmp
	for i in `cat $@.tmp` ; do \
		echo -n "<li><a href=$$i>$$i</a>" >> ${.CURDIR}/$@ ; \
		head -1 ${.CURDIR}/$$i >> ${.CURDIR}/$@ ; \
		echo "</li>" >> ${.CURDIR}/$@ ; \
		done
	-rm ${.CURDIR}/$@.tmp
	@# The head above should be changed to cat till blank line
	echo -n '</ul><HR><a href="../txt/footer.html">' >> ${.CURDIR}/$@
	echo '<img alt="footer" src="/gifs/footer.gif"></a></BODY></HTML>' \
		>> ${.CURDIR}/$@
	chmod 644 ${.CURDIR}/$@
	@echo "Finishing $@ }"

# Xdefaults:
#	le -d .Xdefaults.master > .Xdefaults.desk
#	le -w .Xdefaults.master > .Xdefaults.wall
#	le -p .Xdefaults.master > .Xdefaults.phil
#	le -c .Xdefaults.master > .Xdefaults.chris
#	le -r .Xdefaults.master > .Xdefaults.lapr
#	ln .Xdefaults.fire .Xdefaults
#	# le -`hostname` .Xdefaults.master > .Xdefaults

vi:	# cd ~/public_html/dots; make vi
	@# Edit in new phrases & domains, sort, & produce dependencies.
	@echo "Starting $@ {"
	@# ------
	@# If I want to check they pre-exist, in case of a catastophic edit.
	@# make ${PHRASES_INC} ${PEOPLE_INC}
	@# ------
	@# make sort
	@# Sources are kept sorted, no need to waste time pre sorting,
	@# {------
	@echo "Locking before edit starts:	${LOCK_BASE}_$@.lockfile "
	@if ( test -e ${LOCK_BASE}_$@.lockfile ) ; \
		then \
			echo "Lock fails as pre-existant:	${LOCK_BASE}_$@.lockfile" ; \
			ls -l ${LOCK_BASE}_$@.lockfile ; \
			echo    "`pwd`/Makefile: make vi Failed: Already locked." ; \
			echo    "Suggest you run: cd ~/public_html/dots; make inspect" ; \
			echo -n "`cd ~/public_html/dots; make inspect`" ; \
			exit 1 ; \
		else \
			true ; \
		fi
	lockfile ${LOCK_BASE}_$@.lockfile
	@# This lock is not so important, as it just stop me the human
	@# ocasionaly accidentaly doing a double edit on the same file set,
	@# when vi will warn m when I write out, & so I can abort the edit
	@# &/or write to temporary files, then compare & rescue any mess.
	@# The later locks around the sorts are far more important,
	@# as they will zap master sources if more than one at a time.
	@# ------
	vi ${PEOPLE_SRC} ${PHRASES_SRC_ROOTED} /site/etc/mail/domains2block
	wc ${PEOPLE_SRC} ${PHRASES_SRC_ROOTED} /site/etc/mail/domains2block
	@echo "Edit has ended, Unlocking ${LOCK_BASE}_$@.lockfile"
	rm -f ${LOCK_BASE}_$@.lockfile
	@# ---}
	@echo "Sorting, as the edit will inserted more random spam phrases."
	make sort
	@echo "Caution:	If you have not run this on /host/site your edits via"
	@echo "~/${PROCMAILRC_DOMAINS2BLOCK_SRC} -> /site/etc/mail/domains2block"
	@echo "will get over written from host=site later."
	@# ---
	@echo "Making dependent includes ${PHRASES_INC} ${PEOPLE_INC}"
	make
	-ls -l ${PHRASES_INC} ${PEOPLE_INC}
	@# The - above is as these are not exported to public servers.
	@# ---
	@echo -n "Testing for syntax errors in: "
	@echo	"$$HOME/.DOTS/${PHRASES_SRC_RELATIVE}"
	@echo "with:	grepspam /dev/null"
	@echo	"If there are any errors such as:"
	@echo	"	egrep: Unmatched ( or \\("
	@echo	"	egrep: Unmatched ) or \\)"
	@echo	"	egrep: Trailing backslash"
	@#	@echo	"between next { & }"
	@echo	"Then do:	vi -c/'look for this' \\"
	@echo	"	`pwd`/Makefile"
	@# Then do:
	@#	vi -c/"echo CheckPhrase:" `which grepspam`
	@#	Uncomment the ## echo CheckPhrase:
	@#	cd ~/tmp && rm -rf grepspam && mkdir grepspam \
	@#		 && cd grepspam && script
	@#	nice grepspam /dev/null
	@#	exit ; cd .. ; rm -rf ~/tmp/grepspam
	@#	# After the stream stops its verbose stream,
	@#	# grepspam has completed,
	@#	# the shell prompts, type
	@#	# ^D , which terminates the script process.
	@#	# (warning crap binary umlauts might need an xterm reset)
	@#	^D
	@#	vi typescript
	@#		:g/CheckPhrase:/d
	@#		find & mark the error report, eg
	@#			egrep: Unmatched ) or \)
	@#		Or
	@#			egrep: Trailing backslash
	@#		u	to reverse the edit & see the erroneous phrase
	@#	vi /home/jhs/${PHRASES_SRC_RELATIVE} # fix the erroneous phrase
	grepspam /dev/null
	@#	{} around grepspam no longer needed,
	@#	it used to return an error code if there is an error
	@echo "Finished grepspam /dev/null"
	@# --------
	@make sources_test
	@echo "Suggestion: Consider occasionaly calling make all_test"
	wc ${PEOPLE_SRC} ${PHRASES_SRC_ROOTED} /site/etc/mail/domains2block
	@echo "Suggest make chk_umlauts"
	@echo "Finishing $@ }"

chk_umlauts:
	@# JJLATER Warniing raw binary bytes below, should be delimited.
	-grep Ã ${PHRASES_SRC_ROOTED}
	-grep œ ${PHRASES_SRC_ROOTED}
	@# There may be other umlauts yet to search for

sources_test:	space_test word_test

static_rules_test:	brackets_test

all_test:	static_rules_test logs_test sources_test
	@# Security Warning:
	@#	Do not do script ... make all_test
	@#	else typescript may get exported to web.
	@#	Instead do:
	@#	cd ~/tmp ... script ... cd ~/public_html/dots ; make all_test
	@# Not yet complete: domains_test

PROCM +=	.procmailrc
PROCM +=	.procmailrc_3d
PROCM +=	.procmailrc_berklix
# PROCM +=	.procmailrc_divert
PROCM +=	${PROCMAILRC_DOMAINS2BLOCK_SRC}
PROCM +=	.procmailrc_errors
PROCM +=	.procmailrc_fonts
PROCM +=	.procmailrc_lists
PROCM +=	.procmailrc_multi
PROCM +=	.procmailrc_owner_dump
PROCM +=	.procmailrc_owner_keep
PROCM +=	.procmailrc_people_inc
PROCM +=	.procmailrc_people_src
PROCM +=	.procmailrc_private_dump_after
PROCM +=	.procmailrc_private_dump_before
PROCM +=	.procmailrc_private_keep
PROCM +=	.procmailrc_private_self
PROCM +=	.procmailrc_system_logs
PROCM +=	.procmailrc_web_form
PROCM_SKIP +=	${PHRASES_SRC_ROOTED}
PROCM_SKIP +=	${PHRASES_INC}
PROCM_SKIP +=	${PHRASES_SHRUNK}

PLOG = $$HOME/.mail.procmail.log

brackets_test:
	@echo "Starting $@ {"
	@# Test for unmatched bracket count on static manually edited
	@# .procmailrc files, (omitting source list & generated dependencies)
	@# theres''s a lot more syntax checking could be done than this,
	@# but it''s a start, & also ${PLOG} catches errors
	cd $$HOME && for i in ${PROCM} ; do \
		echo "Starting $$i" ;\
		brackets -p $$i ;\
		done
	@echo "Not tested: ${PROCM_SKIP}."
	@echo "Finishing $@ }"

# JJLATER Not yet checked for:
#	"zip code | country | phone | ssn |"
# unwantedly traps a mail from: date | mail -s xxxyyy

${PEOPLE_INC}:	${PEOPLE_SRC} Makefile
	@echo "Starting $@ {"
	@echo "# ${PEOPLE_INC}" > $@
	@echo "# DO NOT EDIT, Private File created by ~/public_html/dots/Makefile" >> $@
	@echo "# from ${PEOPLE_SRC}" >> $@
	@echo "# Catch known people" >> $@
	@# Names such as eg "John Smith" sometimes rather than eg
	@# j.smith@provider.com, instead I list as "John.Smith" which
	@# matches both "John Smith" at home and john.smith@company.com
	@# thought not inverted companies that list as Smith, John.
	@# Pre 2013-11-28:
	@#	for i in `awk -F : '{printf "%s\n",$$1}' ${PEOPLE_SRC}` ; do
	@# Now filter out comment lines beginning @@
	for i in `grep -v @@ ${PEOPLE_SRC} | \
			awk -F : '{printf "%s\n",$$1}' ` ; do \
	 echo ":0 Hw" >> $@ ; \
	 echo "* ^From:.*$$i" >> $@ ; \
	 echo " {" >> $@ ; \
	 echo " :0 cw" >> $@ ; \
	 echo " 	{" >> $@ ; \
	 echo " 	:0 Hw" >> $@ ; \
	 echo " 	* ^MIME-Version:" >> $@ ; \
	 echo " 		{" >> $@ ; \
	 echo " 		:0 Hw" >> $@ ; \
	 echo " 		* ^Content-Type: multipart/alternative" >> $@ ; \
	 echo " 			{" >> $@ ; \
	 echo " 			:0 Bw" >> $@ ; \
	 echo " 			* ^Content-Type: text/plain" >> $@ ; \
	 echo " 			* ^Content-Type: text/html" >> $@ ; \
	 echo ' 			| $$NOMIME | $$RCVSTORE +$$INBOX_KNOWN' >> $@;\
	 echo " 			}" >> $@ ; \
	 echo " 		:0 Hw" >> $@ ; \
	 echo " 		* ^Content-Type: text/html" >> $@ ; \
	 echo " 			{" >> $@ ; \
	 echo " 			:0 Bw" >> $@ ; \
	 echo " 			* html" >> $@ ; \
	 echo " 			* body" >> $@ ; \
	 echo ' 			| $$NOMIME | $$RCVSTORE +$$INBOX_KNOWN' >> $@;\
	 echo " 			}" >> $@ ; \
	 echo " 		}" >> $@ ; \
	 echo " 	:0 w" >> $@ ; \
	 echo ' 	| $$RCVSTORE +$$INBOX_KNOWN' >> $@ ; \
	 echo " 	}" >> $@ ; \
	 echo " :0 c" >> $@ ; \
	 echo " .backup/filtered" >> $@ ; \
	 echo " :0 ic" >> $@ ; \
	 echo ' | cd .backup/filtered && rm -f dummy `ls -t msg.* | sed -e 1,100d`' >> $@ ; \
	 echo " :0" >> $@ ; \
	 echo ' $$XBIFTWO' >> $@ ; \
	 echo " }" >> $@ ; \
	 done
	@# These comments were removed from the above,
	@# so they dont repeat & bloat ${PEOPLE_INC}.
	@#	{ ----------------
	@#	echo " :0 cw	# Place in mbox." >> $@ ;
	@#	echo ' | $$RCVSTORE +${INBOX_KNOWN}' >> $@ ;
	@#	echo " :0 cW	# Store recent 100." >> $@ ;
	@#	echo " .backup/filtered" >> $@ ;
	@#	echo " :0 Wic	# Keep .backup/filtered/ down to 100." >> $@ ;
	@#	echo ' | cd .backup/filtered && rm -f dummy `ls -t msg.* | sed -e 1,100d`' >> $@ ;
	@#	echo " :0 W	# Raise flag & ring bell." >> $@ ;
	@#	---------------- }
	@make space_test
	@echo "Finishing $@ }"

space_test:
	@echo "Starting $@ {"
	@echo "Testing for illegal space chars in left of"
	@echo "	${PEOPLE_SRC}"
	@# echo "Caution: if ${PEOPLE_SRC} has spaces in 1st column"
	@# echo "between 1st & last names, instead of 1 good filter emitted"
	@# echo "to ${PEOPLE_INC}, 2 bad filter are emitted,"
	@# echo "so some spam would then leak through to mbox."
	@# The next 3 lines of code are no good, as they already split
	@# around a space character.
	@# for i in `awk -F : '{printf "%s\n",$$1}' ${PEOPLE_SRC}` ; do \
	@#	echo "AA,$$i,BB" | grep -i -v ' ' ; \
	@#	done
	-awk -F : '{printf "%s\n",$$1}' ${PEOPLE_SRC} | grep -n ' ' | cat \
		> ${.CURDIR}/$@.tmp
	@# The cat avoids a preceeding Error code 1 (ignored)
	@cat ${.CURDIR}/$@.tmp
	@echo "If next test breaks, there are space[s] shown above in"
	@echo "	${PEOPLE_SRC}"
	@echo "	that you should edit out using \"make vi\""
	test \! -s ${.CURDIR}/$@.tmp
	@echo "OK, no illegal space in left column of ${PEOPLE_SRC}"
	@rm ${.CURDIR}/$@.tmp
	@# Unfortunately rm doesnt happen if illegal spaces exist.
	@# But after spaces are edited out & this is run again, it deletes.
	@echo "Finishing $@ }"

word_test:
	@echo "Starting $@ {"
	@echo "$@: Any dodgey single short words in ${PHRASES_SRC_ROOTED}"
	@echo "Look carefully as this test does not exit if it finds dodgey words."
	@grep -i "^[a-z]$$" ${PHRASES_SRC_ROOTED} | cat
	@# cat is a dummy so last command does not fail if no error strings.
	@grep -i "^[a-z][a-z]$$" ${PHRASES_SRC_ROOTED} | cat
	@grep -i "^[a-z][a-z][a-z]$$" ${PHRASES_SRC_ROOTED} | cat
	@grep -i "^[a-z][a-z][a-z][a-z]$$" ${PHRASES_SRC_ROOTED} | cat
	@grep -i "^[a-z][a-z][a-z][a-z][a-z]$$" ${PHRASES_SRC_ROOTED} | cat
	@# Next few commented out as they usually just show longish Polish words. All Polish is spam to me.
	@# @grep -i "^[a-z][a-z][a-z][a-z][a-z][a-z]$$" ${PHRASES_SRC_ROOTED} | cat
	@# @grep -i "^[a-z][a-z][a-z][a-z][a-z][a-z][a-z]$$" ${PHRASES_SRC_ROOTED} | grep -v burnfat | grep -v sexlife | cat
	@# -grep -i "^[a-z][a-z][a-z][a-z][a-z][a-z][a-z][a-z]$$" ${PHRASES_SRC_ROOTED} | cat
	@# -grep -i "^[a-z][a-z][a-z][a-z][a-z][a-z][a-z][a-z][a-z]$$" ${PHRASES_SRC_ROOTED} | cat
	@# -grep -i "^[a-z][a-z][a-z][a-z][a-z][a-z][a-z][a-z][a-z][a-z]$$" ${PHRASES_SRC_ROOTED} | cat
	@echo "Finishing $@ }"

sort:
	@echo "Starting $@ {"
	@make sort-phrases
	@make sort-people
	@# rsh site "cd public_html/dots ; make sort-domain"
	@# rsh site doesnt work when not at holz
	make sort-domain
	@# rsh as ${PROCMAILRC_DOMAINS2BLOCK_SRC} is a sym link to /site, & if we
	@# update any other host, rdist from host=site will zap it later.
	@echo "Finishing $@ }"

sort-phrases:
	@echo "Starting $@ {"
	wc ${PHRASES_SRC_ROOTED}
	@echo "Sorting phrases"
	@if ( test -e ${LOCK_BASE}_$@.lockfile ) ; \
		then \
			ls -l ${LOCK_BASE}_* ; \
			echo "Lock exists: ${LOCK_BASE}_$@.lockfile" ; \
			echo "`pwd`/Makefile: $@ is already locked, suggest you run make inspect after this abort" ; \
			exit 1 ; \
		else \
			true ; \
		fi
	lockfile ${LOCK_BASE}_$@.lockfile
	@# JJLATER I could add tab to space, but currently no need.
	@# JJLATER I need to add delimeter to next double space.
	cat ${PHRASES_SRC_ROOTED} \
		| sed -e 's/ $$//g' \
		| sed -e 's/  / /g' \
		| grep -v "^$$" \
		| tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz \
		| sort \
		| uniq \
		> ${.CURDIR}/$@.tmp
	if ( test -s ${.CURDIR}/$@.tmp && ! cmp -s ${.CURDIR}/$@.tmp ${PHRASES_SRC_ROOTED} ) ; \
		then \
			cat ${.CURDIR}/$@.tmp > ${PHRASES_SRC_ROOTED} ; \
			rm ${.CURDIR}/$@.tmp ; \
			echo "${PHRASES_SRC_ROOTED} now sorted" ; \
		else \
			echo -n "No difference (or zero size), " ; \
			echo "so not updating ${PHRASES_SRC_ROOTED}" ; \
			rm ${.CURDIR}/$@.tmp ; \
		fi
	@echo "Unlocking ${LOCK_BASE}_$@.lockfile"
	rm -f ${LOCK_BASE}_$@.lockfile
	wc ${PHRASES_SRC_ROOTED}
	@echo "Finishing $@ }"

sort-people:
	@echo "Starting $@ {"
	wc ${PEOPLE_SRC}
	@echo "Checking ${PEOPLE_SRC}"
	@if ( grep -s ' ' ${PEOPLE_SRC} > /dev/null ) ; \
		then \
		echo "Aborting: Remove spaces from ${PEOPLE_SRC}" ; \
		exit 1 ; \
		fi
	@echo "Sorting ${PEOPLE_SRC}"
	@if ( test -e ${LOCK_BASE}_$@.lockfile ) ; \
		then \
			ls -l ${LOCK_BASE}_* ; \
			echo "Lock exists: ${LOCK_BASE}_$@.lockfile" ; \
			echo "`pwd`/Makefile: $@ is already locked, suggest you run make inspect after this abort" ; \
			exit 1 ; \
		else \
			true ; \
		fi
	lockfile ${LOCK_BASE}_$@.lockfile
	cat ${PEOPLE_SRC} \
		| sed -e 's/ $$//g' \
		| grep -v "^$$" \
		| sort \
		| uniq \
		> ${.CURDIR}/$@.tmp
	# maybe later: tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz
	if ( test -s ${.CURDIR}/$@.tmp && ! cmp -s ${.CURDIR}/$@.tmp ${PEOPLE_SRC} ) ; \
		then \
			cat ${.CURDIR}/$@.tmp > ${PEOPLE_SRC} ; \
			rm ${.CURDIR}/$@.tmp ; \
			echo "${PEOPLE_SRC} now sorted" ; \
		else \
			echo -n "No difference (or zero size), " ; \
			echo "so not updating ${PEOPLE_SRC}" ; \
			rm ${.CURDIR}/$@.tmp ; \
		fi
	@echo "Unlocking ${LOCK_BASE}_$@.lockfile"
	rm -f ${LOCK_BASE}_$@.lockfile
	wc ${PEOPLE_SRC}
	@echo "Finishing $@ }"

sort-domain:
	@echo "Starting $@ {"
	wc ${PROCMAILRC_DOMAINS2BLOCK_SRC}
	@echo "Sorting domains"
	@if ( test -e ${LOCK_BASE}_$@.lockfile ) ; \
		then \
			ls -l ${LOCK_BASE}_* ; \
			echo "Lock exists: ${LOCK_BASE}_$@.lockfile" ; \
			echo "`pwd`/Makefile: $@ is already locked, suggest you run make inspect after this abort" ; \
			exit 1 ; \
		else \
			true ; \
		fi
	lockfile ${LOCK_BASE}_$@.lockfile
	cat ${PROCMAILRC_DOMAINS2BLOCK_SRC} \
		| sed -e 's/ $$//g' \
		| grep -v " " \
		| grep -v "^$$" \
		| tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz \
		| grep -v "^10\\." \
		| grep -v "^192\\.168\\." \
		| grep -v "^172\\.16\\." \
		| grep -v 144.76.10.75 \
		| grep -v 94.185.90.68 \
		| grep -v t-dialin.net \
		| grep -v dsl.sntc01.pacbell.net \
		| sort \
		| uniq \
		> ${.CURDIR}/$@.tmp
	if ( test -s ${.CURDIR}/$@.tmp && \
		! cmp -s ${.CURDIR}/$@.tmp ${PROCMAILRC_DOMAINS2BLOCK_SRC});\
		then \
			cp ${.CURDIR}/$@.tmp \
				${.CURDIR}/${PROCMAILRC_DOMAINS2BLOCK_SRC} ; \
			echo "${PROCMAILRC_DOMAINS2BLOCK_SRC} now sorted" ; \
		else \
			echo -n "No difference (or zero size), " ; \
			echo "so not updating ${PROCMAILRC_DOMAINS2BLOCK_SRC} ";\
		fi
	rm ${.CURDIR}/$@.tmp
	@# cp rather than move as ${PROCMAILRC_DOMAINS2BLOCK_SRC} --> /site/...
	@# The cp may fail if /site/etc/mail/domains2block is not uid=jhs,
	@# I'd like to add dip.t-dialin.net to the block list,
	@#	but that would block me.
	@# I ensure I havent included internal private IP ranges from spammers,
	@# that other innocents elsewhere will also use, by deleting
	@#	10.*.*.* & 192.168.*.* & 172.16.0.0-172.31.255.255
	@# The other IPs are numbers I might paste by mistake.
	@echo "Unlocking ${LOCK_BASE}_$@.lockfile"
	rm -f ${LOCK_BASE}_$@.lockfile
	wc ${PROCMAILRC_DOMAINS2BLOCK_SRC}
	@echo "Finishing $@ }"

grepspam_hook:	${PHRASES_SHRUNK}	# Called from ~jhs/bin/.sh/grepspam

# Remove blank lines from ${PHRASES_SHRUNK}, else they get turned into
# a match line in ${PHRASES_INC} consisting of a simple "* "
# which matches all lines, & personal mail gets dumped in spam box.
# Later phrases may be fed into a different anti spam tool, so preserve
# the original list, & copy & reduce to a safer form for procmail.
${PHRASES_SHRUNK}:	${PHRASES_SRC_ROOTED} Makefile
	@echo "Starting $@ {"
	cat ${PHRASES_SRC_ROOTED} | grep -v "^$$" > $@
	@# maybe later add here a global sed space to \s
	@echo "Finishing $@ }"

SPAM_TMP = ${HOME}/tmp/.procmailrc_spam.tmp
#	SPAM_TMP achieves a Massive speed-up, avoiding 5000 slow small nfs
#	appends to ~/dots/${PHRASES_INC}

${PHRASES_INC}: ${PHRASES_SHRUNK} Makefile # ${PROCMAILRC_DOMAINS2BLOCK_SRC}
	@echo "Starting $@ {"
	@echo "# DO NOT EDIT $@" > $@
	@echo "# Built by ~/public_html/dots/Makefile"	>> $@
	@echo "# From phrases source file: ${PHRASES_SRC_ROOTED}" >> $@
	@echo "# Private File" >> $@
	@echo "# Included by ~/.procmailrc_private" >> $@
	@# Warning phrases such as h.o.t. false match to olt in Ashbolt
	@# Warning phrases such as i.r.a.q (or pretty much anything else
	@#	of smae type) false match mime encoded pics
	@# Warning phrases such xyzxyzxyz .*
	@# via echo $$phrase
	@# unwantedly expand to:
	@#	xyzxyzxyz . .. .Xdefaults .acidriprc.src .gnokiirc
	@#	.gnokiirc.fire .indent.pro .indent.pro.comment
	@#	.jnewsrc .just_com .kermrc .kermrc.generic
	@#	.login_conf.mv .logout .mailcap .mime.types .pcemurc
	@#	.procmailrc .procmailrc_3d .procmailrc_berklix
	@#	${PROCMAILRC_DOMAINS2BLOCK_SRC} .procmailrc_domains2block_txt
	@#	.procmailrc_errors .procmailrc_fonts .procmailrc_last
	@#	.procmailrc_lists .procmailrc_multi .procmailrc_owner_dump
	@#	.procmailrc_owner_keep .procmailrc_system_logs
	@#	.procmailrc_web_form .profile .project .screenlayout
	@#	.termcap .tgdb_help .vac_example .xearth .xfigrc
	@#	.xmodmap .xpdfrc .xsession"
	@# so instead try echo "$$phrase"
	@# -------
	cp /dev/null ${SPAM_TMP}
	@# JJLATER XXXXXXXXX this blew on a bad 12.3-STABLE
	@# JJLATER add lockfile, prevent multiple occurences smashing original.
	@# Below, the sed expansion allows catching hopefully tabs as well as
	@# spaces ? though Ive not checked that, & does catch multiple spaces,
	@# & then  [[:space:]] was extended to ([[:space:]]|_) after I saw
	@# a character set using _ for spaces eg:
	@#	Subject: =?iso-8859-1?Q?Aanvraag_formulier_
	@# JJLATER The :space: macros fail to generate matching patterns,
	@# 	I wanted them for matching eg tabs, but they do not even
	@#	match space charcaters!
	@#	Best test with a small sample pattern before reintroducing
	@#	echo "$$phrase" | sed -e's/ /([[:space:]]|_)+/g' \
	@#		>> ${SPAM_TMP} ;\
	@# Meantime the '+' does work so identifies spam with
	@# intermittent multiple space characters between words.
	cat ${PHRASES_SHRUNK} | \
	  while read phrase ; do \
		echo ":0 BH" >> ${SPAM_TMP} ;\
		echo -n "* " >> ${SPAM_TMP} ;\
		echo "$$phrase" | sed -e's/ / +/g' \
			>> ${SPAM_TMP} ;\
		echo 'spam/phrases/.' >> ${SPAM_TMP} ;\
	  done
	@# 2 lines above here, choose 1 of these 2:
	@#	echo '| $$RCVSTORE +spam/phrases' >> ${SPAM_TMP} ;
	@#		Raises EXMH Blue highlighting
	@#	echo 'spam/phrases/.' >> ${SPAM_TMP} ;
	@#		No EXMH blue highlighting of new spams
	cat ${SPAM_TMP} >> $@
	@# Pre 2017-12-05 I used not to:
	@#	 "rm ${PHRASES_SHRUNK}" as useful for grepspam.
	rm ${PHRASES_SHRUNK}
	@# ----------------
	@# @echo "# Domains: From ~/dots/${PROCMAILRC_DOMAINS2BLOCK_SRC} " >> $@
	@# Next commented out as:
	@#	- short domains create false matches,
	@#	  particulalrly on binary enclosures.
	@#	- domain "this.com" used to falsely match against non spam
	@#	  email which had "this complaint" in the body, so
	@#	  I reduced ":0 BH" below to just
	@#	  ":0 H", however thats not good enough,
	@#	  JJLATER I should delimit all "." to
	@#	  tell procmail its a literal string
	@# cp /dev/null ${SPAM_TMP}
	@# cat ${PROCMAILRC_DOMAINS2BLOCK_SRC} | \
	@# while read domain ; do \
	@#	echo ":0 H" >> ${SPAM_TMP} ;\
	@#	echo "* ^Received:.*$$domain" >> ${SPAM_TMP} ;\
	@#	echo '| $$RCVSTORE +spam/domain' >> ${SPAM_TMP} ;\
	@#	done
	@# cat ${SPAM_TMP} >> $@
	@ rm ${SPAM_TMP}
	@# If code above is re-enabled to use ${PROCMAILRC_DOMAINS2BLOCK_SRC} ,
	@# then also add ${PROCMAILRC_DOMAINS2BLOCK_SRC} to
	@# phrases in ~/bin/.sh/grepspam
	@echo "Finishing $@ }"

domains_test:	${PHRASES_SHRUNK} ${PROCMAILRC_DOMAINS2BLOCK_SRC} Makefile
	@echo "Starting $@ {"
	@# Search for domains first, as thats more likely troublesome.
	@# Limitation: this does not search domains of mail lists
	@# on my remote servers.
	@# The '-' in next line is necessary to avoid first non matching
	@# line stopping the while loop.
	@# Do not "rm ${PHRASES_SHRUNK}" as useful for grepspam.
	-cat ${PROCMAILRC_DOMAINS2BLOCK_SRC} | \
	  while read domain ; do \
		echo "Searching for $$domain" > /dev/null ;\
		(cd /site ; xs Grep $$domain				|\
			grep -v ./etc/mail/domains2block:		|\
			grep -v ./domain/js.berklix.net/etc/mail/access: |\
			grep -v ./domain/berklix/etc/mail/access	\
			) ;\
		done
	echo "make not_yet_ready still needs to be developed."
	@echo "Finishing $@ }"

label_not_yet_ready:	${PHRASES_SHRUNK}
	@echo "Starting $@ {"
	@# the domain marked DISCARD above is typically a line such as:
	@#	./domain/js.berklix.net/etc/mail/access:best-deals2u.biz DISCARD	@#	./domain/xx.berklix.net/etc/mail/access:0.20.46.250 REJECT
	@# Search for phrases.
	-cat ${PHRASES_SHRUNK} | \
	while read phrase ; do \
		echo Seeking for "\"$$phrase\"" ;\
		(cd /site ; xs Grep "\"$$phrase\"" ) ;\
		sleep 10 ;\
		done
	@echo "Finishing $@ }"

# ${PROCMAILRC_DOMAINS2BLOCK_SRC} ${PHRASES_SRC_ROOTED}:
#	No command lines, just text, automaticaly sorted by Makefile.
# ${PHRASES_INC}:
#	Generated by Makefile.

logs_test:	log_echo regexp_test skipped_test brace_test \
		zero_test top_test logs_end

LINE=	"---------------------------------------------------------------------"

log_echo:
	@echo "Checking for some errors I have noticed in .procmailrc*"

regexp_test:
	@echo "Starting $@ {"
	@echo ${LINE}
	@# Test if a ( has not been delimited, and the matching )
	@# has been delimited.
	@echo "If you see any \"procmail: Invalid regexp \" between here {"
	-@grep "procmail: Invalid regexp "		${PLOG} || true
	@echo "and here }, then you need to:"
	@echo " vi -c/'procmail: Invalid regexp ' ${PLOG}"
	@echo "Finishing $@ }"

skipped_test:
	@echo "Starting $@ {"
	@echo ${LINE}
	@# Test if a ":0" line has been forgotten,
	@# preceeding a "* Match pattern".
	@# avoids the output messed by "*** Error code 1 (ignored)"
	@echo "If you see any \"procmail: Skipped \" between here {"
	-@grep "procmail: Skipped "			${PLOG} || true
	@echo "and here }, then you need to:"
	@echo " vi -c/'procmail: Skipped ' ${PLOG}"
	@echo "Finishing $@ }"

brace_test:
	@echo "Starting $@ {"
	@echo ${LINE}
	@# {
	@# Test if no action rule before an existing close brace }.
	@echo "If you see any \"Missing closing brace\" between here {"
	-@grep "Missing closing brace"			${PLOG} || true
	@echo "and here }, then you need to:"
	@echo " vi -c/'Missing closing brace' ${PLOG}"
	@echo "Finishing $@ }"

zero_test:
	@echo "Starting $@ {"
	@echo ${LINE}
	@# Test. I can not remember what error produces this but I have seen it.
	@echo "If you see any \"Folder: :0\" between here {"
	-@grep "Folder: :0"				${PLOG} || true
	@echo "and here }, then you need to:"
	@echo " vi -c/'Folder: :0' ${PLOG}"
	@echo "Finishing $@ }"

top_test:
	@echo "Starting $@ {"
	@echo ${LINE}
	@# Test:
	@# If eg one has an action rule of "| $RCVSTORE +$INBOX_PLAIN"
	@# & one has forgotten to define INBOX_PLAIN one get this log error:
	@#	"^  Folder: /usr/local/libexec/nmh/rcvstore +^I"
	@# JJLATER I need to add delimeter to next double space.
	@echo "If you see any \"Folder: :0\" between here {"
	-@grep "^ \\ Folder: /usr/local/libexec/nmh/rcvstore +	" ${PLOG} \
		 || true
	@echo "and here }, then you need to:"
	@echo " vi -c/'Folder: /usr/local/libexec/nmh/rcvstore' ${PLOG}"
	@# & one gets files such as	~/mail/1
	if [ -e ~/mail/1 ] ; then \
		ls -l ~/mail/[0-9]* ; \
		fi
	@echo "Finishing $@ }"

logs_end:
	@echo ${LINE}

.include <berklix.mk>
