#!/usr/bin/guile -s
!#
(use-modules (ice-9 getopt-long) (ice-9 debug) (srfi srfi-2))

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

(define datadir "../common/")

(load (string-append datadir "compat.scm"))
(load (string-append datadir "ensign.scm"))
(load (string-append datadir "bridge-book.scm"))
(load (string-append datadir "lookout.scm"))

(load (string-append datadir "telegraph.scm"))

;special version of woo-query
(define (woo-query . cmdlist)
  (define (woo-query-single cmd)
    (cdr (run-telegraph (cons cmd (list cmd)))))
  (concatenate
    (map woo-query-single cmdlist)))

(define (enable-telegraph-tracing)
  (set! *telegraph-forward-hook*
    (lambda (name cmd)
      (format #t "DEBUG:~S : received forward message:~S~%"
	      name
	      (cdr cmd))))
  (set! *telegraph-backward-hook*
    (lambda (name cmd)
      (format #t "DEBUG:~S : received backward message:~S~%"
	      name
	      (cdr cmd)))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (usage progname)
  (format #t "Usage:  ~A --map filename~%" progname)
  (format #t "  -h, --help     display help screen~%")
  (format #t "  -v, --version  display version information ~%")
  (format #t "  -c,--config    config file with action descriptions~%~%")
  (format #t "  Report bugs to <inger@altlinux.ru>~%")
  #t)

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

(define option-spec
  '((version (single-char #\v) (value #f))
    (help    (single-char #\h) (value #f))
    (config  (single-char #\c) (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))
(and-let* ((configfile (option-ref options 'config #f)))
          (primitive-load configfile) ; load is a smart and undocumented load-module in guile
	  (quit))
(usage progname)

