(define (script-fu-sg-zibbafy directory)
  (let loop ((files (cadr (file-glob (string-append directory DIR-SEPARATOR "*.bmp") 1))))
    (unless (null? files)
      (let* ((filename (car files))
             (image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
             (layer (car (gimp-image-get-active-layer image)))
             (mask (car (gimp-layer-create-mask layer ADD-COPY-MASK)))
             (half-width (/ (car (gimp-drawable-width layer)) 2))
             (height (car (gimp-drawable-height layer))) )
        (gimp-layer-add-mask layer mask)
        (gimp-invert mask)
        (gimp-drawable-offset mask FALSE OFFSET-TRANSPARENT (- half-width) 0)
        (gimp-layer-remove-mask layer MASK-APPLY)
        (gimp-image-resize image half-width height 0 0)
        (gimp-layer-resize-to-image-size layer)
        (let ((newname (string-append (car (strbreakup filename ".bmp"))
                                      ".png" )))
          (gimp-file-save RUN-NONINTERACTIVE image layer newname newname) )
        (gimp-image-delete image)
        (loop (cdr files)) ))))        
      
(script-fu-register "script-fu-sg-zibbafy"
  "Zibbafy..."
  "Process BMP files in folder using half of image as mask"
  "Saul Goode"
  "Saul Goode"
  "Feb 2012"
  ""
  SF-DIRNAME    "Directory"    "" )
  
(script-fu-menu-register "script-fu-sg-zibbafy"
  "<Image>/Filters/Misc" )
