#!/bin/sh
#
# MASS RENAME V 1.2
# (c) 2000 Giancarlo Erra <rofus@mindless.com>
# This software is under the GNU Public License (GPL) 
#
#
# syntax: mrename '<pattern>' <prefix> -c|-m
# 
# help: mrename -h
#
# ToDo list:
# 1) add <suffix> support
# 2) regexp pattern matching
# 3) usage outside the current directory
#


# it looks ugly ... it's ugly...

	if [ $1 == "-h" ] 2> /dev/null
	then

  more << EOF



MASS RENAME v 1.2
(c) 2000 Giancarlo Erra <rofus@mindless.com>
This software is under the GNU Public License (GPL)


Usage: 
------

  mrename 'pattern' prefix -c|-p

- 'pattern' is the pattern to search files to rename
  (quoted to avoid that bash resolve it)
- prefix is the prefix that will be added to the name of each file
- The option -c will copy each file with the new filename
  (cp oldnamed-file newnamed-file)
- The option -m will move each file in the new filename
  (mv oldnamed-file newnamed-file)

 NOTE:
 1) All parameters are needed
 2) You have to stay and launch the script in the same
    directory of the images. The program should be able
    to write in this directory.


Example:
 If you have a directory with two jpeg images prof.jpg and forp.jpg
 and you want to add them a prefix like item0, item1 etc..
 (that is item0prof.jpg, item1forp.jpg etc..) do this: 

   cd /path/to/the/images
   mrename '*.jpg' item -c

 to copy each matching file into another with the new name
  
   mrename '*.jpg' item -m

 to rename each file without keeping a copy with the previous name
  
 
-Note about the format of rename:
The default format is <prefix><progressive-numer><oldname>
To change this please open the mcpmv file and edit to fit
your needs (you will find also an example)



                             Giancarlo Erra <rofus@mindless.com>
                             http://alfalinux.sourceforge.net

EOF

exit
	else 2> /dev/null
  echo "" 2> /dev/null
	fi

# just a primitive error check...

	if [ ! $3 ]
	then
  echo ""
  echo "Syntax error: Please type mrename -h for help"
  echo ""
  exit
	elif [ $3 == "-c" ]
	then
  action="copy"
	elif [ $3 == "-m" ]
	then
  action="move"
	else
  echo ""
  echo "Error, wrong third parameter! Should be -m or -c"
  echo "You inserted < $3 > "
  echo "Please type mrename -h for help"
  echo ""
  exit
fi

# welcome and enjoy!

echo ""
echo "MASS RENAME"
echo ""
echo "I'll do a < $action > action"
echo "I'm searching for < $1 > renaming with < $2 > prefix..."
echo -n "Please check this parameters and type \"yes\" if correct: "
read reply
	
	if [ $reply == "yes" ]
	then
  echo "0" > ./.mrename-c
  echo ""
  find ./ -name "$1" -exec mcpmv {} $2 $action \; 
  echo ""
  ls --color
  echo ""
  rm -rf ./.mrename-c
  echo "Done"
  echo ""
	else
  echo ""
  echo "You didn't type \"yes\", exiting..."
  echo "Please type mrename -h for help."
  echo ""
  exit 1
	fi

# that's all
