#!/usr/bin/guile \
-e "(scripts use2dot)" -s
!#
;;; use2dot --- Display module dependencies as a DOT specification

;; 	Copyright (C) 2001,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: use2dot [OPTIONS] [FILE ...]
;;
;; Display to stdout a DOT specification that describes module dependencies
;; in FILEs.
;;
;; A top-level `use-modules' form or a `#:use-module' `define-module'-component
;; results in a "solid" style edge.
;;
;; An `#:autoload' `define-module'-component results in a "dotted" style edge
;; with label "N" indicating that N names are responsible for triggering the
;; autoload.  [The "N" label is not implemented.]
;;
;; A top-level `load' or `primitive-load' form results in a a "bold" style
;; edge to a node named with either the file name if the `load' argument is a
;; string, or "[computed in FILE]" otherwise.
;;
;; Options:
;;  -m, --default-module MOD -- Set MOD as the default module (for top-level
;;                              `use-modules' forms that do not follow some
;;                              `define-module' form in a file).  MOD should be
;;                              be a list or `#f', in which case such top-level
;;                              `use-modules' forms are effectively ignored.
;;                              Default value: `(guile-user)'.
;;
;;
;; Usage from a Scheme program:
;;   (use2dot options files)
;;
;; Send DOT output to the current output port.  Return value is unspecified.
;; FILES is a list of files.  OPTIONS is either #f or an alist the keys of
;; which correspond to the shell command options (at this time, the solitary
;; `default-module').

;;; Code:

(define-module (scripts use2dot)
  #:autoload (scripts PROGRAM) (HVQC-MAIN)
  #:use-module ((scripts frisk)
                #:select (make-frisker edge-type edge-up edge-down))
  #:export (use2dot))

(define *default-module* '(guile-user))

(define (q s)
  (list "\"" s "\""))                   ; quote

(define (vv pairs)
  (map (lambda (pair)
         (list (car pair) "=" (cdr pair)))
       pairs))

(define (>>header)
  (list "digraph use2dot {\n"
        (map (lambda (s)
               (list "  " s ";\n"))
             (vv `((label . ,(q "Guile Module Dependencies"))
                   ;;(rankdir . LR)
                   ;;(size . ,(q "7.5,10"))
                   (ratio . fill)
                   ;;(nodesep . ,(q "0.05"))
                   )))))

(define (list-interp ls sep)
  (let ((rev (reverse ls)))
    (list (car rev)
          (map (lambda (x)
                 (list sep x))
               (cdr rev)))))

(define (>>body edges)
  (map (lambda (edge)
         (list
          ;; Need to make into a string here because a module name is
          ;; already a list of symbols, which would confuse the walker.
          (simple-format #f "  \"~A\" -> \"~A\"" (edge-down edge) (edge-up edge))
          (cond ((case (edge-type edge)
                   ((autoload) '((style . dotted) (fontsize . 5)))
                   ((computed) '((style . bold)))
                   (else #f))
                 => (lambda (etc)
                      (list " [" (list-interp (vv etc) ",") "]")))
                (else '()))
          ";\n"))
       edges))

(define (>>footer)
  "}\n")

(define (>> edges)
  (letrec ((walk (lambda (x)
                   (cond ((list? x) (for-each walk x))
                         ((string? x) (display x))
                         (else (write x))))))
    (walk (list (>>header)
                (>>body edges)
                (>>footer)))))

(define (use2dot options files)
  (>> (reverse (((apply make-frisker (or options '())) files) 'edges))))

(define (use2dot/qop qop)
  (use2dot `((default-module . ,(or (qop 'default-module) *default-module*)))
           (qop '())))

(define (main args)
  (HVQC-MAIN args use2dot/qop
             '(usage . commentary)
             '(package . "Guile")
             '(option-spec (default-module
                             (single-char #\m)
                             (value #t)))))

;;; use2dot ends here
