#!/bin/sh
# ------------------------------------------------
# I dont recall sure what this file is for, as
# at Sat 25 Feb 2012 this file 
#	~/public_html/src/bsd/jhs/bin/local/mail/procmail.sh
# was date stamped:
#	-rw-r--r--  1 jhs  staff  5173 May  2  2011 procmail.sh
# whereas 
#	~/.procmailrc@ -> .DOTS/.procmailrc
#	~/._dir/.DOTS/.procmailrc@ -> .PUBLIC/.procmailrc
#	~/._dir/public_html/dots/
#	-rw-r--r--  1 jhs  staff  26934 Dec  8 14:47 .procmailrc
# ------------------------------------------------
# /usr/local/bin/procmail.sh			by Julian Stacey
# Installed from ~jhs/public_html/src/bsd/jhs/bin/local/mail/procmail.sh

echo "Not normally used, now I normally use .forward containing |/usr/local/bin/procmail"
#	- No longer needs to be called by crontab to process normally
#	  incoming mail from /var/mail/jhs, as that arrives via smtp &
#	  gets forwarded by .forward to procmail.
#	- Useful for when I return from weeks away, & gzip & ftp my
#	  remote mail, & manually install into /var/mail/jhs
#	  (when with that much data, I can also harvest a few Invalid regexp).
#	- Useful perhaps after cat /var/mailroot >> /var/mail/jhs
#	- Useful when I upgrade, forgot to get ~mk working & mail
#	  piled up in /var/mail/mk
#	- I do not let exmh auto include mail from /var/mail/jhs as it hangs
#	  the graphical update (in facts becomes unasable for hours)
#	  while procmail pattern matching bulk mails to all spam phrases).
# Bug:
#	If you get error message:
#	lockfile: Try praying, giving up on "/var/mail/jhs.lock"
#	lockfile: Can't unlock "/var/mail/jhs.lock"
# Likely you have /var/mail shown by ls -l as eg:
#	drwxr-xr-x   2 root  mail      512 Aug 20 13:30 ./
# so:	chmod o+w /var/mail

# Called both by crontab & manually.
# JJLATER	I should try calling from .forward

# It reads mail from the localhost & sorts it in my mail directories,
# it can run on any of my home internal hosts, (but is Slow over NFS),
# but it does not read from the central mail host, just the local mail host.
# its the job of my sendmail (+ named) to deliver mail from world via gateway
# to my receiving maildrop host.

# Path for lockfile & formail to avoid mail complaining: "Cannot fork".
PATH=$PATH:/usr/local/bin ; export PATH

# ORGMAIL=/host/user/var/mail/$LOGNAME
ORGMAIL=/var/mail/$LOGNAME

if test ! -s $ORGMAIL ; then	#{
		# echo "No mail for $LOGNAME on `hostname`"
		exit 0
	else
		# ls -l $ORGMAIL
	fi			#}

cd
if test ! -d mail ; then	#{
	echo "No mail directory under `pwd`/ on `hostname`"
	exit 1
	fi			#}

if [ "X`whoami`" = "Xjhs" ] ; then	#{
	ARCHIVE=/pub/FreeBSD/mail
	if cd $ARCHIVE ; then	 # {
		true
		else	# }{
		echo Warning cannot cd $ARCHIVE, mbox may flood with dups.
		fi	# }
	cd
	fi				#}

set seconds = -8	# default
LOCK=$HOME/mail/.procmail.tmp.lock
TEMPMAIL=.procmail.tmp

# Assert a lock unique to this script name.
if cd $HOME && lockfile $seconds -r 0 -l 10800 $LOCK 2> /dev/null
# "10800" is 3 hours, to manually resolve a reboot that leaves a dangling lock,
#	after which it gets deleted at next attempt to lock.
# "$seconds" is probably pointless in conjunction with "-r 0"
then	# {	Normal, Process mail.
	# Arrange to remove lock if error occurs.
	trap "rm -f $LOCK" 1 2	# 1=hangup 2=interrupt
		# JJLATER add more signals here.
		# Last time I did # a `reboot`, it left a dangling lock file.
		# I also need to use shutdown not reboot.

	# List mail gets copied to public archive, so use legible mode 033.
	# (assume ~/mail is 700 for private mail privacy).
	umask 033

	# Lock the user mail box by creating $ORGMAIL.lock
	lockfile $seconds -l10800 -ml
		# I used to specify just 3600 but maybe this doesnt allow
		# enough time on return from holiday with mega heaps of
		# mail & /or named timeouts ?

	test -e $TEMPMAIL && echo "Unexpected `ls -l $TEMPMAIL`"

	cat $ORGMAIL >> $TEMPMAIL && cat /dev/null >$ORGMAIL

	# Unlock user mailbox by removing $ORGMAIL.lock
	lockfile $seconds -mu

	# formail -q- -s procmail < $TEMPMAIL && rm -f $TEMPMAIL
		# Maybe one day add "-D -I Received" before -s

	formail -q- -s procmail -t < $TEMPMAIL && rm -f $TEMPMAIL
	# I added -t 18.06.2007 after losing mail day before with a bad rule
	# man procmail: -t
	#	Make procmail fail softly, i.e., if procmail  cannot
	#	deliver  the mail  to  any  of	the  destinations
	#	you  gave, the mail will not bounce, but will  return
	#	to  the  mailqueue.   Another  delivery- attempt
	#	will be made at some time in the future.

	# Remove the lock unique to this script name.
	rm -f $LOCK # -f needed as lock file is created read-only
	exit 0

else	# }{	Problem. Lock file exists.
	echo "$0 failed to lock,"
	echo "abandoning sort (but will not destroy mail)."
	# If mail gets discarded, it will not happen here, but maybe above,
	# (after the lockfile -l  ).
	if test -f $LOCK ; then #{
		echo "A lock file exists:"
		ls -l $LOCK
		echo "If stale (old), compared with now `date`,"
		echo "it might be from power failure or forced reboot ?"
		echo -n "If not old, probably a collision of manual "
		echo "invocation with crontab."
# One way of getting stale lock message above is when by accident when switching
# frequency, I forgot to comment out one of these 2 crontab lines:
#	2,7,12,17,22,27,32,37,42,47,52,57 * * * * /usr/local/bin/procmail.sh
#	2,12,22,32,42,52		  * * * * /usr/local/bin/procmail.sh
		echo " "
	fi			#}
	echo "If you manually remove a stale lock file, first save content of"
	if test -f $HOME/$TEMPMAIL ; then	#{
		ls -l $HOME/$TEMPMAIL
	fi					#}
	echo "Other files of interest:"
	if test -f $ORGMAIL ; then		#{
		ls -l $ORGMAIL
	fi					#}
	if test -f $ORGMAIL.lock ; then 	#{
		ls -l $ORGMAIL.lock
	fi					#}
	exit 1
fi	# }
