#!/bin/sh
#RC:RUNINRC
#RC:AFTER architecture

# Recover vi editor files.
vibackup=`echo /var/tmp/vi.recover/vi.*`
if [ "$vibackup" != '/var/tmp/vi.recover/vi.*' ]; then
	echo 'Recovering vi editor sessions'
	for i in $vibackup; do
		# Only test files that are readable.
		if test ! -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 -f $i
		fi
	done

	# It is possible to get incomplete recovery files, if the editor
	# crashes at the right time.
	virecovery=`echo /var/tmp/vi.recover/recover.*`
	if [ "$virecovery" != "/var/tmp/vi.recover/recover.*" ]; then
		for i in $virecovery; do
			# Only test files that are readable.
			if test ! -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
				sendmail -t < $i
			else
				rm -f $i
			fi
		done
	fi
fi
