#!/usr/bin/env csi

(use posix regex-case regex)

(define (get-files #!optional (patt #f))
  (let ((files (glob (or patt "*.scm"))))
    files))

(define (get-deps fname #!optional (data #f))
  (let ((hdat    (or data (make-hash-table)))
	(modname (string-substitute ".scm" "" fname)))
    ;; (print "Processing " fname)
    (hash-table-set! hdat fname '())
    (with-input-from-file fname
      (lambda ()
	(let loop ((l (read-line)))
	  (if (eof-object? l)
	      data
	      (begin
		(regex-case
		 l
		 ("^\\s*\\((import|use)\\s+([^\\s]+)\\).*"
		  (x junk md )(print "\"" md "\" -> \"" modname "\";"))
		 ;; now get entries with prefix
		 ("^\\s*\\((import|use)\\s+\\(prefix\\s+([^\\s]+)\\s+.*\\).*"
		  (x junk md ) (print "\"" md "\" -> \"" modname "\";"))
		 )
		(loop (read-line)))))))))

(define (do-all-mod-files)
  (let ((modfiles (append ;; '("megatest.scm" "dashboard.scm")
			  (get-files "*mod.scm"))))
    (for-each
     (lambda (mfile)
       (print "// " mfile)
       (get-deps mfile))
     modfiles)))

(do-all-mod-files)
(exit)
