#!/usr/bin/guile -s
!#
;;;
;;; ALTerator - ALT Linux configuration project
;;;
;;; Copyright (c) 2004,2005 ALT Linux Ltd.
;;; Copyright (c) 2004,2005 Stanislav Ievlev
;;;
;;; This program is free software; you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 2 of the License, or
;;; (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program; if not, write to the Free Software
;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
;;; USA.
;;;
(use-modules (ice-9 popen))

(define datadir "../common/")

(load (string-append datadir "str.scm"))
(load (string-append datadir "command.scm"))
(load (string-append datadir "admiral-pattern.scm"))
(load (string-append datadir "admiral-sandbox.scm"))
(load (string-append datadir "admiral-common.scm"))

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


(define (valid-expr? expr)
  (and (pair? expr) (command? (car expr))))

;read configuration script and return list of blocks from file
(define (read-adm filename)
  (admiral-read-script filename valid-expr?))

;evaluate rest of the expression we read
(define (made-eval ctxt exprs)
  (let ((sandbox (make-sandbox 'hoo-command (lambda args (write-command args))
			       'match (lambda (num) (admiral-pattern-subexpr (car ctxt) num))
			       'all-attrs (lambda () (cddr ctxt))
			       'apply apply
			       'string=? string=?
			       'string-match string-match
			       'string-length string-length
			       'let let
			       '= =
			       'not not
			       'or or
			       'and and
			       'lambda lambda
			       'begin begin
			       'for-each for-each
			       'string-append string-append
			       'line-from-process (lambda (cmdline)
						    (let* ((p (open-input-pipe cmdline))
							   (line (read-line p)))
						      (close-pipe p)
						      (if (eof-object? line)
							""
							line)))
			       'if if
			       'quote quote)))
    (for-each (lambda (x) (eval x sandbox)) exprs)))

;find and eval forward woo command
(define (find-and-eval-forward cmd lst)
  (find-and-eval cmd lst
		 (lambda (curr cmd)
		   (admiral-pattern-match? (make-admiral-pattern curr) cmd))
		 (lambda (match-result cmd lst)
		   (made-eval (cons match-result cmd) lst))))

;write initial information for chooser
(define (opt-init filename) (print-init (read-adm filename)))

;convert command using commander script
(define (opt-convert filename)
    (find-and-eval-forward (read-command) (read-adm filename)))

(define opts (command-line))
(define (usage opts)
  (format #t "usage: ~A <script> init|convert~%" (car opts))) 

(if (< (length opts) 3)
  (usage opts)
  (let ((action (caddr opts)))
   (cond ((string=? action "init") (opt-init (cadr opts)))
	 ((string=? action "convert") (opt-convert (cadr opts)))
	 (else (usage opts)))))

