#!/bin/sh

# Extract documentation for GPC's keywords from their definitions
# and convert it to Texinfo.
#
# Copyright (C) 2000-2005 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 [ $# != 2 ]; then
  echo "Usage: `basename "$0"` input-file-name output-file-name" >&2
  exit 1
fi

# This should work even with a crippled sed ...
dir="`echo "$0" | sed -e 's,\(.\)/*[^/]*$,\1,'`" || exit 1
sed="`"$dir"/find-sed`" || exit 1

{
  echo "@c Generated automatically from `echo "$1" | "$sed" -e 's,.*/,,'`" &&
  echo '@c DO NOT CHANGE THIS FILE MANUALLY!' &&
  echo '' &&
  echo '@table @asis' &&
  "$sed" -e 's/^PREDEF_ID *(\([^,]*\),\([^,)]*\))/PREDEF_KEYWORD (\1, 0, \1)/' "$1" |
  sort |
  "$sed" -ne '
    /^PREDEF_KEYWORD *(/{
      s///;
      h;
      s/,.*/,/;
      s/A/a/g;s/B/b/g;s/C/c/g;s/D/d/g;s/E/e/g;s/F/f/g;s/G/g/g;
      s/H/h/g;s/I/i/g;s/J/j/g;s/K/k/g;s/L/l/g;s/M/m/g;s/N/n/g;
      s/O/o/g;s/P/p/g;s/Q/q/g;s/R/r/g;s/S/s/g;s/T/t/g;s/U/u/g;
      s/V/v/g;s/W/w/g;s/X/x/g;s/Y/y/g;s/Z/z/g;
      G;
      s/[@{}]/@&/g;
      s/<\([^>]*\)>/@samp{\1} (@pxref{\1})/g;
      s/`\([^'"'"']*\)'"'"'/@samp{\1}/g;
      /\([^,]*\),[^,]*, *[^,]*, *\([^ ,)]*\)) *\/\* *\(.*[^ ]\) *\*\//{
        s//@item \1 (\2) (@pxref{\1})\
@cindex \1\
\3/;
        s/NOT_CLASSIC_PASCAL/all except CP/;
        s/CLASSIC_PASCAL_LEVEL_[01]/CP/;
        s/EXTENDED_PASCAL/EP/;
        s/OBJECT_PASCAL/OP/;
        s/UCSD_PASCAL/UCSD/;
        s/BORLAND_PASCAL/BP/;
        s/BORLAND_DELPHI/BD/;
        s/PASCAL_SC/PXSC/;
        s/VAX_PASCAL/VP/;
        s/SUN_PASCAL/SP/;
        s/MAC_PASCAL/MP/;
        s/GNU_PASCAL/GPC/;
        s/ANY_PASCAL/any/;
        s/C_\([A-Z_]*PASCAL\)/CP, \1/;
        s/C1_\([A-Z_]*PASCAL\)/CP, \1/;
        s/E_\([A-Z_]*PASCAL\)/EP, \1/;
        s/O_\([A-Z_]*PASCAL\)/OP, \1/;
        s/U_\([A-Z_]*PASCAL\)/UCSD, \1/;
        s/B_\([A-Z_]*PASCAL\)/BP, \1/;
        s/D_\([A-Z_]*PASCAL\)/BD, \1/;
        s/SC_\([A-Z_]*PASCAL\)/PXSC, \1/;
        s/V_\([A-Z_]*PASCAL\)/VP, \1/;
        s/M_\([A-Z_]*PASCAL\)/MP, \1/;
        s/, PASCAL//;
        p;
      }
    }
  ' &&
  echo '@end table';
} > "$2" || { rm -f "$2"; exit 1; }
