#!/bin/sh
#	@(#)recover.script	8.7 (Berkeley) 8/16/94
#
# Script to recover nvi edit sessions.
#
PATH=/sbin:/usr/sbin:/bin:/usr/bin
RECDIR=/var/tmp/vi.recover
SENDMAIL=/usr/sbin/sendmail


[ ! -x "$SENDMAIL" ] && exit 0
[ ! -d "$RECDIR" ] && exit 0

case "$1" in
  start)
    echo -n 'Recovering nvi editor sessions... '

    # Check editor backup files.
    vibackup=`echo $RECDIR/vi.*`
    if [ "$vibackup" != "$RECDIR/vi.*" ]; then
    	for i in $vibackup; do
            # Make sure we didn't lose the leading directory. This
            # would only happen if some loser is playing games with
            # embedded spaces in vi recovery file names
            i=$RECDIR/${i#$RECDIR/}
		# Nvi editor backup files should not be symlinks.
		# Delete them
    		if test -L $i ; then
    			rm $i
			continue
    		fi

    		# Only test files that are readable.
    		if test ! -f $i -o ! -r $i; then
    			continue
    		fi

    		# Unmodified nvi editor backup files either have the
    		# execute bit set or are zero length.  Delete them.
    		if test -x $i -o ! -s $i; then
    			rm $i
			continue
    		fi

    	done
    fi

    # It is possible to get incomplete recovery files, if the editor crashes
    # at the right time.
    virecovery=`echo $RECDIR/recover.*`
    if [ "$virecovery" != "$RECDIR/recover.*" ]; then
    	for i in $virecovery; do
            # Make sure we didn't lose the leading directory. This
            # would only happen if some loser is playing games with
            # embedded spaces in vi recovery file names
            i=$RECDIR/${i#$RECDIR/}
		# Discard symlinks
    		if test -L $i ; then
    			rm $i
			continue
    		fi
    		# Only test files that are readable.
    		if test ! -f $i -o ! -r $i; then
    			continue
    		fi

    		# Delete any recovery files that are zero length, corrupted,
    		# or that have no corresponding backup file.  Else send mail
    		# to the user.
    		recfile=`awk '/^X-vi-recover-path:/{print $2}' < $i`
    		if test -n "$recfile" -a -s "$recfile"; then
                        owner=`stat --format='%U' $recfile`
			(su - nobody -c $SENDMAIL $owner < $i &) </dev/null >/dev/null 2>&0
    		else
    			rm $i
    		fi
    	done
    fi

    echo "done."
    ;;
  stop|restart|reload|force-reload)
    ;;
esac

exit 0
