xref: /freebsd/libexec/rc/rc.d/virecover (revision 2e3f49888ec8851bafb22011533217487764fdb0)
1#!/bin/sh
2#
3#
4
5# PROVIDE: virecover
6# REQUIRE: mountcritremote ldconfig
7# BEFORE:  DAEMON
8#
9# XXX: should require `mail'!
10
11. /etc/rc.subr
12
13name="virecover"
14desc="Recover crashed vi sessions"
15rcvar="virecover_enable"
16stop_cmd=":"
17start_cmd="virecover_start"
18
19virecover_start()
20{
21	[ -d /var/tmp/vi.recover ] || return
22	find /var/tmp/vi.recover ! -type f -a ! -type d -delete
23	vibackup=`echo /var/tmp/vi.recover/vi.*`
24	if [ "${vibackup}" != '/var/tmp/vi.recover/vi.*' ]; then
25		echo -n 'Recovering vi editor sessions:'
26		for i in /var/tmp/vi.recover/vi.*; do
27			# Only test files that are readable.
28			if [ ! -r "${i}" ]; then
29				continue
30			fi
31
32			# Unmodified nvi editor backup files either have the
33			# execute bit set or are zero length.  Delete them.
34			if [ -x "${i}" -o ! -s "${i}" ]; then
35				rm -f "${i}"
36			fi
37		done
38
39		# It is possible to get incomplete recovery files, if the editor
40		# crashes at the right time.
41		virecovery=`echo /var/tmp/vi.recover/recover.*`
42		if [ "${virecovery}" != "/var/tmp/vi.recover/recover.*" ]; then
43			for i in /var/tmp/vi.recover/recover.*; do
44				# Only test files that are readable.
45				if [ ! -r "${i}" ]; then
46					continue
47				fi
48
49				# Delete any recovery files that are zero length,
50				# corrupted, or that have no corresponding backup file.
51				# Else send mail to the user.
52				recfile=`awk '/^X-vi-recover-path:/{print $2}' < "${i}"`
53				if [ -n "${recfile}" -a -s "${recfile}" ]; then
54					sendmail -t < "${i}"
55				else
56					rm -f "${i}"
57				fi
58			done
59		fi
60		echo '.'
61	fi
62}
63
64load_rc_config $name
65run_rc_command "$1"
66