#!/bin/sh
#	This file is part of the Concurrent Versions System CVS.
#	Written by Dick Grune, Vrije Universiteit, Amsterdam.
#	$Header: GC,v 3.1 89/09/25 16:31:29 dick Exp $

#
#		G a r b a g e   C o l l e c t i o n
#	Collects garbage, dust & dead wood; does consistency checks.
#	Should be called after crashes while a CVS-program was running,
#	and other mishaps.
#
Name=GC; export Name

# CVSBIN, CVSLIB and RCSBIN directories
CVSBIN=/home/top/dick/cvs
CVSLIB=/home/top/dick/cvs
RCSBIN=${RCSBIN-/usr/local/bin}
export CVSBIN CVSLIB RCSBIN

# avoid spurious identifications
PATH=${CVSPATH-/bin:/usr/bin}; export PATH

# determine the name of the repository
Repository=""
. $CVSLIB/NR.aux

Tmp1=CVS.adm/\#GC.1
Tmp2=CVS.adm/\#GC.2

# are there duplicates in the CVS.adm/Entries file?
cat CVS.adm/Entries |
sed 's/.* \(.*\)|/\1/' |
sort |
uniq -c |
grep -v ' 1 ' >$Tmp1
if	# this yielded something
	[ -s $Tmp1 ]
then	# report
	echo $Name: duplicates in CVS.adm/Entries:
	cat $Tmp1
fi

# try to find garbage in CVS.adm
ls CVS.adm |
sed '
	/^Entries$/d
	/^Entries.Backup$/d
	/^Files$/d
	/^Mod$/d
	/^LastUpdate$/d
	/^Repository$/d
	/^#GC.[12]$/d
' >$Tmp1
if	# this yielded something
	[ -s $Tmp1 ]
then	# report
	echo $Name: garbage in directory CVS.adm:
	cat $Tmp1 |
	sed 's/^/	CVS.adm\//'
	echo ''
fi

# try to find locked files in the repository
(
	cd $Repository
	if	# there are RCS files there
		[ "`echo *,v`" != "*,v" ]
	then	# check them
		$RCSBIN/rlog -Lh *,v
	fi
	if	# there are hidden RCS files there
		[ "`echo .*,v`" != ".*,v" ]
	then	# check them too
		$RCSBIN/rlog -Lh .*,v
	fi
)

# try to find garbage left-over by RCS
(
	ls -d ,* $Repository/,* 2>&1
) |
sed '
	/not found/d
' >$Tmp1
if	# this yielded something
	[ -s $Tmp1 ]
then	# report
	echo $Name: RCS left-overs found:
	cat $Tmp1 |
	sed 's/^/	/'
	echo ''
fi

# try to find left-over locks & flags
echo $Repository/\#cvs.* |
grep -v '\*' >$Tmp1
if	# this yielded something
	[ -s $Tmp1 ]
then	# such garbage exists
	echo $Name: left-over flags and locks found:
	cat $Tmp1 |
	sed 's/^/	/'
	echo ''
fi

# If there is a $Header in the user file, see if it has the same
# version number as in the administration
for User in `$CVSLIB/FN.aux $Repository`
do
	. $CVSLIB/VT.aux	# sets $VN_User, $VN_Rcs, $TS_User, $TS_Rcs
	. $CVSLIB/HN.aux	# sets $HN_User
	
	if	# there was a $Header
		[ "$HN_User" != "" ] \
	&&	# it is not equal to the one in Entries
		[ "$HN_User" != "$VN_User" ]
	then
		echo $Name: $User has version number $VN_User \
			in administration and $HN_User in Header
	fi
done

rm -f $Tmp1 $Tmp2
