#!/bin/sh

# Very primitive Pascal to Texinfo converter.
#
# Copyright (C) 1998-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"` pascal-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 '@smallexample' &&
  tlc=`"$sed" -ne '
    /implementation/,$d;
    /^end;$/,$d;
    /^ *{ *[^ @$].*}/{/.\{69\}/p;d;};
    /^ *{ *[^ @$]/,/}/{/.\{69\}/p;d;};
  ' "$1"`
  if [ x"$tlc" = x ]; then
    true
  else
    echo "$1: too long comment lines:" >&2
    echo "$tlc" >&2
    false
  fi &&
  "$sed" -e '
    /implementation/,$d;
    /^end;$/,$d;
    s/ *\( external\)/\1/;
    s/ *\( attribute\)/\1/;
    /.\{69\}/{
      s/\(.\{1,67\}[^{([]\)  *\([^'"'"':]\)/\1\
  \2/;
      : loop;
      /\n[^\n]\{69\}/{
        /\(\n[^\n]\{1,67\}[^{([\n]\)  *\([^'"'"':\n][^\n]*\)$/{
          s//\1\
  \2/;
          b loop;
        }
      }
    }
    s/[@{}]/@&/g;
    s/`\([^'"'"']*\)'"'"'/@samp{\1}/g;
  ' "$1" &&
  echo '@end smallexample';
} > "$2" || { rm -f "$2"; exit 1; }
