#!/bin/sh
#
# Check the consistency of the keys and functions definitions between
# the Zile sources, the Texinfo documentation, and the `zile.1.in' man page.
#
# This script is a big hack, like all shell scripts.

# 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.
# All rights reserved.
#
# This file is part of Zile.
#
# 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.
#
# 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 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

if [ ! -f ../src/tbl_funcs.h ]; then
	echo "Please run this script in the sources directory \`doc/'";
	exit 1
fi

echo "Extracting info..."

name=`basename $0`

TMPFILE1=`mktemp /tmp/$name.XXXXXX` || exit 1
TMPFUNCS=`mktemp /tmp/$name.XXXXXX` || exit 1
TMPVARS=`mktemp /tmp/$name.XXXXXX` || exit 1
TMPFUNCST=`mktemp /tmp/$name.XXXXXX` || exit 1
TMPVARST=`mktemp /tmp/$name.XXXXXX` || exit 1

cat >$TMPFILE1 <<EOF
#define X0(z, c)			z
#define X1(z, c, k1)			z
#define X2(z, c, k1, k2)		z
#define X3(z, c, k1, k2, k3)		z
#define X4(z, c, k1, k2, k3, k4)	z
EOF

cat $TMPFILE1 ../src/tbl_funcs.h | cpp | tr -d " \t\"" \
	| sed -e "/^#/d" -e "/^$/d" >$TMPFUNCS

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

cat $TMPFILE1 ../src/tbl_vars.h | cpp | tr -d " \t\"" \
	| sed -e "/^#/d" -e "/^$/d" >$TMPVARS

cat ../doc/zile.texi | 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 ../doc/zile.texi | 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
