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

	     (alterator algo)
	     (alterator telegraph)
	     (alterator transport server-socket)
	     (alterator configd)
	     (alterator daemonize)
	     (alterator pidfile)
	     (alterator ensign))

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

(define (configd-init)
  (or (getenv "ALTERATOR_HTMLDIR")
      (setenv "ALTERATOR_HTMLDIR" "/var/www")))

(define (configd-init-local)
  (setenv "ALTERATOR_HTMLDIR" (string-append "." ":" (getenv "ALTERATOR_HTMLDIR"))))

(define (configd-daemonize)
  (and (check-pid "/var/run/configd.pid")
       (error "Same process already exists"))
  (daemonize "/var/log/configd.log")
  (write-pid "/var/run/configd.pid"))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (usage progname)
  (format #t "Usage:  ~A [-l]~%" progname)
  (format #t "  -l,--local   try to use local files (maps,po,layout,etc.) if available and not daemonize %~%")
  (format #t "  Report bugs to <inger@altlinux.ru>~%")
  (quit))

(define option-spec
  '((help  (single-char #\h) (value #f))
    (local (single-char #\l) (value #f))))

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

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

(alterator-init)
(configd-init)

(if (option-ref options 'local #f)
  (begin (alterator-init-local)
	 (configd-init-local))
  (configd-daemonize))

(telegraph
  (server-socket "/var/run/configd/.sock" "root" "_configd")
  (configd)
  (ensign))

(telegraph-start)
