#!/usr/bin/env guile
!#
(use-modules (ice-9 getopt-long))
(use-modules (ice-9 match))
(use-modules (ice-9 receive))
(use-modules (srfi srfi-1))
(use-modules (srfi srfi-9))
(use-modules (oop goops))
(use-modules (doctest util))
(use-modules (doctest evaluate))
(define +version-message+
    "
        Guile Archivist v.0.1
    ")
(define +help-message+
    "
        GNU Guile Archivist utility generate documenation for toplevel
        definitons in modules, specified in command line and perform
        doc-tests.

        Usage: doctest [OPTIONS] <modules>
        Options description:
            -v, --verbose  Perform actions verbosely
            -V, --version  Print version and exit
            -h, --help     Print this help message and exit

    ")
(define +options+
    (getopt-long
     (command-line) '((version (single-char #\V) (value #f))
		      (help    (single-char #\h) (value #f))
		      (verbose (single-char #\v) (value #f)))))
(define +own-modules+
    (map (lambda (x) (cons 'doctest x)) '((util) (docstring) (evaluate))))

(when [option-ref +options+ 'help #f]
      (display (docstring->text +help-message+))
      (exit))
(when [option-ref +options+ 'version #f]
      (display (docstring->text +version-message+))
      (exit))


(define cmd-modules (option-ref +options+ '() '()))
(define target-modules
    (if (null? cmd-modules) +own-modules+
	cmd-modules))
(for [module-name in target-modules]
     (let ((module (sexp->module module-name)))
	 (if module (begin (format #t "Resolved module ~a\n" module-name)
			   (module-run-doctests module))
	     (format #t "Failed to resovle ~a\n" module-name))))
