# $p0 - X center
# $p1 - Y center
# $p2 - direction (0 CW, 1 CCW)
# $p3 - wipe to color
# $p4_red
# $p4_green
# $p4_blue

&rfx_sendmsg (
  qq{
    (begin
      (when (= $frame $start)
        (define next-angle (make-progressor (/ *pi* 2) 
                                            (if (< (- $end $start) 0)
                                              0
                                              (* 2 *pi* (/ (succ (- $end $start)))) )
                                            (* *pi* 2) ))
        (define last-angle (/ *pi* 2)) )
      (let* ((input-file (string-append "$curtmpdir" DIR-SEPARATOR "$in"))
             (image (car (gimp-file-load RUN-NONINTERACTIVE input-file input-file)))
             (layer (car (gimp-image-get-active-layer image)))
             (b-layer 0) )
        (if (zero? $p3)
          (let* ((input-file2 (string-append "$in2"))
                 (image2 (car (gimp-file-load RUN-NONINTERACTIVE input-file2 input-file2))) )
            (set! b-layer (car (gimp-layer-new-from-drawable 
                                 (car (gimp-image-get-active-layer image2)) 
                                 image )))
            (gimp-image-add-layer image b-layer 1)
            (gimp-image-delete image2) )
          (begin
            (set! b-layer (car (gimp-layer-new image $width $height 
                                               RGBA-IMAGE "b-layer" 100 NORMAL-MODE )))
            (gimp-context-set-foreground (list $p4_red $p4_green $p4_blue))
            (gimp-drawable-fill b-layer FOREGROUND-FILL)
            (gimp-image-add-layer image b-layer 1) ))
        (gimp-image-undo-disable image)
        (let ((angle (next-angle))
              (mask (car (gimp-layer-create-mask layer ADD-WHITE-MASK))) )
          (gimp-layer-add-mask layer mask)
          (let ((p0x $p0)
                (p0y (- (* 2 $height)))
                (p1x (+ $p0 (* 2 (cos angle) $width)))
                (p1y (- $p1 (* 2 (sin angle) $height))) )
            (gimp-context-set-foreground '(0 0 0))
            (unless (< angle *pi*)
              (gimp-rect-select image 0 0 $p0 $p1 CHANNEL-OP-REPLACE FALSE 0)
              (unless (zero? (car (gimp-selection-bounds image)))
                (gimp-edit-fill mask FOREGROUND-FILL) )
              (set! p0x (- (* 2 $width)))
              (set! p0y $p1) )
            (unless (< angle (* (/ *pi* 2) 3))
              (gimp-rect-select image 0 $p1 $p0 (- $height $p1) CHANNEL-OP-REPLACE FALSE 0)
              (unless (zero? (car (gimp-selection-bounds image)))
                (gimp-edit-fill mask FOREGROUND-FILL) )
              (set! p0x $p0)
              (set! p0y (* 2 $height)) )
            (unless (< angle (* *pi* 2))
              (gimp-rect-select image $p0 $p1 (- $width $p0) (- $height $p1) CHANNEL-OP-REPLACE FALSE 0)
              (unless (zero? (car (gimp-selection-bounds image)))
                (gimp-edit-fill mask FOREGROUND-FILL) )
              (set! p0x (* 2 $width))
              (set! p0y $p1) )
            (gimp-free-select image 6 (vector p0x p0y $p0 $p1 p1x p1y)
                              CHANNEL-OP-REPLACE TRUE FALSE 0 )
            (unless (zero? (car (gimp-selection-bounds image)))
              (gimp-edit-fill mask FOREGROUND-FILL) )
            )
          (gimp-selection-none image)
          (when (zero? $p2)
            (gimp-drawable-transform-flip-simple mask 
                                                 ORIENTATION-HORIZONTAL 
                                                 FALSE 
                                                 $p0 
                                                 TRUE ))
          )
        (gimp-image-merge-visible-layers image CLIP-TO-IMAGE)
        (rfx-save-frame image "$out") 
        )
      )
    }
  );
