#!/bin/bash
##########################################################################
#
#  gimp-deprecates     --  Remove deprecated procedure calls from 
#                          Script-Fu scripts for GIMP.
#
#  $Id: gimp-deprecates,v 1.1.0.1 2008/10/02 03:22:58 floyd Exp floyd $
#
##########################################################################
#
#  Copyright 2008 by Floyd L. Davidson, floyd@apaflo.com
#  File created:  Wed Oct  1 19:09:00 2008
#  Last updated:  Thu Oct  2 02:41:31 2008
#
##########################################################################
#
# 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.

rm -rf *.scm.orig

# remove these two for distribution
rm -rf *.scm
cp ../sherman/*scm .

if [ "${1}" == "--help" -o "${1}" == "-h" -o "${1}" == "-?" ] ; then
  echo ""
  echo "Invoke this script with -h, --help or -? to see this screen."
  echo "Invoke this script with -v to cause more verbose updates."
  echo "Invoke this script with -q to cause it to run quietly"
  echo ""
  echo "This script searches every \"*.scm\" file in the current"
  echo "for calls to depricated GIMP procedures, and replaces each"
  echo "with a call to an appropriate procedure.  The original file"
  echo "file will is retained as \"*.scm.orig\"."
  echo ""
  exit 0
fi

verbose=1
if [ "${1}" == "-v" ] ; then verbose=2 ; fi
if [ "${1}" == "-q" ] ; then verbose=0 ; fi

if [ ${verbose} -eq 1 ] ; then
  echo ""
  echo "Invoke this script with -v to cause more verbose updates."
  echo "Invoke this script with -q to cause it to run quietly"
  echo ""
  echo "This script is searching for depricated GIMP procedures"
  echo "in every \"*.scm\" file in the current directory.  The"
  echo "original file will be retained as \"*.scm.orig\"."
  echo ""
fi

#     Procedures in match pairs.  The first will be replaced by the
#     second.  If the second is "NONE_AVAILABLE" no substitution
#     is made, but the procedure is added to the commented list
#     inserted into each modified script to flag modifications.

list=(gimp-drawable-set-image           NONE_AVAILABLE
      gimp-channel-menu-new             NONE_AVAILABLE
      gimp-drawable-menu-new            NONE_AVAILABLE
      gimp-image-menu-new               NONE_AVAILABLE
      gimp-layer-menu-new               NONE_AVAILABLE
      gimp-brush-select-widget-close    NONE_AVAILABLE
      gimp-brush-select-widget-new      NONE_AVAILABLE
      gimp-brush-select-widget-set      NONE_AVAILABLE
      gimp-font-select-widget-close     NONE_AVAILABLE
      gimp-font-select-widget-new       NONE_AVAILABLE
      gimp-font-select-widget-set       NONE_AVAILABLE
      gimp-gradient-select-widget-close NONE_AVAILABLE
      gimp-gradient-select-widget-new   NONE_AVAILABLE
      gimp-gradient-select-widget-set   NONE_AVAILABLE
      gimp-palette-select-widget-close  NONE_AVAILABLE
      gimp-palette-select-widget-new    NONE_AVAILABLE
      gimp-palette-select-widget-set    NONE_AVAILABLE
      gimp-palette-set-default-colors   NONE_AVAILABLE
      gimp-palette-swap-colors          NONE_AVAILABLE
      gimp-pattern-select-widget-close  NONE_AVAILABLE
      gimp-pattern-select-widget-new    NONE_AVAILABLE
      gimp-pattern-select-widget-set    NONE_AVAILABLE
      gimp-progress-install             NONE_AVAILABLE
      gimp-brushes-get-brush            gimp-context-get-brush
      gimp-brushes-get-brush-data       gimp-brush-get-pixels
      gimp-brushes-get-opacity          gimp-context-get-opacity
      gimp-brushes-get-paint-mode       gimp-context-get-paint-mode
      gimp-brushes-get-spacing          gimp-brush-get-spacing
      gimp-brushes-set-brush            gimp-context-set-brush
      gimp-brushes-set-opacity          gimp-context-set-opacity
      gimp-brushes-set-paint-mode       gimp-context-set-paint-mode
      gimp-brushes-set-spacing          gimp-brush-set-spacing
      gimp-bucket-fill                  gimp-edit-bucket-fill
      gimp-channel-ops-offset           gimp-drawable-offset
      gimp-drawable-image               gimp-drawable-get-image
      gimp-flip                         gimp-drawable-transform-flip-simple
      gimp-get-path-by-tattoo           gimp-image-get-vectors-by-tattoo
      gimp-gradients-get-gradient       gimp-context-get-gradient
      gimp-gradients-get-gradient-data  gimp-gradient-get-uniform-samples
      gimp-gradients-sample-custom      gimp-gradient-get-custom-samples
      gimp-gradients-sample-uniform     gimp-gradient-get-uniform-samples
      gimp-gradients-set-gradient       gimp-context-set-gradient
      gimp-image-active-drawable        gimp-image-get-active-drawable
      gimp-image-add-layer-mask         gimp-layer-add-mask
      gimp-image-free-shadow            gimp-drawable-free-shadow
      gimp-image-get-cmap               gimp-image-get-colormap
      gimp-image-remove-layer-mask      gimp-layer-remove-mask
      gimp-image-set-cmap               gimp-image-set-colormap
      gimp-layer-get-name               gimp-drawable-get-name
      gimp-layer-get-preserve-trans     gimp-layer-get-lock-alpha
      gimp-layer-get-visible            gimp-drawable-get-visible
      gimp-layer-mask                   gimp-layer-get-mask
      gimp-layer-set-name               gimp-drawable-set-name
      gimp-layer-set-preserve-trans     gimp-layer-set-lock-alpha
      gimp-layer-set-visible            gimp-drawable-set-visible
      gimp-levels-auto                  gimp-levels-stretch
      gimp-palette-get-background       gimp-context-get-background
      gimp-palette-get-foreground       gimp-context-get-foreground
      gimp-palette-set-background       gimp-context-set-background
      gimp-palette-set-foreground       gimp-context-set-foreground
      gimp-palettes-get-palette         gimp-context-get-palette
      gimp-palettes-get-palette-entry   gimp-palette-entry-get-color
      gimp-palettes-set-palette         gimp-context-set-palette
      gimp-path-delete                  gimp-image-remove-vectors
      gimp-path-get-current             gimp-image-get-active-vectors
      gimp-path-get-locked              gimp-vectors-get-linked
      gimp-path-get-point-at-dist       gimp-vectors-stroke-get-point-at-dist
      gimp-path-get-points              gimp-vectors-stroke-get-points
      gimp-path-get-tattoo              gimp-vectors-get-tattoo
      gimp-path-import                  gimp-vectors-import-from-file
      gimp-path-list                    gimp-image-get-vectors
      gimp-path-set-current             gimp-image-set-active-vectors
      gimp-path-set-locked              gimp-vectors-set-linked
      gimp-path-set-points              vectors-stroke-new-from-points
      gimp-path-set-tattoo              gimp-vectors-set-tattoo
      gimp-path-stroke-current          gimp-edit-stroke-vectors
      gimp-path-to-selection            gimp-vectors-to-selection
      gimp-patterns-get-pattern         gimp-context-get-pattern
      gimp-patterns-get-pattern-data    gimp-pattern-get-pixels
      gimp-patterns-set-pattern         gimp-context-set-pattern
      gimp-perspective                  gimp-drawable-transform-perspective-default
      gimp-rgb-intensity                gimp-rgb-luminance
      gimp-rgb-intensity-uchar          gimp-rgb-luminance-uchar
      gimp-rotate                       gimp-drawable-transform-rotate-default
      gimp-scale                        gimp-drawable-transform-scale-default
      gimp-selection-clear              gimp-selection-none
      gimp-shear                        gimp-drawable-transform-shear-default
      gimp-text                         gimp-text-fontname
      gimp-text-get-extents             gimp-text-get-extents-fontname
      gimp-transform-2d                 gimp-drawable-transform-2d-default
      gimp-undo-push-group-end          gimp-image-undo-group-end
      gimp-undo-push-group-start        gimp-image-undo-group-start)

countp=0      # count of procedures we test for
countf=0      # count of files we changed
countm=0      # count of times we modify any file
counts=0      # count of procedures in a single script
countn=0      # count of found procedure with no replacement
period=0

while [ "${list[${countp}]}" != "" ] ; do
  pattern="${list[${countp}]}"
  let ++countp
  substitute="${list[${countp}]}"
  let ++countp

  # check every scheme script for this set of procedures
  countf=0
  for i in *.scm ; do
     let ++countf
     # look in each file for offending procedure
     if grep -q "${pattern}" "${i}" ; then
       let ++countm
       if [ ${verbose} -eq 1 ] ; then
         echo -n "."
         if [ 0 -eq $(expr ${countm} \% 40) ] ; then echo ""; fi
       fi
       counts=$(expr $counts \+ $(grep -c "${pattern}" "${i}"))
       if [ ${verbose} -gt 1 ] ; then
         # tell us about it
         echo "${pattern}/${substitute}:  ${i}"
       fi
       if [ ! -r "${i}.orig" ] ; then
         let ++countf
         # keep a copy of the original for every modified script
         cp "${i}" "${i}.orig"
       fi

       > tmpout
       # Add our notice to each file that is modified
       if ! grep -q "^; @@@@$" "${i}" ; then
         echo "; $(date)"                                                          > tmpout
         echo "; Modified to remove deprecated procedures as listed:"             >> tmpout
         echo "; @@@@"                                                            >> tmpout
         echo ";"                                                                 >> tmpout
       fi
       # Indicate this particular set of procedures has been modified
       sed -e "s/^; @@@@$/;     ${pattern}  ==>  ${substitute}\n; @@@@/" < "${i}" >> tmpout
       mv tmpout "${i}"
       if [ "${substitute}" != "NONE_AVAILABLE" ] ; then
         # Finally get around to doing it
         sed -e "s/${pattern}/${substitute}/g"                           < "${i}"  > tmpout
         mv tmpout "${i}"
       else
         let ++countn
       fi
     fi
  done
done

for i in *.scm ; do
  # remove pawprints
  sed -e "s/^; @@@@$/;/" < "${i}" > tmpout
  mv tmpout "${i}"
done

if [ ${verbose} -eq 2 ] ; then
  echo ""
  echo ""
  echo "Invoke this script with -v to cause more verbose updates."
  echo "Invoke this script with -q to cause it to run quietly"
  echo ""
  echo "This script has searched for depricated GIMP procedures"
  echo "in every \"*.scm\" file in the current directory.  The"
  echo "original files where retained as \"*.scm.orig\"."
fi

if [ ${verbose} -gt 0 ] ; then
  echo ""
  echo ""
  printf "%4d -- Number of scripts we searched through\n"             ${countf}
  printf "%4d -- Number of scripts we modified\n"                     ${countf}
  printf "%4d -- Number of procedures we searched for in scripts\n"   ${countp}
  printf "%4d -- Number times modifications were made to a script\n"  ${countm}
  printf "%4d -- Number procedures flagged, with no replacement\n"    ${countn}
  printf "%4d -- Total number of modifications made to all scripts\n" ${counts}
fi