#!/bin/sh -
#
# $Id: chk.flags,v 12.4 2007/11/18 20:47:29 bostic Exp $
#
# Check flag name-spaces.

d=../..
t1=__1
t2=__2

if cc -g -Wall -I.. t.c -o t; then
	:
else
	echo "FAIL: unable to compile test program t.c"
	exit 1
fi

if ./t $d/*/*.[ch] $d/*/*.in > $t1; then
	:
else
	echo "FAIL: test program failed"
	exit 1
fi

echo 'Checking "dbenv" variables with flags other than DB_ENV_XXX'
grep 'dbenv,' $t1 |
sed -e '/DB_ENV_/d' \
    -e '/env_method.c.*, mapped_flags*)/d' \
    -e '/env_region.c.*, flags_orig*)/d' \
    > $t2
[ -s $t2 ] && {
	cat $t2
	exit 1
}

echo 'Checking DB_ENV_XXX flags with variables other than "dbenv"'
grep 'DB_ENV_' $t1 |
sed -e '/dbenv,/d' \
    -e '/(dbenv),/d' \
    > $t2
[ -s $t2 ] && {
	cat $t2
	exit 1
}

echo 'Checking "env" variables with flags other than ENV_XXX'
grep '[^b]env,' $t1 |
sed -e '/[^B]ENV_/d' \
    -e '/env_method.c.*, mapped_flags*)/d' \
    > $t2
[ -s $t2 ] && {
	cat $t2
	exit 1
}

echo 'Checking ENV_XXX flags with variables other than "env"'
grep '[^A-Z_]ENV_' $t1 |
sed -e '/[^b]env,/d' \
    -e '/(env),/d' \
    > $t2
[ -s $t2 ] && {
	cat $t2
	exit 1
}

echo 'Checking dbenv "verbose" field with flags other than DB_VERB_XXX'
grep -- 'dbenv->verbose,' $t1 |
sed -e '/DB_VERB_/d' \
    -e '/env_method.c.*, which)/d' \
    > $t2
[ -s $t2 ] && {
	cat $t2
	exit 1
}

echo 'Checking DB_VER_XXX flags with other than dbenv "verbose" field'
grep -- 'DB_VERB_' $t1 |
sed -e '/dbenv->verbose,/d' \
    > $t2
[ -s $t2 ] && {
	cat $t2
	exit 1
}

echo 'Checking "db" variables with flags other than DB_AM_XXX'
cp $t1 /tmp/_f
grep 'dbp,' $t1 |
sed -e '/DB_AM_/d' \
    -e '/dbp, mapped_flag)/d' \
    > $t2
[ -s $t2 ] && {
	cat $t2
	exit 1
}

echo 'Checking DB_AM_XXX flags with variables other than "db"'
grep 'DB_AM_' $t1 |
sed \
    -e '/(&db,/d' \
    -e '/_method.c:.*outflagsp,/d' \
    -e '/rep_backup.c:.*->flags,/d' \
    -e '/db.c:.*save_flags,/d' \
    -e '/((*[	 ]*db_rep->rep_db)*,/d' \
    -e '/((*[	 ]*dbc)*->dbp,/d' \
    -e '/((*[	 ]*dbc_arg->dbp)*,/d' \
    -e '/((*[	 ]*dbp)*,/d' \
    -e '/((*[	 ]*dbp)*->s_primary,/d' \
    -e '/((D),/d' \
    -e '/((sdbp),/d' \
    -e '/(file_dbp,/d' \
    -e '/(ldbp,/d' \
    -e '/(mdbp,/d' \
    -e '/(pdbp,/d' \
    -e '/(pginfo, /d' \
    -e '/(sdbp,/d' \
    -e '/(subdbp,/d' \
    -e '/fop_util.c:.*(t2dbp,/d' \
    -e '/fop_util.c:.*(tmpdbp,/d' \
    -e '/rep_backup.c.*(rfp,/d' \
    > $t2
[ -s $t2 ] && {
	cat $t2
	exit 1
}

echo 'Checking "dbc" variables flags with flags other than DBC_XXX'
echo Checking DBC flags...
cat $t1 |
grep 'dbc,' |
sed -e '/DBC_/d' \
    > $t2
[ -s $t2 ] && {
	cat $t2
	exit 1
}

echo 'Checking DBC_XXX flags with variables other than "dbc"'
grep 'DBC_' $t1 |
sed -e '/((*dbc)*,/d' \
    -e '/(dbc_arg,/d' \
    -e '/(dbc_c,/d' \
    -e '/(dbc_n,/d' \
    -e '/(dbc_orig,/d' \
    -e '/(opd,/d' \
    -e '/(pdbc,/d' \
    -e '/(sdbc,/d' \
    > $t2
[ -s $t2 ] && {
	cat $t2
	exit 1
}

echo Checking for bad use of macros...
egrep 'case .*F_SET\(|case .*F_CLR\(' $d/*/*.c > $t1
egrep 'for .*F_SET\(|for .*F_CLR\(' $d/*/*.c >> $t1
egrep 'if .*F_SET\(|if .*F_CLR\(' $d/*/*.c >> $t1
egrep 'switch .*F_SET\(|switch .*F_CLR\(' $d/*/*.c >> $t1
egrep 'while .*F_SET\(|while .*F_CLR\(' $d/*/*.c >> $t1
[ -s $t1 ] && {
	echo 'if statement followed by non-test macro'
	cat $t1
	exit 1
}

exit 0
