#!/bin/sh
# drmmer - Remove messages marked by drmm script
# 
#                             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.


PATH=/usr/local/nmh/bin:/bin:/usr/bin:$PATH; export PATH
field=X-remove-after    # Date field that drmm adds to header
myname=`basename $0`    # basename (no pathname) of this script
seq=drmm        # Private sequence of removable messages
temp=/tmp/DRMMER$$  # temporary storage for pick output
trap 'rm -f $temp; exit' 0 1 2 15

# Note: loop doesn't skip read-only folders.  If you have some, it should.
for folder in `folders -fast -recurse`
do
    # Change current folder to $folder; skip folders made by rmmer or RCS:
    case "$folder" in
    DELETE|*/DELETE|RCS|*/RCS) continue ;;
    *)  folder +$folder || exit ;;
    esac

    # Put picked messages in private temporary sequence named $seq.
    # pick writes "no messages match specification" on stderr, but it
    # writes "n hits" on stdout :-(, so merge with 2>&1 and parse
    # out good stuff below.  pick returns non-zero if no match.
    if pick -datefield $field -before today -sequence $seq -nopublic >$temp 2>&1
    then
        echo "$myname removing:"
        scan -form scan.drmm $seq
        rmm $seq || exit 1
    fi
    # Show pick output except "n hits" or "no messages match specification":
    sed -e '/[0-9] hit/d' -e '/pick: no messages match specification/d' $temp 1>&2
done
