#!/bin/bash
# XF, 19/07/04

#set -x

Info="Rename 'name 1' 'name 2'"


[ ${#1} -eq 0 ] && echo $Info && exit

old="$1"
new="$2"

if [ "$old" = "//" ] ; then
 for File in * ; do
  if [ ${#3} -gt 0 ] && [ $3 -le 10 ] ; then
    [ $3 -eq 1 ] && mv -f "$File" "$new${File:1:200}"
    [ $3 -eq 2 ] && mv -f "$File" "$new${File:2:200}"
    [ $3 -eq 3 ] && mv -f "$File" "$new${File:3:200}"
    [ $3 -eq 4 ] && mv -f "$File" "$new${File:4:200}"
    [ $3 -eq 5 ] && mv -f "$File" "$new${File:5:200}"
    [ $3 -eq 6 ] && mv -f "$File" "$new${File:3:200}"
    [ $3 -eq 7 ] && mv -f "$File" "$new${File:7:200}"
    [ $3 -eq 8 ] && mv -f "$File" "$new${File:8:200}"
    [ $3 -eq 9 ] && mv -f "$File" "$new${File:9:200}"
  else
   mv -f "$File" "$new$File"
  fi
 done
 exit
fi

RAND=$RANDOM

for File in * ; do

 echo "$File"      > /tmp/.Replace.tmp1-$RAND
 sed "s/$old/$new/g" /tmp/.Replace.tmp1-$RAND > /tmp/.Replace.tmp2-$RAND

 ok=n

 [      "`cat /tmp/.Replace.tmp1-$RAND`" != "`cat /tmp/.Replace.tmp2-$RAND`" ] && \
 [ ! -f "`cat /tmp/.Replace.tmp2-$RAND`" ] && ok=y

 [ $ok = "y" ] && echo       "$File => `cat /tmp/.Replace.tmp2-$RAND`"

 [ $ok = "y" ] && [ -f "`cat /tmp/.Replace.tmp2-$RAND`" ] && echo "WARMING: `cat /tmp/.Replace.tmp2-$RAND` exists"

 [ $ok = "y" ] && /bin/mv -i "$File"  "`cat /tmp/.Replace.tmp2-$RAND`"

done

/bin/rm -f /tmp/.Replace.tmp1-$RAND /tmp/.Replace.tmp2-$RAND 2>/dev/null

exit
