#!/bin/sh
#
# chg_ext.sh
#
#   version:	1.0
#   author:	Edward Buck <ed at bashware.net>
#   about:	Script to change file extensions recursively
#   license:	GNU General Public License (GPL) 2 or later
#   

fromext="$1"
toext="$2"

usage () {
  echo "Usage: $0 fromext toext"
  echo "Example: $0 htm html"
}

if [ -z "$1" ] || [ -z "$2" ]
then
  echo "Too few arguments."
  usage
  exit 0
fi
if [ -n "$3" ]
then
  echo "Too many arguments."
  usage
  exit 0
fi

filelist=`find . -type f -name "*.$fromext"`

changeext () {
  while read file
  do
    newname=`echo "$file" | sed "s/\(.*\.\)${fromext}$/\1$toext/"`
    mv "$file" "$newname"
  done
}

if [ -n "$filelist" ]
then
  echo "$filelist" | changeext
else
  echo "No files to process."
fi


exit 0
