#! /bin/sh
# $Id: drmm 2.1 1997/09/25 03:16:25 jpeek book5 $
### drmm - "delayed rmm"; sets message(s) to be rmm'ed later by cron
### Usage: drmm [-help|-nov(erbose)] [msgs] [+/@folder]
##
##  USE drmm FOR MESSAGES THAT YOU WANT TO KEEP AROUND "FOR A WHILE".
##  drmm USES anno -noinplace TO ADD A HEADER COMPONENT LIKE THIS:
##      X-remove-after: 04 Feb 91
##  THEN, A cron JOB COMES AROUND EVERY SO OFTEN (ONCE A WEEK,
##  OR HOWEVER OFTEN YOU WANT) TO RUN THE COMMAND:
##      rmm `pick -datefield X-remove-after -before today -list`
##
##  IF YOU WANT TO KEEP THE MESSAGE AROUND LONGER, YOU CAN USE drmm
##  AGAIN ANY TIME BEFORE THE MESSAGE IS DELETED; THE NEW DATE WILL
##  OVERRIDE THE OLD DATE (BECAUSE IT COMES FIRST IN THE MESSAGE).
##  OR, YOU CAN DELETE THE HEADER COMPLETELY WITH A COMMAND LIKE:
##      vi `mhpath cur`
##
##  NOTE: This doesn't work on old MH versions with two-digit years.
##  NOTE: drmm CHANGES THE CURRENT MESSAGE, BUT rmm DOESN'T.
##  BUG: If you don't give message number(s) and there's no current
##  message, drmm won't complain until after you enter removal date.
# 
#                             NO WARRANTY
# 
#   BECAUSE THIS PROGRAM IS AVAILABLE FREE OF CHARGE, THERE IS NO WARRANTY
# FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
# OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
# PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
# OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
# TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
# PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
# REPAIR OR CORRECTION.
# 
#   IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
# WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
# REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
# INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
# OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
# TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
# YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
# PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGES.


# UNCOMMENT THE RIGHT LINE FOR YOUR UNIX:
echo="echo -n" nnl=     # BSD
# echo=echo      nnl="\c"   # SYSV
# echo="echo -n" nnl=   PATH=/usr/bin:$PATH; export PATH    # SunOS

field=X-remove-after    # Field to add to header
myname="`basename $0`"
verbose=y               # unset by -noverbose switch

case "$1" in
-nov*)  verbose= ;;
-h*)    # Give help and RCS version number, as a multiline message, then exit:
    echo "Usage: $myname [-help|-nov(erbose)] [msgs] [+/@folder]

You'll be prompted for date after which message(s) should be removed.  Enter:

- RETURN to remove today (usually overnight)

- A one or two-digit date to remove on that date this month
  Example: if today is 12 July, enter >> 14 << to remove on or after 14 July.

- A date plus a month (with space between) to remove then during this year
  Month should be a three-character abbreviation, like
      jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
      Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec
  Example: if today is 12 July, enter >> 6 aug << to remove on 6 August.

- A date, month and year to remove then.  If you use a 2-digit year,
  the message will be removed in this century.  Or use a 4-digit year.
  Example: if this is 1997, enter >> 4 jul 98 << to remove on/after 4 July 1998.

\$Id: drmm 2.1 1997/09/25 03:16:25 jpeek book5 $"
    exit 0
    ;;
-*) echo "$myname: I don't understand '$1'.  For help, type '$myname -help'" 1>&2; exit 1 ;;
esac

# Put date in $dtoday, month in $mtoday, year in $ytoday, and all in $today.
# NOTE: IF date COMMAND DOESN'T SUPPORT %Y FOR 4-DIGIT YEAR, THIS WILL BOMB.
# Feed to shell with eval command to set those three variables.
# This requires date(1) that understands formatting like %d:
eval `date '+dtoday=%d mtoday=%h ytoday=%Y'` || exit 1
today="$dtoday $mtoday $ytoday"
ctoday=`expr "$ytoday" : '\([0-9][0-9]\)[0-9][0-9]'` || exit 1  # Century

$echo "$myname: type date to remove message (RETURN gives $today):
> $nnl"
read dremove mremove yremove

case "$yremove" in
[0-9][0-9][0-9][0-9]) ;;
[0-9][0-9]) yremove="$ctoday$yremove" ;;
"") yremove="$ytoday" ;;
*) echo "$myname quitting: bad year '$yremove'.  For help, type '$myname -help'" 1>&2; exit 1 ;;
esac

case "$mremove" in
jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec) ;;
Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) ;;
"") mremove="$mtoday" ;;
*) echo "$myname quitting: bad month '$mremove'.  For help, type '$myname -help'" 1>&2; exit 1 ;;
esac

case "$dremove" in
"") # Empty date.  Default to today (ignore month/year set above).
    case "$verbose" in
    y) echo "$myname: message(s) will be removable on/after $today." ;;
    esac
    anno -nodate -noinplace -component $field -text "$today" $*
    exit
    ;;
[0-9]|[012][0-9]|3[01]) ;;
*) echo "$myname quitting: bad date '$dremove'.  For help, type '$myname -help'" 1>&2; exit 1 ;;
esac

case "$verbose" in
y) echo "$myname: message(s) will be removable on/after $dremove $mremove $yremove." ;;
esac
anno -nodate -noinplace -component $field -text "$dremove $mremove $yremove" $*
exit    # RETURN EXIT STATUS FROM anno
