#!/usr/bin/perl
# Convert a Script-Fu script from the 1.2 API interface to the new 2.0 API.
# Created January 28, 2004 by Kevin Cozens <kcozens@interlog.com>
#
# This script changes references to deprecated functions and constants
# so they conform to the new 2.0 API. If this script is applied to a script
# which already confirms to the 2.0 API, only deprecated constants will
# be updated. No changes will be made to functions which already conform
# to the 2.0 API.
#
# -------------------------------------------------------------------------
# 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., 675 Mass Ave, Cambridge, MA 02139, USA.
# -------------------------------------------------------------------------
#
# Changed on January 29, 2004 by Kevin Cozens <kcozens@interlog.com>
# Modified to change deprecated constants to conform to the 2.0 API.
# Modified to remove 'img' argument from two deprecated function calls.
# Modified to add missing arguments to deprecated gimp-blend function call.
# Modified to add missing argument to plug-in-edge call.
#
# Changed on January 30, 2004 by Kevin Cozens <kcozens@interlog.com>
# Prevented change to plug-in-edge call that already conforms to the 2.0 API.
#
# Changed on February 2, 2004 by Kevin Cozens <kcozens@interlog.com>
# Now handles conversion of constants whether they use _ or -.

#The information in the following array comes from:
#	libgimp/gimpcompat.h
#	app/pdb/procedural_db.c
%functions = (
	"gimp-channel-get-image-id", "gimp-drawable-get-image",
	"gimp-channel-ops-offset", "gimp-drawable-offset",
	"gimp-channel-ops-duplicate", "gimp-image-duplicate",
	"plug-in-randomize", "plug-in-blur-randomize",

	"gimp-brushes-list", "gimp-brushes-get-list",
	"gimp-bucket-fill", "gimp-edit-bucket-fill",
	"gimp-channel-delete", "gimp-drawable-delete",
	"gimp-channel-get-name", "gimp-drawable-get-name",
	"gimp-channel-get-tattoo", "gimp-drawable-get-tattoo",
	"gimp-channel-get-visible", "gimp-drawable-get-visible",
	"gimp-channel-set-name", "gimp-drawable-set-name",
	"gimp-channel-set-tattoo", "gimp-drawable-set-tattoo",
	"gimp-channel-set-visible", "gimp-drawable-set-visible",
	"gimp-color-picker", "gimp-image-pick-color",
	"gimp-convert-grayscale", "gimp-image-convert-grayscale",
	"gimp-convert-indexed", "gimp-image-convert-indexed",
	"gimp-convert-rgb", "gimp-image-convert-rgb",
	"gimp-crop", "gimp-image-crop",
	"gimp-drawable-bytes", "gimp-drawable-bpp",
	"gimp-drawable-image", "gimp-drawable-get-image",
	"gimp-gradients-get-active", "gimp-gradients-get-gradient",
	"gimp-gradients-set-active", "gimp-gradients-set-gradient",
	"gimp-image-active-drawable", "gimp-image-get-active-drawable",
	"gimp-image-floating-selection", "gimp-image-get-floating-sel",
	"gimp-layer-delete", "gimp-drawable-delete",
	"gimp-layer-get-linked", "gimp-drawable-get-linked",
	"gimp-layer-get-name", "gimp-drawable-get-name",
	"gimp-layer-get-tattoo", "gimp-drawable-get-tattoo",
	"gimp-layer-get-visible", "gimp-drawable-get-visible",
	"gimp-layer-mask", "gimp-layer-get-mask",
	"gimp-layer-set-linked", "gimp-drawable-set-linked",
	"gimp-layer-set-name", "gimp-drawable-set-name",
	"gimp-layer-set-tattoo", "gimp-drawable-set-tattoo",
	"gimp-layer-set-visible", "gimp-drawable-set-visible",
	"gimp-palette-refresh", "gimp-palettes-refresh",
	"gimp-patterns-list", "gimp-patterns-get-list",
	"gimp-temp-PDB-name", "gimp-procedural-db-temp-name",
	"gimp-undo-push-group-end", "gimp-image-undo-group-end",
	"gimp-undo-push-group-start", "gimp-image-undo-group-start",
);

