#!/bin/sh

# Extract documentation for GPC's keywords from the gpc.gperf file
# and convert it to Texinfo.
#
# This is not a general-purpose gperf to Texinfo converter. It
# assumes the fields, dialect constants and comments like in
# gpc.gperf.
#
# 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 [ $# != 2 ]; then
  echo "Usage: `basename "$0"` gperf-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' &&
  # NOTE: The `*' at the end of the long regex should be `\?', but not all
  #       `sed's understand `\?'. Since several comments in a row should
  #       not occur, this seems safe.
  "$sed" -ne '
    /^%%$/,${
      s/^A/a/;s/^B/b/;s/^C/c/;s/^D/d/;s/^E/e/;s/^F/f/;s/^G/g/;s/^H/h/;s/^I/i/;
      s/^J/j/;s/^K/k/;s/^L/l/;s/^M/m/;s/^N/n/;s/^O/o/;s/^P/p/;s/^Q/q/;s/^R/r/;
      s/^S/s/;s/^T/t/;s/^U/u/;s/^V/v/;s/^W/w/;s/^X/x/;s/^Y/y/;s/^Z/z/;
      s/[@{}]/@&/g;
      s/<\([^>]*\)>/@samp{\1} (@pxref{\1})/g;
      s/`\([^'"'"']*\)'"'"'/@samp{\1}/g;
      /\(.*\),\(.*\),\(.*\),\(.*\), *\([^ ]*\)\( *\/\* *\(.*[^ ]\) *\*\/\)*/{
        s//@item \1 (\5) (@pxref{\1})\
@cindex \1\
\7/;
        s/STANDARD_PASCAL/SP/;
        s/STANDARD_PASCAL_LEVEL_[01]/SP/;
        s/EXTENDED_PASCAL/EP/;
        s/ISO_PASCAL/SP, 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/GNU_PASCAL/GPC/;
        s/ANY_PASCAL/any/;
        s/S_\([A-Z_]*PASCAL\)/SP, \1/;
        s/S1_\([A-Z_]*PASCAL\)/SP, \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/G_\([A-Z_]*PASCAL\)/GPC, \1/;
        s/V_\([A-Z_]*PASCAL\)/VP, \1/;
        s/B_\([A-Z_]*PASCAL\)/BP, \1/;
        s/, PASCAL//;
        p;
      }
    }
  ' < "$1" &&
  echo '@end table';
} > "$2" || { rm -f "$2"; exit 1; }
