#!/bin/sh   
#
#   A script to add an expiration date to mail messages.
#
#   Usage (from within mailer):  <pipemessage> | exp dd mmm yy
#

#	1) add expiration date
#	2) create an X-Label header if not already present, and
#	   prepend a center-dot or a macron to its value.
#	3) set an expiration date on enclosure-files.

#  For mbox messages, need a format which Mutt can recognize 
#       (dd mmm yy hh:mm:ss)

#  10 Jun 11
#  	overhaul of X-Label treatment to (hopefully) prevent headers
#  	from getting split.  "-I" flag ok; "-a" causes problems.
#  13 Aug 11
#  	Working fine on the whole, but problem persists for paycheck
#  	messages.
#  25 Feb 13
#	Problems setting enclosure expiration date on "format=flowed" messages

cat > /tmp/exp.$$

Old_X_Label=$(formail -z -x X-Label < /tmp/exp.$$)
  formail -I "Expires: $* 23:50:00" -I "x-Label: ¨$Old_X_Label" -I "Status: RO" \
	 < /tmp/exp.$$  >> $MAIL


# Set expiration date for enclosures (in ~/FTP/ )
#
#    The date needs to be processed by expr comparison, so convert
#    "dd mmm [yy]yy" to "yyyymmdd"

file=`sed -e /^-------/q -e 's/[*].*[*] *//' /tmp/exp.$$ | grep 'file =' | cut -f3 -d' '`

if [ _$file != _ ]; then
    PathToFile=${HOME}/FTP/${file}.mime 
    ExpDate=`date -j -f "%e %b %Y" "$*" +%Y%m%d 2> /dev/null || \
	     date -j -f "%e %b %y" "$*" +%Y%m%d`	# 2- or 4-digit year


    if [ -f $PathToFile ] ; then
	xattr -w PurgeDate $ExpDate $PathToFile	&
      else
	echo "     [41;37m   enclosure file not found  --  $file   [m" 
	exit 1
    fi
fi    

/bin/rm /tmp/exp.$$
exit 0

------------ Archive Scraps below ------------

###
#  ***	"-j" and "-f" flags do not work with version of "date" in OS 10.4 ***
#
### begin kludge

    expday=0${1}	# prepend a zero to the day (perhaps stripped off below)

			# convert month name into a number
    case `echo $2 | tr [:upper:] [:lower:]` in
	jan) expmo=01  ;;
	feb) expmo=02  ;;
	mar) expmo=03  ;;
	apr) expmo=04  ;;
	may) expmo=05  ;;
	jun) expmo=06  ;;
	jul) expmo=07  ;;
	aug) expmo=08  ;;
	sep) expmo=09  ;;
	oct) expmo=10  ;;
	nov) expmo=11  ;;
	dec) expmo=12  ;;
    esac

    expyr=${3:${#3}-2:2}    # trim year to two digits  *** Y-2099 bug ***  (deal with it then.)

    ExpDate=20${expyr}${expmo}${expday:${#expday}-2:2}
### end kludge
