#!/bin/bash

if [[ $# -ne 3 ]]
then 
	echo "Replaces strings in textfiles. ">&2
	echo "All arguments must be text files with one item per line." >&2
	echo "Usage: $0 old_texts new_texts change_in_these_files" >&2
	exit 1
fi

declare -a oldnames=( `cat $1` )
declare -a newnames=( `cat $2` )
declare -a thesefiles=( `cat $3` )
declare -i index=0

if [[ ${#newnames[@]} -ne ${#oldnames[@]} ]] 
then 
	echo "Different number of entries in $1 and $2" >&2
	exit 1
fi

# For each old file
for currentfile in "${thesefiles[@]}" 
do
	for (( index=0; index < ${#newnames[@]}; ++index )) 
	do
		# I think this should handle directories properly, but
		# just in case, using | instead of the usual / 
		sed -i "s|${oldnames[index]}|${newnames[index]}|g" "$currentfile"
	done
	echo "Changed all in $currentfile "
done

exit 0
