; save_and_export.scm
; by Rob Antonishen
; http://ffaat.pointclark.net

; Version 1.2 (20091005)

; Changes:
; v1.1 added messagebox handler
; v1.2 added second script to allow save of xcf files or save-as to allow specifying the xcf

; Description
; Saves the current XCF file as well as a png and jpg in the 
; same directory with the same name  It uses the defaults for png and jpg
; and will overwrite any file already there.
;
; 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.
;
; The GNU Public License is available at
; http://www.gnu.org/copyleft/gpl.html

(define (save-as-and-export img inLayer inDir inFileRoot exportpng? exportjpg?)
  (let* 
    (
      (shortname (string-append inFileRoot ".xcf"))
      (fullname (string-append inDir DIR-SEPARATOR shortname))
      (realfullname (car (gimp-image-get-filename img)))
      (dupimage (car (gimp-image-duplicate img)))
      (temp 0)
  	)

    ;save the xcf file
    (gimp-progress-set-text (string-append "Saving: " shortname))
    (gimp-xcf-save 0 img (car (gimp-image-get-active-drawable img)) fullname shortname)

    ;reload the xcf - this is necessary if the original file was not an xcf or if the filname/path was changed!
    (set! temp (car (gimp-file-load RUN-NONINTERACTIVE fullname shortname)))
    (gimp-displays-reconnect img temp)
    (gimp-image-clean-all temp)

    ;remove filename from fullname to get the path
    (set! fullname (substring fullname 0 (- (string-length fullname) (string-length shortname))))
     
    ;strip old extension
    (while (not (string=? (substring shortname (- (string-length shortname) 1)) "."))
      (set! shortname (substring shortname 0 (- (string-length shortname) 1)))
    )
    (set! shortname (substring shortname 0 (- (string-length shortname) 1)))
        
    (gimp-image-merge-visible-layers dupimage CLIP-TO-IMAGE)
        
    (when (equal? exportpng? TRUE)
      ;save the png file
      (gimp-progress-set-text (string-append "Saving: " (string-append shortname ".png")))
      (file-png-save-defaults RUN-NONINTERACTIVE dupimage (car (gimp-image-get-active-drawable dupimage)) 
                      (string-append fullname shortname ".png")
                      (string-append shortname ".png")))

    (when (equal? exportjpg? TRUE)
      ;save the jpeg file
      (gimp-progress-set-text (string-append "Saving: " (string-append shortname ".jpg")))
      (gimp-file-save RUN-NONINTERACTIVE dupimage (car (gimp-image-get-active-drawable dupimage)) 
                      (string-append fullname shortname ".jpg")
                      (string-append shortname ".jpg")))
            
    ;clean up
    (gimp-image-delete dupimage) 
  )
)

(script-fu-register "save-as-and-export"
        		    "<Image>/File/Save-As XCF and Export..."
					      "Saves the current file as a specified XCF and exports a png and jpg as selected."
		      			"Rob Antonishen"
				      	"Rob Antonishen"
		      			"Sept 2009"
				      	"RGB* GRAY* INDEXED"
		      			SF-IMAGE      "image"      0
		      			SF-DRAWABLE   "drawable"   0
                SF-DIRNAME    "Save-as Directory" ""
                SF-STRING     "Save-as filename (without .xcf)" "filename"
				      	SF-TOGGLE     "Export a png" TRUE
			      		SF-TOGGLE     "Export a jpg" TRUE
)				

(define (save-and-export img inLayer exportpng? exportjpg?)
  (let* 
    (
      (shortname (car (gimp-image-get-name img)))
      (fullname (car (gimp-image-get-filename img)))
      (handler (car (gimp-message-get-handler)))
    )

    (gimp-message-set-handler MESSAGE-BOX)

    (if (= (string-length fullname) 0)
      (gimp-message "The file must be saved before a backup can be made.")
      (if (not (string-ci=? (substring shortname (- (string-length shortname) 4)) ".xcf"))
        (gimp-message "The file must be an xcf.")
        (save-as-and-export img inLayer 
                        (substring fullname 0 (- (string-length fullname) (string-length shortname)))
                        (substring shortname 0 (- (string-length shortname) 4))
                        exportpng? exportjpg?)
      )
    )
    (gimp-message-set-handler handler)
  )
)

(script-fu-register "save-and-export"
        		    "<Image>/File/Save XCF and Export..."
					      "Re-saves the current XCF and exports a png and jpg as selected."
		      			"Rob Antonishen"
				      	"Rob Antonishen"
		      			"Sept 2009"
				      	"RGB* GRAY* INDEXED"
		      			SF-IMAGE      "image"      0
		      			SF-DRAWABLE   "drawable"   0
				      	SF-TOGGLE     "Export a png" TRUE
			      		SF-TOGGLE     "Export a jpg" TRUE
)