#!/bin/bash

# Copyright (C) 2000 Free Software Foundation, Inc.
#
# Author: Frank Heckenbach <frank@pascal.gnu.de>
#
# This file is part of GNU Pascal.
#
# GNU Pascal 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 2, or (at your option)
# any later version.
#
# GNU Pascal 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 Pascal; see the file COPYING.  If not, write to
# the Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.

if [ "$1" == "-h" ] || [ "$1" == "--help" ]; then
  echo "Usage: $0 [mode]"
  echo "Modes:"
  echo "  -d  List all differences (default)"
  echo "  -m  Create menu entries for missing items"
  echo "  -s  Create skeleton nodes for missing items"
  exit
fi

if [ -z "$GPC_SRCDIR" ]; then
  echo "GPC_SRCDIR must be set" >&2
  exit 1
fi

if [ -e $GPC_SRCDIR/gcc/p/gpc.c ]; then
  cd $GPC_SRCDIR/gcc/p
elif [ -e $GPC_SRCDIR/p/gpc.c ]; then
  cd $GPC_SRCDIR/p
else
  echo "Invalid GPC_SRCDIR" >&2
  exit 1
fi

TMPFILE1=/tmp/c-r-d-1.$$
TMPFILE2=/tmp/c-r-d-2.$$
TMPFILE3=/tmp/c-r-d-3.$$

rm -rf $TMPFILE1 $TMPFILE2 $TMPFILE3

sed < doc/reference.texi -ne '/^@menu/,/^@end menu/{s/^* //;s/::$//p;}' | sort -f > $TMPFILE1

(
  # Keywords
  sed < gpc.gperf -ne '/^%%/,/DUMMY%EOF/s/,.*//p';

  # Keyword combinations that deserve their own reference entry
  echo "to begin"
  echo "to end"
  echo "and then"
  echo "or else"

  # Built-in interfaces
  echo "StandardError"
  echo "StandardInput"
  echo "StandardOutput"

  # Built-in identifiers
  sed < util.c -ne '/KItable.*=/,/;$/{/^[ \t]*{[ \t]*"/{s///;s/".*//p;};}'
  sed < util.c -ne '/.*declare_known_id[ \t]*([ \t]*"/{s///;s/".*//p;}'
) | sort -fu > $TMPFILE2

diff -i -U0 $TMPFILE1 $TMPFILE2 | grep -v -e '^@@' -e '^---' -e '^+++' > $TMPFILE3
sed -ne '/^+/s///p' $TMPFILE3 > $TMPFILE1
sed -ne '/^[^+]/s///p' $TMPFILE3 > $TMPFILE2

if [ -s $TMPFILE2 ]; then
  echo "Superfluous documentation entries:"
  cat $TMPFILE2
  echo ""
fi

if [ -s $TMPFILE1 ]; then
  if [ "$1" == "-m" ]; then
    sed -e 's/.*/* &::/' < $TMPFILE1
  elif [ "$1" == "-s" ]; then
    for id in `cat $TMPFILE1`; do
      cat << EOF
@c ----------------------------------------------------------------------------


@node $id
@unnumberedsec $id
@cindex $id

(Under construction.)

@unnumberedsubsec Syntax

@unnumberedsubsec Description

@unnumberedsubsec Standards

@unnumberedsubsec Example

@unnumberedsubsec See also


EOF
    done
  else
    echo "Missing in documentation:"
    cat $TMPFILE1
  fi
fi

rm -rf $TMPFILE1 $TMPFILE2 $TMPFILE3
