; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version.
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.

(define (script-fu-sg-numbered-frames image filedir basename number-of-frames)
  (let* ((layers (vector->list (cadr (gimp-image-get-layers image))))
         (template (car layers))
         (orig-layer (cadr layers))
         (x (car (gimp-drawable-offsets template)))
         (y (cadr (gimp-drawable-offsets template)))
         (font (car (gimp-text-layer-get-font template)))
         (font-size (car (gimp-text-layer-get-font-size template)))
         (number-of-digits (string-length (car (gimp-text-layer-get-text template))))
         )
    (gimp-drawable-set-visible template FALSE)
    (gimp-image-lower-layer-to-bottom image template)
    (let loop ((frame-number 1))
      (unless (> frame-number number-of-frames)
        (gimp-image-set-active-layer image orig-layer)
        (let* ((number-text (string-append (make-string number-of-digits #\0)
                                           (number->string frame-number) ))
               (frame-text (substring number-text (- (string-length number-text) 
                                                     number-of-digits )))
               (filename (string-append filedir DIR-SEPARATOR basename frame-text ".jpg"))
               (text-layer (car (gimp-text-fontname image -1 
                                                    x y 
                                                    frame-text 
                                                    0 TRUE 
                                                    font-size PIXELS
                                                    (car (gimp-context-get-font)) )))
               (frame-layer (car (gimp-layer-copy orig-layer TRUE)))
               )
          (gimp-image-set-active-layer image template)
          (gimp-image-add-layer image frame-layer 1)
          (set! frame-layer (car (gimp-image-merge-down image text-layer CLIP-TO-IMAGE)) )
          (gimp-file-save RUN-NONINTERACTIVE 
                          image 
                          frame-layer
                          filename 
                          filename )
          (gimp-image-remove-layer image frame-layer)
          (loop (+ frame-number 1)) )))
          
    (gimp-image-raise-layer-to-top image template)
    (gimp-drawable-set-visible template TRUE) ))
             
(script-fu-register "script-fu-sg-numbered-frames"
  "Numbered frames"
  "Save image as numbered frames "
  "Saul Goode"
  "saulgoode"
  "April 2012"
  "*"
  SF-IMAGE    "Image"    0
  SF-DIRNAME "Directory" "/home/saul/tmp-frames"
  SF-TEXT "Base filename" "frame_"
  SF-ADJUSTMENT "Number of frames" '( 500 1 2000 1 1 0 1 )
  )
(script-fu-menu-register "script-fu-sg-numbered-frames"
  "<Image>/Filters/Misc"
  )   
