#! /bin/sh
# $Id: rmmer,v 4.7 1996/06/09 22:56:55 jerry Exp $
### rmmer, rmmer.noask, rmmer_1, rmmer_1.noask - MH "rmmproc" SCRIPT
### rmmer AND rmmer.noask MOVE MAIL TO @DELETE FOR find TO CLEAN UP
### rmmer_1 AND rmmer_1.noask MOVE MAIL TO +DELETE FOR find TO CLEAN UP
### xmh USERS SHOULD USE A ".noask" VERSION.
##
##  rmmer IS DESIGNED TO BE USED WITH THE MH MAIL rmm COMMAND.
##  INSTEAD OF JUST ADDING A COMMA (,) TO THE MESSAGE NAME, LIKE
##  STANDARD rmm DOES, rmmer MOVES THE MESSAGE INTO A SUB-FOLDER
##  NAMED "DELETE".  A SYSTEM PROGRAM SHOULD CLEAN OUT THAT FOLDER
##  EVERY SO OFTEN (YOU MAY HAVE TO SET THAT UP, TOO).  MESSAGES IN A
##  "DELETE" SUB-FOLDER ARE EASY TO RECOVER IF YOU rmm'ED ACCIDENTALLY.
##
##  FOR EXAMPLE, LET'S SAY YOU JUST DELETED A MESSAGE BY ACCIDENT.
##  TO GET IT BACK, YOU GO TO THE SUB-FOLDER.  YOUR MESSAGE WILL
##  BE THE LAST ONE IN THE SUB-FOLDER BECAUSE YOU JUST REMOVED IT.
##  TO RECOVER THE DELETED MESSAGE, MOVE IT BACK TO THE PARENT FOLDER
##  (WHERE IT WAS BEFORE) WITH refile.  AFTER YOU RECOVER IT, IT'LL
##  BE THE LAST MESSAGE IN THE FOLDER:
##      % rmm
##      % show last @DELETE
##      (Message inbox/DELETE:25)
##          (message appears -- this is the one you "deleted")
##      % refile @..
##      % show last @..
##      (Message inbox:54)
##          (same message appears -- now it's back in the parent folder)
##
##  IF YOU DON'T WANT rmmer TO USE A SUB-FOLDER--AND, INSTEAD, PUT
##  ALL THE MESSAGES IN A CENTRAL "DELETE" FOLDER--YOU CAN CALL THE
##  PROGRAM WITH THE NAME rmmer_1 AND THAT'LL DO IT.
##
##  IF YOU PUT A FILE NAMED .rmmer.ask IN A FOLDER, THAT WILL MAKE
##  rmmer PROMPT BEFORE REMOVING ANY MESSAGES THERE.  IT WON'T
##  PROMPT WHEN THE STANDARD INPUT ISN'T COMING FROM A TERMINAL OR
##  WHEN YOU USE THE VERSIONS NAMED rmmer.noask AND rmmer_1.noask.
##  xmh USERS NEED A .noask VERSION TO KEEP xmh FROM HANGING!
##
##  TO INSTALL THIS, PUT IN A FILE NAMED rmmer, THEN MAKE THREE LINKS
##  (SYMBOLIC OR STANDARD): rmmer_1, rmmer.noask, AND rmmer_1.noask.
# 
#                             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.


askfile=.rmmer.ask  # IF THIS FILE EXISTS IN FOLDER, ASK BEFORE REMOVING
moveto=DELETE       # NAME OF FOLDER FOR DELETED MESSAGES
myname=rmmer        # NAME FOR PROMPTS, FASTER/CLEANER THAN basename $0

mhbin=/usr/local/mh mkdir=/bin/mkdir touch=/bin/touch # SET FOR YOUR SYSTEM
profile=/tmp/RMMERp$$ context=/tmp/RMMERc$$
folder="`$mhbin/folder -fast`" || exit
mhdir="`$mhbin/mhpath +`" || exit

# 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

# PROGRAM NAME SETS DESTINATION FOLDER, WHETHER TO LOOK FOR $askfile:
case "$0" in
*rmmer)         destfol="@$moveto" ask=ok ;;
*rmmer.noask)   destfol="@$moveto" ask=no ;;
*rmmer_1)       destfol="+$moveto" ask=ok ;;
*rmmer_1.noask) destfol="+$moveto" ask=no ;;
*)  echo "$0 aborting: can't find my name." 1>&2; exit 1 ;;
esac

stat=1  # DEFAULT EXIT STATUS; RESET TO 0 BEFORE NORMAL EXIT
trap '/bin/rm -f $profile $context; exit $stat' 0
trap 'echo "$0: ouch! $* may not be removed." 1>&2; exit' 1 2 15

# TO AVOID ENDLESS LOOPS WHERE THE refile IN rmmer RUNS rmmproc,
# MAKE TEMPORARY MH PROFILE TO BYPASS USER'S rmmproc: rmmer.
# rmm SETS SHELL'S DIRECTORY TO THE FOLDER WITH THE MESSAGES.
# STORE $folder IN CONTEXT FILE SO refile CAN FIND FOLDER.
# rmm UPDATES THE SEQUENCES BEFORE THE rmmproc RUNS, SO
# THIS rmmproc CAN HIDE SEQUENCE FILE FROM refile.
/bin/cat > $profile << ENDPROF || exit
Path: $mhdir
rmmproc: /bin/rm
mh-sequences:
context: $context
ENDPROF
echo "Current-Folder: $folder" > $context || exit
MH=$profile MHCONTEXT=$context
export MH MHCONTEXT

# IF [OVERHEAD] CONFIG OPTION IS SET, OVERRIDE IT TO USE OUR FILES:
case "${MHFD}X${MHCONTEXTFD}" in
[0-9]X[0-9])
    eval exec $MHCONTEXTFD\< $MHCONTEXT
    eval exec $MHFD\< $MH
    ;;
esac

# IF CURRENT FOLDER ISN'T ALREADY $moveto, WE MIGHT MAKE SUB-FOLDER.
# OTHERWISE, DON'T BURY MESSAGE DEEPER (IT'S PROBABLY BEING UNDELETED).
# THANX TO David Vezie, dv@Sonoma.EDU, FOR THE SUGGESTION, 3/24/92.
case "$folder" in
?*/$moveto)
    # DON'T RUN refile (BELOW) SO MESSAGE WILL STAY HERE.
    stat=0
    exit
    ;;
*)  # rmm SETS CURRENT DIRECTORY TO FOLDER, SO IT'S EASY TO MAKE
    # NEW SUB-FOLDER.  (LET USER MAKE THEIR OWN "+DELETE" ONCE.)
    if [ "$destfol" = "@$moveto" -a ! -d "$moveto" ]
    then $mkdir $moveto || exit
    fi
    ;;
esac

# IF WE SHOULD ASK AND USER ANSWERS "NO", EXIT:
if [ -f $askfile -a -t 0 -a "$ask" = ok ]
then
    $echo "$myname: remove $* in +$folder? [ny](n) $nnl"
    read ans
    case "$ans" in
    [yY]*) ;;   # DO refile BELOW
    *)  exit ;;
    esac
fi
# rmm PUTS SINGLE MESSAGE NUMBERS INTO $* (LIKE 12 13 14).
# UPDATE LAST-MOD TIME SO find -mtime WON'T DELETE TOO SOON.
# (USE "./" BEFORE FIRST NAME SO SYSTEM V touch WON'T THINK IT'S A TIME.)
# USE refile TO "REMOVE" THE MESSAGES.  EXIT WITH refile's STATUS:
$touch ./$* >/dev/null 2>&1
$mhbin/refile $destfol $*
stat=$?
exit
