#! /bin/csh -f
#
#	ckrcs <version> <destination> <files>
#
#	look for the following conditions
#	1.  files on the server not mentioned in the Makefile (not fatal)
#	2.  files that are locked under rcs on the server (not fatal)
#	3.  files to be released that are different than rcs (fatal)
#	4.  files to be released that are not under rcs on the server (fatal)
#
if ($#argv < 2) then
    echo "Usage: $0 <version> <destination> <files>"
    exit 1
endif

set nonomatch

set locked=()
set extras=()
set movedrcs=()
set norcsfiles=()
set rcsmods=()
set nonfiles=()

set localdir=$cwd

# make a list of all the files that really exist here
rm -f /tmp/ckrcsFiles$$
foreach file ($argv[3-])
    if (-e $file) then
	echo $file >>/tmp/ckrcsFiles$$
    endif
end

# find if there is a corresponding RCS for all the files mentioned in makefile
foreach file ($argv[3-])
    cd $2
    if (-e RCS/$file,v) then
	cd $localdir
	set locked=($locked `rlog -L -R $file`)
	set head=`rlog -h $file | awk '$1 == "head:" {print int($2) "."; exit}' - `
	if (! -e $file) then
	    echo "checking out file $file"
	    co -q$head $file
	endif
	co -p -q$head $file | cmp -s - $file
	if ($status) then
	    set rcsmods=($rcsmods $file)
	endif
    else
	if (-e $localdir/$file) then
	    set norcsfiles=($norcsfiles $file)
	else
	   if (`echo $file | sed "s/.*\.//"` != sfd) set nonfiles=($nonfiles $file)
	endif	
    endif
end

cd $2

# look in the destination directory for extraneous files
set sfiles=(`/bin/ls -aF | sed -f $0.sed`)
if ($#sfiles != 0) then
    if (-e /tmp/ckrcsFiles$$) then
	foreach file ($sfiles)
	    set extras=($extras `echo $file | fgrep -v -f /tmp/ckrcsFiles$$`)
	end
    else
	set extras=($sfiles)
    endif
endif

rm -f /tmp/ckrcsFiles$$

if ($#extras != 0) then
    echo "files on the server not mentioned in the Makefile (not fatal):"
    foreach file ($extras)
	echo "    $file"
    end
    echo ""
    echo ""
    echo ""
endif

if ($#locked != 0) then
    echo "files that are locked under rcs on the server (not fatal):"
    foreach file ($locked)
	echo "    $file"
    end
    echo ""
    echo ""
    echo ""
endif

if (($#norcsfiles == 0) && ($#nonfiles == 0) && ($#rcsmods == 0)) exit 0

if ($#nonfiles != 0) then
    echo "files listed in Makefile that don't exist (fatal):"
    foreach file ($nonfiles)
	echo "    $file"
    end
    echo ""
    echo ""
    echo ""
endif

if ($#norcsfiles != 0) then
    echo "files to be released that are not under rcs on the server (fatal):"
    foreach file ($norcsfiles)
	echo "    $file"
    end
    echo ""
    echo ""
    echo ""
endif

if ($#rcsmods != 0) then
    echo "files to be released that are different than rcs (fatal)"
    foreach file ($rcsmods)
	echo "    $file"
    end
    echo ""
    echo ""
    echo ""
endif

exit 1
