#!/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.
;;;

(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))
       (let valid-actions? ((lst (cdr expr)))
	 (cond
	   ((null? lst) #t)
	   ((and (pair? (car lst)) (command? (caar lst)))
	    (valid-actions? (cdr lst)))
	   (else #f)))))

(define (read-radm filename)
  (admiral-read-script filename valid-expr?))

(define (optimize-single-action action)
  (let ((first (car action))
	(second (cdr action)))
    (cons (make-admiral-pattern first) second)))

;precompile regexps for the actions
(define (optimize-actions lst)
  (let loop ((lst lst)
	     (result '()))
    (if (null? lst)
      result
      (loop (cdr lst) (append result (list (optimize-single-action (car lst))))))))

;find and eval single backward command
(define (made-eval ctxt actions)
  (let ((sandbox (make-sandbox 'woo-command (lambda args (write-command args))
			       'match (lambda (num) (admiral-pattern-subexpr (car ctxt) num))
			       'match2 (lambda (num) (admiral-pattern-subexpr (cadr ctxt) num))
			       'all-attrs (lambda () (cdaddr ctxt))
			       'apply apply
			       'string=? string=?
			       'string-append string-append
			       'string-index	string-index
			       'string-length	string-length
			       'substring	substring
			       'string-starts-with? string-starts-with?
			       'if if
			       '= =
			       'or or
			       'cond cond
			       'quote quote)))
    (for-each (lambda (x) (eval x sandbox)) actions)))


;loop for finding and converting backward hoo commands
(define (find-and-eval-backward match-result1 rcmd lst)
  (find-and-eval rcmd lst
		 (lambda (curr rcmd)
		   (admiral-pattern-match? curr rcmd))
		 (lambda (match-result2 rcmd actions)
		   (made-eval (list match-result1 match-result2 rcmd) actions)))
  (write-command '("/rcommander/stop"))
  (force-output (current-output-port))
  (find-and-eval-backward match-result1 (read-command) lst))

;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)
		   (find-and-eval-backward
		     match-result
		     (read-command)
		     (optimize-actions lst)))))

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

;unconvert command using rcommander script
(define (opt-unconvert filename)
  (find-and-eval-forward (read-command) (read-radm filename)))

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

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

