#!/bin/sh -
#
# Reconcile the list of public defines with the man pages and the Java files.

d=../..
docs=$d/docs_src

[ -f $d/LICENSE ] || {
	echo 'FAIL: cannot find source distribution directory.'
	exit 1
}

p=$d/dist/pubdef.in

exitv=0

cat <<END_OF_TEXT
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Check that pubdef.in has everything listed in m4.links.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
END_OF_TEXT

f=$docs/m4/m4.links
sed -n \
    -e 's/^\$1, \(DB_[^,]*\).*/\1/p' \
    -e d < $f |
while read name; do
	if `egrep -w "$name" $p > /dev/null`; then
		:
	else
		echo "$f: $name is missing from $p"
		exitv=1
	fi
done

cat <<END_OF_TEXT
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Check that m4.links has everything listed in pubdef.in.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
END_OF_TEXT

f=$docs/m4/m4.links
sed '/^#/d' $p |
while read name isdoc isinc isjava; do
	if `egrep -w "^.1, $name" $f > /dev/null`; then
		[ "X$isdoc" != "XD" ] && {
			echo "$name should not appear in $f"
			exitv=1
		}
	else
		[ "X$isdoc" = "XD" ] && {
			echo "$name does not appear in $f"
			exitv=1;
		}
	fi
done

cat <<END_OF_TEXT
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Check that pubdef.in has everything listed in db.in plus api_flags.in.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
END_OF_TEXT

cat $d/dbinc/db.in $d/dbinc_auto/api_flags.in | sed -n \
    -e 's/^#define[	 ]*\(DB_[A-Z_0-9][A-Z_0-9]*\).*/\1/p' \
    -e 's/^[	 ]*\(DB_[A-Z_]*\)=[0-9].*/\1/p' \
    -e d |
while read name; do
	if `egrep -w "$name" $p > /dev/null`; then
		:
	else
		echo "db.in/api_flags.in: $name is missing from $p"
		exitv=1
	fi
done

cat <<END_OF_TEXT
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Check that api_flags.in plus db.in has everything listed in pubdef.in.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
END_OF_TEXT

sed '/^#/d' $p |
while read name isdoc isinc isjava; do
	if `egrep -w "#define[	 ]$name|[	 ][	 ]*$name=[0-9][0-9]*" \
	    $d/dbinc/db.in $d/dbinc_auto/api_flags.in > /dev/null`; then
		[ "X$isinc" != "XI" ] && {
			echo "$name should not appear in db.in/api_flags.in"
			exitv=1
		}
	else
		[ "X$isinc" = "XI" ] && {
			echo "$name does not appear in db.in/api_flags.in"
			exitv=1
		}
	fi
done

cat <<END_OF_TEXT
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Check that pubdef.in has everything listed in DbConstants.java.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
END_OF_TEXT

j=$d/java/src/com/sleepycat/db
f=$j/internal/DbConstants.java
sed -n -e 's/.*int[	 ]\([^ 	]*\).*;/\1/p' < $f |
while read name; do
	if `egrep -w "$name" $p > /dev/null`; then
		:
	else
		echo "$f: $name is missing from $p"
		exitv=1
	fi
done

cat <<END_OF_TEXT
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Check that DbConstants.java has everything listed in pubdef.in.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
END_OF_TEXT

f=$j/internal/DbConstants.java
sed '/^#/d' $p |
while read name isdoc isinc isjava; do
	if `egrep -w "int[	 ]$name =" $f > /dev/null`; then
		[ "X$isjava" != "XJ" ] && {
			echo "$name should not appear in $f"
			exitv=1
		}
	else
		[ "X$isjava" = "XJ" ] && {
			echo "$name does not appear in $f"
			exitv=1
		}
	fi
done

cat <<END_OF_TEXT
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Check that all constants in pubdef.in are wrapped by the Java API.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
END_OF_TEXT

#Strip out Javadoc comments
t=__1
cat $j/*.java $d/rpc_server/java/*.java \
    $j/internal/Db.java $j/internal/DbEnv.java \
    $j/internal/db_javaJNI.java | sed '/\/\*\*/,/\*\// d' > $t

sed '/^#/d' $p |
while read name isdoc isinc isjava; do
	if `egrep -w "$name" $t > /dev/null`; then
		[ "X$isjava" != "XJ" ] && {
			echo "$name should not appear in the Java API"
			exitv=1
		}
	else
		[ "X$isjava" = "XJ" ] && {
			echo "$name does not appear in the Java API"
			exitv=1
		}
	fi
done

cat <<END_OF_TEXT
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Check that all constants in pubdef.in are wrapped by the Java native layer.
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
END_OF_TEXT

sed '/^#/d' $p |
while read name isdoc isinc isjava; do
	if `egrep -w "$name" $d/libdb_java/db_java_wrap.c > /dev/null`; then
		[ "X$isjava" != "XN" ] && [ "X$isjava" != "XJ" ] && {
			echo "$name should not appear in the Java native layer"
			exitv=1
		}
	else
		[ "X$isjava" = "XN" ] && {
			echo "$name does not appear in the Java native layer"
			exitv=1
		}
	fi
done

exit $exitv
