#!/bin/sh
exec ${GUILE-guile} -s $0 "$@" # -*- scheme -*-
!#
;;; mkinc --- Generate texi @include fragment

;; Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 Thien-Thi Nguyen
;;
;; This file is part of Guile-PG.
;;
;; Guile-PG 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 3 of the License, or
;; (at your option) any later version.
;;
;; Guile-PG 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 Guile-PG; if not, write to the Free Software Foundation,
;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

;;; Commentary:

;; Usage: mkinc QCONS
;;
;; Write to stdout a texinfo fragment comprising some built-in lookup
;; tables in QCONS, typically ‘$(top_srcdir)/src/postgres-qcons.scm’.
;; Render each as a small blurb followed by a texinfo example block.

;;; Code:

(primitive-load (list-ref (command-line) 1))

(define-module (database postgres-qcons))

(use-modules ((ice-9 rdelim) #:select ((write-line . fln))))

(define (symbol->texi sym)
  (let loop ((ls (reverse! (string->list (symbol->string sym)))) (acc '()))
    (if (null? ls)
        (apply string acc)
        (loop (cdr ls)
              (let ((c (car ls)))
                (if (char=? #\@ c)
                    (cons* c c acc)
                    (cons c acc)))))))

(define (pp-ls ls blurb)
  (fln (symbol->string blurb))
  (fln "@example")
  (let loop ((col 0) (ls ls))
    (if (null? ls)
        (newline)
        (let* ((str (symbol->texi (car ls)))
               (len (string-length str)))
          (cond ((< 30 (+ col len))
                 (set! col (1- (- len)))
                 (newline)))
          (display str)
          (display "   ")
          (loop (+ col 3 len) (cdr ls)))))
  (fln "@end example"))

;; do it!
(pp-ls *infix-operations* '#{
These are the infix operators.
}#)

(pp-ls *postfix-operations* '#{
These are the postfix operators.
}#)

(pp-ls (map car *display-aliases*) '#{
Display aliases exist for these symbols.
}#)

(pp-ls (hash-fold (lambda (k v acc) (cons k acc)) '() ==kw-over-commas) '#{
These functions render differently depending on the presence of keyword args.
}#)

;;; mkinc ends here
