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