#!/bin/sh
#
# Check the consistency of the keys and functions definitions between
# the Zile sources and the Texinfo documentation.
#
# This script is a big hack. Invoked as zlint TBL_FUNCS.H TBL_VARS.H ZILE.TEXI

# Copyright (c) 2008 Free Software Foundation, Inc.
# Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 Sandro Sigala.
# Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008 Reuben Thomas.
# Copyright (c) 2004 David Capello.
#
# This file is part of GNU Zile.
#
# GNU Zile is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# GNU Zile is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Zile; see the file COPYING.  If not, write to the
# Free Software Foundation, Fifth Floor, 51 Franklin Street, Boston,
# MA 02111-1301, USA.

ret=0

name=`basename $0`

TMPFILE1=`mktemp /tmp/${name}_temp.XXXXXX` || exit 1
TMPFUNCS=`mktemp /tmp/${name}_funcs.XXXXXX` || exit 1
TMPVARS=`mktemp /tmp/${name}_vars.XXXXXX` || exit 1
TMPFUNCST=`mktemp /tmp/${name}_funcst.XXXXXX` || exit 1
TMPVARST=`mktemp /tmp/${name}_varst.XXXXXX` || exit 1

cat >$TMPFILE1 <<EOF
#define X(z, c, d)	z
EOF

export LC_COLLATE=C # Ensure byte sorting for sort
cat $TMPFILE1 $1 | cpp | tr -d " \t\"" \
	| sed -e "/^#/d" -e "/^$/d" | sort >$TMPFUNCS

cat >$TMPFILE1 <<EOF
#define X(v, d, l, c)	v
EOF

cat $TMPFILE1 $2 | cpp | tr -d " \t\"" \
	| sed -e "/^#/d" -e "/^$/d" >$TMPVARS

cat $3 | awk '
BEGIN { v=0;i=0 }
/^@c LINT FUNC/ { v=1 }
/^@c LINT END FUNC/ { v=0 }
/^@c LINT IGNORE/ { i=1 }
/^@c LINT END IGNORE/ { i=0 }
{ if (v && !i) print }
' | grep "@item" | sed "s/@item //" >$TMPFUNCST

cat $3 | awk '
BEGIN { v=0;i=0 }
/^@c LINT VAR/ { v=1 }
/^@c LINT END VAR/ { v=0 }
/^@c LINT IGNORE/ { i=1 }
/^@c LINT END IGNORE/ { i=0 }
{ if (v && !i) print }
' | grep "@item" | sed "s/@item //" >$TMPVARST

echo "Checking functions..."
for f in `comm -13 $TMPFUNCS $TMPFUNCST`; do
	echo "function $f defined in zile.texi but not in tbl_funcs.h."
        ret=1
done
for f in `comm -23 $TMPFUNCS $TMPFUNCST`; do
	echo "function $f defined in tbl_funcs.h but not in Texinfo."
        ret=1
done

echo "Checking variables..."
for f in `comm -13 $TMPVARS $TMPVARST`; do
	echo "variable $f defined in zile.texi but not in tbl_vars.h."
        ret=1
done
for f in `comm -23 $TMPVARS $TMPVARST`; do
	echo "variable $f defined in tbl_vars.h but not in Texinfo."
        ret=1
done

rm -f $TMPFILE1 $TMPFUNCS $TMPVARS $TMPFUNCST $TMPVARST
exit $ret