#The information in the following array comes from:
#	plug-ins/script-fu/siod/siod-wrapper.c
%constants = (
	"NORMAL", "NORMAL-MODE",
	"DISSOLVE", "DISSOLVE-MODE",
	"BEHIND", "BEHIND-MODE",
	"MULTIPLY", "MULTIPLY-MODE",
	"SCREEN", "SCREEN-MODE",
	"OVERLAY", "OVERLAY-MODE",
	"DIFFERENCE", "DIFFERENCE-MODE",
	"ADDITION", "ADDITION-MODE",
	"SUBTRACT", "SUBTRACT-MODE",
	"DARKEN[-_]ONLY", "DARKEN-ONLY-MODE",
	"LIGHTEN[-_]ONLY", "LIGHTEN-ONLY-MODE",
	"HUE", "HUE-MODE",
	"SATURATION", "SATURATION-MODE",
	"COLOR", "COLOR-MODE",
	"VALUE", "VALUE-MODE",
	"DIVIDE", "DIVIDE-MODE",

	"BLUR", "BLUR-CONVOLVE",
	"SHARPEN", "SHARPEN-CONVOLVE",

	"RGB_IMAGE", "RGB-IMAGE",
	"RGBA_IMAGE", "RGBA-IMAGE",
	"GRAY_IMAGE", "GRAY-IMAGE",
	"GRAYA_IMAGE", "GRAYA-IMAGE",
	"INDEXED_IMAGE", "INDEXED-IMAGE",
	"INDEXEDA_IMAGE", "INDEXEDA-IMAGE",

	"WHITE[-_]MASK", "ADD-WHITE-MASK",
	"BLACK[-_]MASK", "ADD-BLACK-MASK",
	"ALPHA[-_]MASK", "ADD-ALPHA-MASK",

	"ADD", "CHANNEL-OP-ADD",
	"SUB", "CHANNEL-OP-SUBTRACT",
	"REPLACE", "CHANNEL-OP-REPLACE",
	"INTERSECT", "CHANNEL-OP-INTERSECT",

	"FG[-_]BG[-_]RGB", "FG-BG-RGB-MODE",
	"FG[-_]BG[-_]HSV", "FG-BG-HSV-MODE",
	"FG[-_]TRANS", "FG-TRANSPARENT-MODE",
	"CUSTOM", "CUSTOM-MODE",

	"FG[-_]IMAGE[-_]FILL", "FOREGROUND-FILL",
	"BG[-_]IMAGE[-_]FILL", "BACKGROUND-FILL",
	"WHITE[-_]IMAGE[-_]FILL", "WHITE-FILL",
	"TRANS[-_]IMAGE[-_]FILL", "TRANSPARENT-FILL",

	"APPLY", "MASK-APPLY",
	"DISCARD", "MASK-DISCARD",

	"HARD", "BRUSH-HARD",
	"SOFT", "BRUSH-SOFT",

	"CONTINUOUS", "PAINT-CONSTANT",
	"INCREMENTAL", "PAINT-INCREMENTAL",

	"HORIZONTAL", "ORIENTATION-HORIZONTAL",
	"VERTICAL", "ORIENTATION-VERTICAL",
	"UNKNOWN", "ORIENTATION-UNKNOWN",

	"LINEAR", "GRADIENT-LINEAR",
	"BILINEAR", "GRADIENT-BILINEAR",
	"RADIAL", "GRADIENT-RADIAL",
	"SQUARE", "GRADIENT-SQUARE",
	"CONICAL[-_]SYMMETRIC", "GRADIENT-CONICAL-SYMMETRIC",
	"CONICAL[-_]ASYMMETRIC", "GRADIENT-CONICAL-ASYMMETRIC",
	"SHAPEBURST[-_]ANGULAR", "GRADIENT-SHAPEBURST-ANGULAR",
	"SHAPEBURST[-_]SPHERICAL", "GRADIENT-SHAPEBURST-SPHERICAL",
	"SHAPEBURST[-_]DIMPLED", "GRADIENT-SHAPEBURST-DIMPLED",
	"SPIRAL[-_]CLOCKWISE", "GRADIENT-SPIRAL-CLOCKWISE",
	"SPIRAL[-_]ANTICLOCKWISE", "GRADIENT-SPIRAL-ANTICLOCKWISE",

	"VALUE[-_]LUT", "HISTOGRAM-VALUE",
	"RED[-_]LUT", "HISTOGRAM-RED",
	"GREEN[-_]LUT", "HISTOGRAM-GREEN",
	"BLUE[-_]LUT", "HISTOGRAM-BLUE",
	"ALPHA[-_]LUT", "HISTOGRAM-ALPHA"
);


while (<>)
{
	#Remove img parameter
	if ($_ =~ /gimp-image-add-layer-mask/)
	{
		$_ =~ s/([(\s])gimp-image-add-layer-mask\s+\S+/\1gimp-layer-add-mask/g;
	}

	#Remove img parameter
	if ($_ =~ /gimp-image-remove-layer-mask/)
	{
		$_ =~ s/([(\s])gimp-image-remove-layer-mask\s+\S+/\1gimp-layer-remove-mask/g;
	}

	#Add two extra arguments (reverse, and dither)
	if ($_ =~ /gimp-blend/)
	{
		$_ =~ s/([(\s])gimp-blend(\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+)(\s+\S+\s+\S+\s+\S+)/\1gimp-edit-blend\2 FALSE\3 FALSE/g;
	}

	#Add extra argument (edgemode)
	if ($_ =~ /plug-in-edge/)
	{
		$_ =~ s/([(\s]plug-in-edge\s+\S+\s+\S+\s+\S+\s+\S+\s+[-\w]+)(\s*\))/\1 0\2/g;
	}

	#Replace deprecated functions with the name of a current function
	foreach $old_function (keys(%functions))
	{
		$_ =~ s/$old_function/$functions{$old_function}/g;
	}

	#Replace deprecated constants with the name of a current constant
	foreach $old_constant (keys(%constants))
	{
		#To avoid an incorrect match, check for white space or a bracket
		#before and after the constant to ensure we don't falsely match
		#a portion of some other constant.
		$_ =~ s/([(\s])$old_constant([)\s])/\1$constants{$old_constant}\2/g;
	}

	#This bit of code to change certain SF-VALUE lines comes from
	#the convert-script file written by Sven Neumann <sven@gimp.org>.
	#So far, this block does the following:
	# - Use SF-STRING instead of SF-VALUE where appropriate
	#
	#Could this be extended to change SF-VALUE or SF-STRING to
	#either SF-GRADIENT or SF-PALETTE as appropriate?
	if (/SF-VALUE/) {
		if (s/\"\\\"(.*)\\\"\"/\"$1\"/) {
			s/SF-VALUE/SF-STRING/;
		}
	}

	print ($_);
}