#! /bin/bash
# Copyright (C) 2006-2016  CEA/DEN, EDF R&D
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
#
# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
#

usage()
{
    echo
    echo Usage :
    echo "  renameSalomeModule  oldName newName dirModule"
    echo
    echo "  -> replace any occurence of oldName by newName in module dirModule"
    echo 
    echo "    (nex name cannot contain old name)"
    echo
    echo Example :
    echo
    echo "  cp -r HELLO_SRC CALCULATOR_SRC"
    echo "  renameSalomeModule  HELLO CALCULATOR CALCULATOR_SRC"
    echo
    exit 1
}

rename_dir()
{
    # Rename the first directory containing $oldName in their file name
    liste=`find . -type d -name "*${oldName}*"`
    for file in $liste
    do
	newFile=`echo $file | sed "s/${oldName}/${newName}/g"`
	echo "mv -f $file $newFile"
	mv -f $file $newFile
	return 1
    done
    return 0
}

if [ "$1" == "-i" ]
then
    # mode interactif
    if [ $# -ne 4 ]
    then
        usage
    fi
    oldName=$2
    newName=$3
    moduleName=$4
else
    if [ $# -ne 3 ]
    then
	usage
    fi
    oldName=$1
    newName=$2
    moduleName=$3
fi

# check names for restriction
echo $newName | grep $oldName > /dev/null
if [ $? -eq 0 ]
then
    echo -e "Sorry : There is a restriction!\nNew name cannot contain old name.\n"
    usage 
fi

today=`date +%d%m%y`

# Check arguments
if [ "$1" == "-i" ] # mode interactif
then
    echo replace any occurence of $oldName by $newName in module $moduleName
else
    clear
    echo "Do you want to replace any occurence of $oldName by $newName in module $moduleName ?"
    echo
    read rep
    case $rep in

      y* | Y* | o* | O* )       ;;
      * )                 usage ;;
    esac
fi

cd ${moduleName}
echo
echo Renamed Directories:
echo

# rename directories one by one
rename_dir
while [ $? -eq 1 ]
do
    rename_dir
done

# rename files containing ${oldName} in their name
echo
echo Renamed Files:
echo
liste=`find . -name "*${oldName}*"`
for file in $liste
do
    newFile=`echo $file | sed "s/${oldName}/${newName}/g"`
    echo "mv -f $file $newFile"
    mv -f $file $newFile
done

echo
echo Modified Files:
echo
# modify all files containing $oldName, replace occurences by $newName
liste=`find -name "*"`
for file in $liste
do
    if [ -f $file ]
    then
	grep $oldName $file > /dev/null 2>&1
	if [ $? -eq 0 ]
	then
	    echo $file
	    cat $file | sed "s/${oldName}/${newName}/g" > fic_temp_${today}
	    cp -f fic_temp_${today} $file
	fi
    fi
done
rm -f fic_temp_${today}
cd -
