#lang scheme/base
(require (planet bzlib/base)
         (planet bzlib/shp) ;; I want access to $htdocs
         (planet bzlib/file)
         (planet bzlib/os)

(define mxml-base-path (make-parameter "/mxml"))

(define (mxml->string mxml)
  (xexpr->string mxml))

;; determine the path of the mxml-file
(define (mxml-path path)
  (build-path* ($htdocs) (mxml-base-path) path))

(define (compile! path) 
  (sys/call ;; (+:windows "c:\\downloads\\flexsdk\\bin\\mxmlc.exe" "mxmlc")
   (path->string (mxml-path path))
            (path->string (flash-path (string-append path ".swf")))))

;; is there a way to know the caller's file location???
;; we can have (__FILE__) being mapped to the file object (being set during include!)
(define (mxml path #:id (id #f) #:height (height 400) #:width (width 400) 
              . widgets)
  (define (helper)
      (let ((app (apply mx:app widgets)))
            (mxml-path path)
          (lambda (out)
            (display (mxml->string app) out))
          #:exists 'replace)
        (compile! path) ;;
        (flash (string-append path ".swf") #:id id #:height height #:width width)))
  (if (file-exists? (flash-path (string-append path ".swf")))
      (if (> (mtime (__PATH__)) (mtime (flash-path (string-append path ".swf")))) ;; we need to compile...
          (flash (string-append path ".swf") #:id id #:height height #:width width))

 (mxml-base-path (parameter/c path-string?))
 (mxml (->* (path-string?)
            #:rest (listof any/c)