:
# NAME:
#	chkcvs - check CVS repository is complete
#
# DESCRIPTION:
#	We need to check that all files needed by named(8) have been
#	added to the CVS repository or the live named(8) is going to
#	get upset.
#
#	If the SCM is git we need to make sure if there are any staged
#	files that there are no unstaged changes.
#	
# AUTHOR:
#	Simon J. Gerraty <sjg@crufty.net>

# RCSid:
#	$Id: chkcvs,v 1.11 2023/06/10 20:36:20 sjg Exp $
#
#	@(#) Copyright (c) 1997-2022 Simon J. Gerraty.
#
#	This file is provided in the hope that it will
#	be of use.  There is absolutely NO WARRANTY.
#	Permission to copy, redistribute or otherwise
#	use this file is hereby granted provided that 
#	the above copyright notice and this notice are
#	left intact. 
#      
#	Please send copies of changes and bug-fixes to:
#	sjg@crufty.net
#

Myname=chkcvs

. debug.sh

DebugOn cvs scm

(
. atexit.sh
. source.sh

if [ x$NO_NAMED = x ]; then
	#
	# get a list of files that CVS knows nothing about...
	#
	TF=${TMP:-/tmp}/ccvs$$

	$DEBUG_SKIP atexit "rm -f \$TF*"

	# the -n is necessary to avoid deadlock with cvs commit
	CVS_FLAGS=-n

	# what scm are we actually using?
	. scm.sh

        # this just makes testing easier
        $DEBUG_DO source_rc $ND_DIR/.scm.debug

	case "$SCM" in
	cvs) scm update;;
	git) scm status -s -u;;
	hg) scm status -u;;
	svn) svn status;;
	esac 2>&1 | egrep '^([M ][M ]| *[?A])' > $TF
	[ -s $TF ] || Exit 0

	case "$SCM" in
	git)	# untracked files are bad
                if egrep -q '^M' $TF &&
                        egrep -q '^ M' $TF; then
                        echo "ERROR: staged and unstaged files:"
                        egrep '^[M ][M ]' $TF
                        Exit 1
		fi
                egrep '^ *[?A]' $TF > $TF.x
                [ -s $TF.x ] || Exit 0
                mv $TF.x $TF
                ;;
	cvs)
	        if [ ! -d $PWD/$2 ]; then
        		# yes...

        		#
        		# if these are on our command line - ok
        		# if not, we just warn about them - to avoid deadlock
        		#

        		grep -v '^?' $TF | cut -d' ' -f2 > $TF.new

                	if [ -s $TF.new ]; then
                		args=`echo $* | tr ' ' ','`
                		for file in `cat $TF.new`
                		do
                			case ",$args," in
					*,$file,*) continue;;
					esac
                                        b=`basename $file`
                                        case ",$args," in
					*,$b,*) continue;;
					esac
                			echo "$file: WARNING: needs commit"
				done
			fi
        	fi
        	;;
	esac
	
	#
	# these files had better not be found in .depend
	#
	grep '^?' $TF | cut -d' ' -f2 | sort -u > $TF.unknown

	if [ -s $TF.unknown ]; then
		#
		# make sure .depend is up to date
		#
		${MAKE:-make} depend > /dev/null

		sed -n -e 's/	/ /g' -e 's/^  *\([^ ][^ ]*\).*/\1/p' .depend | sort -u > $TF.need
		comm -12 $TF.unknown $TF.need > $TF.dep
		if [ -s $TF.dep ]; then
			sed "s/\$/: ERROR: unknown to $SCM but needed by named./" $TF.dep
			ExitStatus=1
		fi
	fi
fi
Exit ${ExitStatus:-0} ) >&2

