virecover revision 98184
133965Sjdp#!/bin/sh
233965Sjdp#
338889Sjdp# $NetBSD: virecover,v 1.6 2002/03/22 04:34:00 thorpej Exp $
460484Sobrien# $FreeBSD: head/etc/rc.d/virecover 98184 2002-06-13 22:14:37Z gordon $
533965Sjdp#
633965Sjdp
738889Sjdp# PROVIDE: virecover
838889Sjdp# REQUIRE: mountcritremote
938889Sjdp# BEFORE:  DAEMON
1077298Sobrien# KEYWORD: FreeBSD NetBSD
1138889Sjdp#
1238889Sjdp# XXX: should require `mail'!
1338889Sjdp
1438889Sjdp. /etc/rc.subr
1538889Sjdp
1638889Sjdpname="virecover"
1738889Sjdpstop_cmd=":"
1838889Sjdp
1938889Sjdpcase `${CMD_OSTYPE}` in
2038889SjdpFreeBSD)
2138889Sjdp	start_cmd="virecover_start"
2238889Sjdp	;;
2333965SjdpNetBSD)
2433965Sjdp	command="/usr/libexec/${name}"
2533965Sjdp	;;
2633965Sjdpesac
2733965Sjdp
2833965Sjdpvirecover_start()
2933965Sjdp{
3033965Sjdp	find /var/tmp/vi.recover ! -type f -a ! -type d -delete
3133965Sjdp	vibackup=`echo /var/tmp/vi.recover/vi.*`
3233965Sjdp	if [ "${vibackup}" != '/var/tmp/vi.recover/vi.*' ]; then
3333965Sjdp		echo -n 'Recovering vi editor sessions:'
3433965Sjdp		for i in /var/tmp/vi.recover/vi.*; do
3533965Sjdp			# Only test files that are readable.
3633965Sjdp			if [ ! -r "${i}" ]; then
3733965Sjdp				continue
3833965Sjdp			fi
3933965Sjdp
4033965Sjdp			# Unmodified nvi editor backup files either have the
4133965Sjdp			# execute bit set or are zero length.  Delete them.
4233965Sjdp			if [ -x "${i}" -o ! -s "${i}" ]; then
4333965Sjdp				rm -f "${i}"
4433965Sjdp			fi
4533965Sjdp		done
4633965Sjdp
4733965Sjdp		# It is possible to get incomplete recovery files, if the editor
4833965Sjdp		# crashes at the right time.
4933965Sjdp		virecovery=`echo /var/tmp/vi.recover/recover.*`
5033965Sjdp		if [ "${virecovery}" != "/var/tmp/vi.recover/recover.*" ]; then
5133965Sjdp			for i in /var/tmp/vi.recover/recover.*; do
5233965Sjdp				# Only test files that are readable.
5377298Sobrien				if [ ! -r "${i}" ]; then
5477298Sobrien					continue
5577298Sobrien				fi
5677298Sobrien
5777298Sobrien				# Delete any recovery files that are zero length,
5877298Sobrien				# corrupted, or that have no corresponding backup file.
5977298Sobrien				# Else send mail to the user.
6077298Sobrien				recfile=`awk '/^X-vi-recover-path:/{print $2}' < "${i}"`
6177298Sobrien				if [ -n "${recfile}" -a -s "${recfile}" ]; then
6277298Sobrien					sendmail -t < "${i}"
6377298Sobrien				else
6477298Sobrien					rm -f "${i}"
6577298Sobrien				fi
6677298Sobrien			done
6777298Sobrien		fi
6877298Sobrien		echo '.'
6977298Sobrien	fi
7077298Sobrien}
7177298Sobrien
7277298Sobrienload_rc_config $name
7377298Sobrienrun_rc_command "$1"
7438889Sjdp