streamer.ss
#lang scheme/base
(require (planet bzlib/base)
         (planet bzlib/shp) ;; I want access to $htdocs
         (planet bzlib/file)
         (planet bzlib/os)
         xml
         "mxml.ss"
         "util.ss" 
         )

(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")
   "c:\\downloads\\flexsdk\\bin\\mxmlc"
   (path->string (mxml-path path))
            "-output"
            (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)))
        (call-with-output-file 
            (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...
          (helper)
          (flash (string-append path ".swf") #:id id #:height height #:width width))
      (helper)))

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