#!/usr/bin/guile \
-e "(scripts snarf-guile-m4-docs)" -s
!#
;;; snarf-guile-m4-docs --- Parse guile.m4 comments for texi documentation

;; 	Copyright (C) 2002,2003,2004 Free Software Foundation, Inc.
;;
;; This program 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.
;;
;; This program 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 this software; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;
;; As a special exception, the Free Software Foundation gives permission
;; for additional uses of the text contained in its release of GUILE.
;;
;; The exception is that, if you link the GUILE library with other files
;; to produce an executable, this does not by itself cause the
;; resulting executable to be covered by the GNU General Public License.
;; Your use of that executable is in no way restricted on account of
;; linking the GUILE library code into it.
;;
;; This exception does not however invalidate any other reasons why
;; the executable file might be covered by the GNU General Public License.
;;
;; This exception applies only to the code released by the
;; Free Software Foundation under the name GUILE.  If you copy
;; code from other Free Software Foundation releases into a copy of
;; GUILE, as the General Public License permits, the exception does
;; not apply to the code that you add in this way.  To avoid misleading
;; anyone as to the status of such modified files, you must delete
;; this exception notice from them.
;;
;; If you write modifications of your own for GUILE, it is your choice
;; whether to permit this exception to apply to your modifications.
;; If you do not wish that, delete this exception notice.

;;; Author: Thien-Thi Nguyen <ttn@gnu.org>

;;; Commentary:

;; Usage: snarf-guile-m4-docs FILE
;;
;; Grep FILE for comments preceding macro definitions, massage
;; them into valid texi, and display to stdout.  For each comment,
;; lines preceding "^# Usage:" are discarded.  Also discarded are the
;; first line following "Usage" and the last line before "AC_DEFUN".
;;
;; Additionally, lines of the form "# {some text}" are massaged into
;; subsection headers (in this example, "@subsection some text").
;;
;;
;; Usage from a Scheme program:
;;   (snarf-guile-m4-docs file)
;;
;; Output is sent to the current output port.
;;
;;
;; TODO: Generalize.

;;; Code:

(define-module (scripts snarf-guile-m4-docs)
  #:autoload (scripts PROGRAM) (HVQC-MAIN)
  #:autoload (ice-9 rdelim) (read-line)
  #:export (snarf-guile-m4-docs))

(define (display-texi lines)
  (display "@deffn {Autoconf Macro}")
  (for-each (lambda (line)
              (display (cond ((and (>= (string-length line) 2)
				   (string=? "# " (substring line 0 2)))
			      (substring line 2))
			     ((string=? "#" (substring line 0 1))
			      (substring line 1))
			     (else line)))
              (newline))
            lines)
  (display "@end deffn")
  (newline) (newline))

(define (prefix? line sub)
  (false-if-exception
   (string=? sub (substring line 0 (string-length sub)))))

(define (massage-usage line)
  (let loop ((line (string->list line)) (acc '()))
    (if (null? line)
        (list (list->string (reverse acc)))
        (loop (cdr line)
              (cons (case (car line)
                      ((#\( #\) #\,) #\space)
                      (else (car line)))
                    acc)))))

(define (snarf-guile-m4-docs file)
  (let* ((p (open-input-file file))
         (next (lambda () (read-line p))))
    (let loop ((line (next)) (acc #f))
      (or (and (eof-object? line)
               (close-port p))
          (cond ((prefix? line "# {")
                 (simple-format #t "@subsection ~A\n\n"
                                (substring line 3 (string-index line #\})))
                 (loop (next) #f))
                ((prefix? line "# Usage:")
                 ;; call `next' to discard first empty line
                 (next)
                 (loop (next) (massage-usage (substring line 8))))
                ((prefix? line "AC_DEFUN")
                 (display-texi (reverse
                                ;; use `cdr' to discard last empty line
                                (cdr acc)))
                 (loop (next) #f))
                ((and acc (prefix? line "#"))
                 (loop (next) (cons line acc)))
                (else
                 (loop (next) #f)))))))

(define (main args)
  (HVQC-MAIN args (lambda (args)
                    (snarf-guile-m4-docs (cadr args)))
             '(usage . commentary)
             '(package . "Guile")))

;;; snarf-guile-m4-docs ends here
