#!/bin/sh -
#
#	$Id: recover.in,v 8.9 2001/08/30 13:59:23 skimo Exp $ (Berkeley) $Date: 2001/08/30 13:59:23 $
#
# Script to recover nvi edit sessions.

RECDIR="/var/tmp/vi.recover"
SENDMAIL="/usr/sbin/sendmail"

echo 'Recovering nvi editor sessions.'
sessions_found=""

# Check editor backup files.
vibackup=`echo $RECDIR/vi.*`
if [ "$vibackup" != "$RECDIR/vi.*" ]; then
	for i in $vibackup; do
                # Discard symlinks
                if test -L $i ; then
                        rm -f $i
                        continue
                fi

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

		INUSE="fuser -s $i"
		if test "$INUSE" && $INUSE; 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
		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
                # Discard symlinks
                if test -L $i ; then
                        rm -f $i
                        continue
                fi

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

		INUSE="fuser -s $i"
		if test "$INUSE" && $INUSE; 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
                        sessions_found="yes"
                        owner=`stat --format='%U' $recfile`
                        (su nobody -s /bin/sh -c "$SENDMAIL $owner" < $i &) </dev/null >/dev/null 2>&0
                else
                        rm $i
                fi
	done
fi

if [ -n "$sessions_found" ] ; then 
        echo "done."
else
        echo "none found."
fi


