#!/usr/bin/guile -s
!#

(use-modules (ice-9 getopt-long)
             (srfi srfi-1)
	     
	     (alterator algo)
	     
             (alterator http template)
             (alterator http html))

(define (print-msg keyword str)
  (write (list (string->symbol keyword) str))
  (newline))

(define (print-value keyword)
  (lambda (options content)
    (let ((value (cond-assoc 'value options)))
      (and value (print-msg keyword value))
      "")))

(define (print-content keyword)
  (lambda (options content)
    (print-msg keyword (apply string-append (filter string? content)))
    ""))

(define (extract-text keyword)
  (lambda(filename)
    (template filename
              (tag: "input" (@ 'type "submit") (print-value keyword))
              (tag: "input" (@ 'type "reset") (print-value keyword))
              (tag: "span" (@ 'translate keyword) ( print-content keyword)))))

(define cmdline (command-line))
(define progname (car cmdline))

(define (usage progname)
  (format #t "Usage:  ~A file1 file2 ...~%" progname)
  (format #t "  -h, --help     display help screen~%")
  (format #t "  -v, --version  display version information ~%")
  (format #t "  -k,--keyword   keyword to extract~%~%")
  (format #t "  Report bugs to <inger@altlinux.ru>~%")
  #t)

(define (version progname) (format #t "~A version 0.1 ~%" progname) #t)

(define option-spec
  '((version (single-char #\v) (value #f))
    (help    (single-char #\h) (value #f))
    (keyword  (single-char #\k) (value #t))))

(define options (getopt-long cmdline option-spec))

(and (option-ref options 'help #f) (usage progname) (quit))
(and (option-ref options 'version #f) (version progname) (quit))

(define keyword (option-ref options 'keyword "_"))

(for-each (extract-text keyword)
          (option-ref options '() '()))
