; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License version 2 or higher
; as published by the Free Software Foundation.

(define (script-fu-sg-process-jpegs-in-folder directory quality)
  (let type-loop ((patterns '(".jpg" ".JPG" ".jpeg" ".JPEG")))
    (unless (null? patterns)
      (let loop ((files (cadr (file-glob (string-append directory 
                                                        DIR-SEPARATOR 
                                                        "*"
                                                        (car patterns) )
                                         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))) )
            (file-jpeg-save RUN-NONINTERACTIVE 
                            image 
                            layer
                            filename 
                            filename 
                            (/ quality 100) ; quality
                            0 ; smoothing 
                            1 ; optimize 
                            1 ; progressive 
                            "" ; comment 
                            0 ; subsmp (0-4)
                            1 ; baseline 
                            0 ; restart 
                            0 ;dct 
                            )
            (gimp-image-delete image)
            )
          (loop (cdr files)) ))
      (type-loop (cdr patterns)) )))
      
(script-fu-register "script-fu-sg-process-jpegs-in-folder"
  "Process JPEGs..."
  "Process JPEGs files in folder saving with Quality = 30"
  "Saul Goode"
  "Saul Goode"
  "June 2012"
  ""
  SF-DIRNAME    "Directory"    "" 
  SF-ADJUSTMENT "Quality (%)" '(30 10 100 1 10 0 0) )
  
(script-fu-menu-register "script-fu-sg-process-jpegs-in-folder"
  "<Image>/Filters/Misc" )
