#!/usr/bin/guile \
-e "(scripts prep-Ptexi)" -s
!#
;;; prep-Ptexi --- prepare for foo.Ptexi interaction

;;	Copyright (C) 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: prep-Ptexi [-d DEPSDIR] MAKEFILE [STEM ...]
;;
;; Append to MAKEFILE a line
;;
;;   include ./.deps/STEM.Ptexi
;;
;; for each STEM.  Optional arg "-d DEPSDIR" specifies another
;; directory name to use instead of ".deps", the default.
;; Additionally, create the deps dir and each referenced .Ptexi file
;; if these do not already exist.
;;
;; This program is meant to work around limitations in the GNU Automake
;; worldview that disallow specifying such "include" directives directly.
;; It is meant to be invoked using using post-config.status facilities
;; provided by recent versions of autoconf.  Here's how:
;;
;;  - Create a file w/ a list of all .twerp file stems, one per line.
;;    For example, let's call it ".twerp-list" and assume it lives in
;;    the doc/ref/ subdirectory.
;;
;;  - Add to configure.in:
;;
;;      AC_CONFIG_COMMANDS([twerp-prep],[
;;        guile-tools prep-Ptexi doc/ref/Makefile \
;;                  `cat $srcdir/doc/ref/.twerp-list`
;;      ])
;;
;;    Replace "doc/ref" with the relative path to your documentation
;;    dir, and don't forget to include $srcdir.  In the case where you
;;    have only a handful of stems, you could replace the `cat ...` part
;;    of the example with the stems directly (and forego the .twerp-list
;;    file entirely, as well).
;;
;; This methodology is modelled after Automake's "depcomp" behavior, so
;; it's conceivable that this kind of preparation will eventually become
;; unnecessary as automake is extended to handle twerp processing either
;; directly or via some forthcoming doc-dependency-hook API.

;;; Code:

(define-module (scripts prep-Ptexi)
  #:autoload (scripts PROGRAM) (HVQC-MAIN))

(define (prep-Ptexi depsdir makefile stems)
  (or (file-exists? makefile)
      (error "no such makefile:" makefile))
  (chdir (dirname makefile))
  (or (and (file-exists? depsdir)
           (file-is-directory? depsdir))
      (mkdir depsdir))
  (let ((p (open-file (basename makefile) "a"))) ; hmm, no OPEN_APPEND
    (for-each (lambda (stem)
                (let ((Ptexi (simple-format #f "./~A/~A.Ptexi" depsdir stem)))
                  (or (file-exists? Ptexi)
                      (simple-format (open-output-file Ptexi) "# dummy\n"))
                  (simple-format p "include ~A\n" Ptexi)))
              stems)))

(define (prep-Ptexi/qop qop)
  (let ((makefile (car (qop '())))
        (stems (cdr (qop '()))))
    (prep-Ptexi (or (qop 'depsdir) ".deps") makefile stems))
  #t)

(define (main args)
  (HVQC-MAIN args prep-Ptexi/qop
             '(usage . commentary)
             '(package . "Guile")
             '(option-spec (depsdir (single-char #\d)
                                    (value #t)))))

;;; prep-Ptexi ends here
