#!/bin/bash

#   FILE: gimp-thumbnailer -- 
# AUTHOR: W. Michael Petullo <mike@flyn.org>
#   DATE: 06 July 2003
# 
# Copyright (C) 2003 W. Michael Petullo <mike@flyn.org>
# All rights reserved.
# 
# 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.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

OUT=/dev/stdout

USAGE="[OPTION]...

  -h, -?   print this message
  -i path  set the input path   [ $IN ]
  -o path  set the output path  [ $OUT ]"

while :;
	do case "$1" in
		-h | "-?" )	
			echo -e usage: ${0##*/} "$USAGE" >&2
			exit 1 ;;
		-i )
			IN=$2
			shift ;;
		-o )
			OUT=$2
			shift ;;
		-?* )
			echo "${0##*/}: unrecognised option: $1" >&2
			exit 1 ;;
		* )
			break ;;
	esac
	shift
done

if [ -z "$IN" -o ! -f "`vfstofs -u "$IN"`" ]; then
        echo "${0##*/}: input file (`vfstofs "$IN"`) does not exist" >&2
	exit 1
fi

# Does not work right because nautilus requires that thumbnail image be
# written to $OUT.  Currently, nautilus think the thumbnail creation fails
# because $OUT is not created.  However, it does succede...you just need to 
# tell nautilus to refresh an extra time.
# /usr/bin/gimp --no-data --no-interface --batch '(let* ((img (car (gimp-file-load 1 "'"`vfstofs -u "$IN"`"'" "'"`vfstofs -u "$IN"`"'")))) (gimp-file-save-thumbnail img "'"`vfstofs -u "$IN"`"'"))' '(gimp-quit 0)'

# This works.  Nautilus does the scaling for us.
/usr/bin/gimp-2.3 --no-data --no-interface --batch '(let* ((img (car (gimp-file-load 1 "'"`vfstofs -u "$IN"`"'" "'"`vfstofs -u "$IN"`"'")))) (file-png-save 1 img (car (gimp-image-active-drawable img)) "'$OUT'" "'$OUT'" 1 9 1 0 0 1 1))' '(gimp-quit 0)'
