xref: /freebsd/contrib/nvi/files/recover.in (revision 3c4ba5f55438f7afd4f4b0b56f88f2bb505fd6a6)
1#!/bin/sh
2#
3# Script to recover nvi edit sessions.
4
5RECDIR="@vi_cv_path_preserve@"
6
7[ -d ${RECDIR} ] || exit 1
8find ${RECDIR} ! -type f -a ! -type d -delete
9
10# Check editor backup files.
11vibackup=`echo ${RECDIR}/vi.*`
12if [ "${vibackup}" != '${RECDIR}/vi.*' ]; then
13	echo -n 'Recovering vi editor sessions:'
14	for i in ${RECDIR}/vi.*; do
15		# Only test files that are readable.
16		if [ ! -r "${i}" ]; then
17			continue
18		fi
19
20		# Unmodified nvi editor backup files either have the
21		# execute bit set or are zero length.  Delete them.
22		if [ -x "${i}" -o ! -s "${i}" ]; then
23			rm -f "${i}"
24		fi
25	done
26else exit
27fi
28
29# It is possible to get incomplete recovery files, if the editor crashes
30# at the right time.
31virecovery=`echo ${RECDIR}/recover.*`
32if [ "${virecovery}" != "${RECDIR}/recover.*" ]; then
33	for i in ${RECDIR}/recover.*; do
34		# Only test files that are readable.
35		if [ ! -r "${i}" ]; then
36			continue
37		fi
38
39		# Delete any recovery files that are zero length, corrupted,
40		# or that have no corresponding backup file.  Else send mail
41		# to the user.
42		recfile=`awk '/^X-vi-data: *file;/ { sub(/^.*;/, " "); \
43		    do { if (substr($0,1,1) == " ") print; else exit } \
44		    while(getline) }' < "${i}" | uudecode -mr`
45		if [ -n "${recfile}" -a -s "${recfile}" ]; then
46			sendmail -odb -t < "${i}"
47			echo -n '.'
48		else
49			rm -f "${i}"
50		fi
51	done
52fi
53echo ' done.'
54