#!/usr/bin/env python

from gimpfu import *
import sys

# Get file name from command line
if sys.argv[1]:
    this_file = sys.argv[1]

def python_thumbnailer(this_file):
    
    # Strip of the "file:///" prefix
    if this_file.startswith('file:///'):
        this_file = this_file.replace('file:///', '/')
        
    img = ""
    try:
        img = pdb.gimp_file_load(this_file,this_file)
    except:
        # Oops - we couldn't load the file.  Cowardly bailing out!
        pdb.gimp_quit(0)
        sys.exit()
    try:
        pdb.gimp_file_save_thumbnail(img,this_file)
    except:
        # Uh-oh - we couldn't save.  Cowardly bailing out!
        pdb.gimp_quit(0)
        sys.exit()
    pdb.gimp_image_delete(img)
    pdb.gimp_quit(0)

register(
	"python-fu-thumbnailer",
	"Generating thumbnails",
	"Generating thumbnails for Nautilus via Gimp",
	"Eckhard M. Jaeger",
	"Eckhard M. Jaeger",
	"2007",
	"<Toolbox>/Xtns/Thumbnail a file...",
	"",
	[
		(PF_FILE, "this_file", "File", ""),
	],
	[],
	python_thumbnailer)

main()
