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

	     (srfi srfi-1)
	     (srfi srfi-2)
	     (srfi srfi-13)

	     (alterator exit-handler)

	     (alterator algo)
	     (alterator object)

	     ;for telegraph modules
	     (alterator telegraph)
	     (alterator ensign))

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

(define (go)
 (with-exit-handler telegraph-start))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(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 "  -l,--local     try to use local files (maps,po,layout,etc.) if available ~%~%")
  (format #t "  Report bugs to <inger@altlinux.ru>~%")
  #t)

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

(define option-spec
  '((version (single-char #\v) (value #f))
    (help    (single-char #\h) (value #f))
    (config  (single-char #\c) (value #t))
    (local  (single-char #\l) (value #f))))

(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 *system-layout-dir* "/usr/share/alterator/layouts/")
(define *local-layout-dir* "layouts/")

;;run layout
;;note: load is a smart and undocumented load-module in guile, so we use normal primitive-load
(and-let* ((configfile (option-ref options 'config #f)))
          (with-fluids ((use-local-files (option-ref options 'local #f)))
                       (let ((system-layout (string-append *system-layout-dir* configfile))
                             (local-layout (and (fluid-ref use-local-files)
                                                (string-append *local-layout-dir* configfile))))
                         (with-first-readable (list local-layout system-layout)
                                              (lambda(configfile)
                                                (primitive-load configfile)))))
	  (quit))
(usage progname)
