#!/bin/bash
# tomz.pl-find_replace>Find and replace name of files with backup
#


## look @
if [ -n "$1" ]
    then
	look_at="$1"
else
  	echo "Usage: </path/to/change> </path/to/backup>"
    exit 0 
fi			
		
## copy to
if [ -n "$2" ]
    then

        path_to_cp="$1"
        IFS=/ read -a ARRAY <<< "$2"

        if [ -f "$path_to_backup/backup" ]
          then
            echo "The file name backup exists in specified path, delete or specify new path"
            exit 0
      	fi

        dir="/"
        for dir in ${ARRAY[@]}
	        do
		          path_to_backup="$path_to_backup/$dir"
		          if [ -f "$path_to_backup" ]
                then
                     echo "$path_to_backup is a file, exiting"
                     exit 0
      	      fi
            
              if [ -e "$path_to_backup" ]
                then
                      continue
      	      fi      	         
      	         
      	         `mkdir $path_to_backup`
	        done
	         path_to_backup="$path_to_backup/"
    else
	echo "Usage: </path/to/change> </path/to/backup>"

fi

old_string=('LAN' 'MGMT' 'CORE' 'PUBLIC' 'LAB')
new_string=('$LAN' '$MGMT' '$CORE' '$PUBLIC' '$LAB')



i=0
pass_nr=0



while [ -n "${old_string[$i]}" ] && [ -n "${new_string[$i]}" ] 
    do
	command="grep -l -r -e '\<${old_string[$i]##\$}\>' $look_at"

	
echo "${old_string[$i]}"

	for file_list in `eval $command`
	    do
		if [ -h $file_list ]
		  then
		      echo "Symlink detected skipping - $file_list"
		      continue
		  fi
    path_to_file="${file_list%/*}"
    		short_path_dir="${path_to_file#$look_at}"	    

	        if [ -n "$short_path_dir" ]
    		    then
			if [ -d "$path_to_backup/$short_path_dir" ] && [ -d "$path_to_cp/$short_path_dir" ]
			    then
				cp_new_path="$path_to_cp/$short_path_dir/"
				cp_new_backup="$path_to_backup/$short_path_dir/"
			else
				if [ ! -d "$path_to_cp/$short_path_dir" ]
				    then
					`mkdir -p $path_to_cp/$short_path_dir`
					cp_new_path="$path_to_cp/$short_path_dir/"
				    fi
				if [ ! -d "$path_to_backup/$short_path_dir" ]
				    then
					`mkdir -p $path_to_backup/$short_path_dir`
					cp_new_backup="$path_to_backup/$short_path_dir/"
				    fi
			    # File permission
				cmd="'printf \"%o\", (07777 & (stat(\"$path_to_file\"))[2])'"
				permission="`eval perl -le $cmd`"
				#`chmod $permission $cp_new_path`
				`chmod $permission $cp_new_backup`
				unset permission
				
			    # File owner
				cmd="'printf \"%d\", (stat(\"$path_to_file\"))[4]'"
				    uid="`eval perl -le $cmd`"

				cmd="'printf \"%d\", (stat(\"$path_to_file\"))[5]'"
				    gid="`eval perl -le $cmd`"

				`chown $uid:$gid $cp_new_path`
				`chown $uid:$gid $cp_new_backup`
				
			fi
		fi

	    file_i=0
	    matched=0

		while [ $matched -lt 1  ] || [ $pass_nr == 0 ]
		    do
			if [ -n "${change_file_index[$file_i]}" ]
			    then
				    if [ "${change_file_index[$file_i]}" == "$file_list" ]
					then
						change_string_index[$file_i]="${change_string_index[$file_i]} $i"
	    					let matched+=1													
				    else
				    		let file_i+=1
				    fi
			elif [ $pass_nr == 0 ]
				then
				    change_file_index[$file_i]="$file_list"
				    change_string_index[$file_i]="$i"
				    let matched+=1													
			else
				change_file_index[$file_i]="$file_list"

			fi
		    let pass_nr+=1
		done
 
	done
	let i+=1
 done

#Make and save changes
change_i=0
while [ -n "${change_file_index[$change_i]}" ]
    do
	sed_command=""
	echo -e "PROCESSING FILE: ${change_file_index[$change_i]} \n"

	for string_index in ${change_string_index[$change_i]}
	    do
		echo "String was changed: FROM ${old_string[$string_index]} TO ${new_string[$string_index]}"
		sed_command="$sed_command -i -e 's/\<${old_string[$string_index]}\>/${new_string[$string_index]}/g'"
	    done    
	new_path="${change_file_index[$change_i]/$look_at/$path_to_cp}" 
	new_backup="${change_file_index[$change_i]/$look_at/$path_to_backup}" 
	echo $new_backup
# Backup
    	`cp ${change_file_index[$change_i]} $new_backup`
# Change	
    	`eval sed $sed_command ${change_file_index[$change_i]}`

	

    # File permission
	cmd="'printf \"%o\", (07777 & (stat(\"${change_file_index[$change_i]}\"))[2])'"
	permission="`eval perl -le $cmd`"

	#`chmod $permission $new_path`
	`chmod $permission $new_backup`
	
	unset permission
				
    # File owner
	cmd="'printf \"%d\", (stat(\"${change_file_index[$change_i]}\"))[4]'"
        uid="`eval perl -le $cmd`"

	cmd="'printf \"%d\", (stat(\"${change_file_index[$change_i]}\"))[5]'"
        gid="`eval perl -le $cmd`"

	#`chown $uid:$gid $new_path`
	`chown $uid:$gid $new_backup`
	
#	unset gid, uid
	
	echo -e "\nFINISHED - NEW FILE: $new_path"
	echo "---------------------------------------------------------------------"
	let change_i+=1
    done

