#lang scheme/base
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; JSMGR: Javascript Manager
;; a Javascript Manager/builder for managing your complex ajax scripts 
;; Bonzai Lab, LLC.  All rights reserved.
;; Licensed under LGPL.
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; - performing additional processing during the installation
;; yc 8/21/2009 - first version 
(require scheme/system

;; post-installer
;; test to see if there is java > v1.4 available.
;; if not - throw an error.
(define (post-installer (collect-path (current-directory)) (package-path (current-directory)))
  (define (satisfy-version? out (match 1.4))
    (let ((ver (regexp-match #px"[^\\d]*(\\d+\\.\\d+).*" out)))
      (cond ((not ver) (values ver #f))
            ((>= (string->number (cadr ver)) match) 
             (values (cadr ver) #t))
            (else (values ver #f)))))
  (define (helper cout) 
    (let-values (((ver match?)
                  (satisfy-version? cout)))
      (if match?
          (display (format "You have Java (version = ~a) installed. Your package should work correctly.\n" ver))
          (error 'wrong-verion "Your Java (version = ~a) does not meet requirement - please upgrade to version > 1.4." ver))))
  (let ((cerr (open-output-bytes)))
      (let ((res 
             (parameterize ((current-error-port cerr)) 
               (system "java -version"))))
        (if (not res) 
            (error 'not-java-installed "We cannot find your Java installation via shell.  Please make sure it is installed (version >= 1.4)")
            ;; there might be nothing there...
            (helper (get-output-string cerr))))))
(provide post-installer)