Migrate WTF script

==Intoduction==
The following script allows you to change the realm / character names within your WTF. It is used in the following situations:
* Copying your WTF to the test realm installation
* Moving your characters from one realm to another
* Forced to rename your character
The script was developed and tested on a Mac, however it can be run on Linux as-is and on Windows via cygwin.
==Usage==
The script can be stored and run from anywhere.
migrateWTF.sh -a <account name> [-s <source dir>] [-d <destination dir>] -f <realm from> -t <realm to> {-o <old name> -n <new name>}
-a <account name> 	 	Name of the account in capitals; this is the directory under World of Warcraft/WTF/Account
-s <source dir> 	(optional) 	Source installation directory, defaults to /Applications/World of Warcraft
-d <destination dir>	(optional) 	Destination installation directory, defaults to /Applications/World of Warcraft/WoWTest
-f <realm from> 	 	Realm you are migrating from; remember to quote it if it contains special characters
-f <realm to> 	 	Realm you are migrating to; remember to quote it if it contains special characters
-o <old name> 	(optional, repeats)	Character name on source realm
-o <new name> 	(optional, repeats)	Character name on destination realm
-o and -n must be specified in pairs; zero or many -o -n pairs may be specified.
== Additional Information ==
To migrate from one realm to another within the same WoW installation i.e. you have moved realms, you should specify the same -s and -d. The old configuration will be backed up to:
 World of Warcraft/WTF.beforemigration
==Limitations==
Only one account may be migrated.
Only one realm may be migrated; you cannot have multiple from or to realms.
==Examples==
===Migration from your realm to the test realm without renaming any characters===
 ./migrateWTF.sh -a NEMES11 -s "/Applications/World of Warcraft" -d "/Applications/World of Warcraft/WoWTest" -f "Dath'Remar" -t "Test Server (US PVE)"
===Migration from one realm to another, renaming two toons===
 ./migrateWTF.sh -a NEMES11 -s "/Applications/World of Warcraft" -d "/Applications/World of Warcraft" -f "Thunderhead" -t "Privybush" -o Nemes -n Nemesy -o Rhad -n Dhar
==migrateWTF.sh==
#!/bin/sh
SRC="/Applications/World of Warcraft"
DST="${SRC}/WoWTest"
NUM_OLD=0
NUM_NEW=0
# print the usage message then exit with error
usage()
{
 echo
 echo "${0#*/} by nemes"
 echo
 echo "wiki: https://wowpedia.fandom.com/wiki/MigrateWTFscript"
 echo
 echo "usage: ${0#*/} -a <account name> [-s <source dir>] [-d <destination dir>] -f <realm from> -t <realm to> {-o <old name> -n <new name>}"
 echo " -a account name in caps"
 echo " -s source directory of the WOW install, defaults to /Applications/World of Warcraft"
 echo " -d destination directory of the WOW install, defaults to /Applications/World of Warcraft/WoWTest"
 echo " -f realm migrating from"
 echo " -t realm migrating to"
 echo " -o character name on source"
 echo " -n character name on destination"
 echo
 echo "-o and -n must be specified in pairs; zero or many -o -n pairs may be specified"
 echo "when -s and -d are the same, the exiting configuration will be backed up to WTF.beforemigration"
 echo
 echo "e.g.: to migrate from your regular server to the test realm, renaming two characters:"
 echo " ./${0#*/} -a NEMES11 -s \"/Applications/World of Warcraft\" -d \"/Applications/World of Warcraft/WoWTest\" -f \"Dath'Remar\" -t \"Test Server (US PVE)\" -o Nemes -n Nemesey -o Rhadamanth -n Rhad"
 echo
 echo "e.g.: to migrate from Thunderhead to Privybush without renaming any characters:"
 echo " ./${0#*/} -a NEMES11 -s \"/Applications/World of Warcraft\" -d \"/Applications/World of Warcraft\" -f \"Thunderhead\" -t \"Privybush\""
 echo
 exit 1
}
# check the return code (first argument) and if it's not success, print the second argument and exit with error
chkrc()
{
 RC=${1}
 MSG=${2}
 if [[ ${RC} -ne 0 ]]; then
 echo "$MSG"
 echo "return code: ${RC}"
 exit 1
 fi
}
# execute the sed script (first argument) on each lua file in the current directory
modifyLUA()
{
 SCRIPT=${1}
 PRINTED=0
 echo "Modifying lua files in ${PWD}:"
 LUA_FILE_LIST=$(ls *.lua 2>&1 > /dev/null)
 if [[ ${?} -eq 0 ]]; then
 for F in *.lua; do
 if [[ ${PRINTED} -gt 2 ]]; then
 PRINTED=0
 printf "\n"
 else
 PRINTED=$((${PRINTED}+1))
 fi
 printf "\t${F}"
 sed -f "${SCRIPT}" "${F}" > "${F}.new"
 chkrc ${?} "\nError executing sed on ${F}, with command file ${SCRIPT}, exiting"
 mv "${F}.new" "${F}"
 done
 printf "\n"
 fi
}
# parse the command line options
while getopts ":s:d:f:t:o:n:a:" OPT; do
 case ${OPT} in
 a) ACCT=${OPTARG} ;;
 s) SRC=${OPTARG} ;;
 d) DST=${OPTARG} ;;
 f) FROM=${OPTARG} ;;
 t) TO=${OPTARG} ;;
 o) OLD[${NUM_OLD}]=${OPTARG}; NUM_OLD=$((${NUM_OLD}+1)) ;;
 n) NEW[${NUM_NEW}]=${OPTARG}; NUM_NEW=$((${NUM_NEW}+1)) ;;
 \?) usage
 esac
done
shift $((${OPTIND} - 1))
# check from and to for existence
if [[ ${FROM} == "" ]]; then
 usage
fi
if [[ ${TO} == "" ]]; then
 usage
fi
# check the source and dest for existence
if [[ ! -d "${SRC}" ]]; then
 echo "${SRC} does not exist, exiting"
 exit 1
fi
if [[ ! -d "${DST}" ]]; then
 echo "${DST} does not exist, exiting"
 exit 1
fi
# check the account for existence
if [[ ${ACCT} == "" ]]; then
 usage
fi
if [[ ! -d "${SRC}/WTF/Account/${ACCT}" ]]; then
 echo "${SRC}/WTF/Account/${ACCT} does not exist, exiting"
 exit 1
fi
# check that the same number of old/new names are specified
if [[ ${NUM_OLD} -ne ${NUM_NEW} ]]; then
 echo "Different number of old and new character names specified, exiting"
 exit 1
fi
if [[ ${SRC} == ${DST} ]]; then
 SINGLEINSTALL=1
else
 SINGLEINSTALL=0
fi
# tell the user what we're going to do and give them a chance to stop
echo
echo "========================================"
if [[ ${SINGLEINSTALL} -eq 1 ]]; then
 echo "Performing migration for installation:"
 echo " ${SRC}"
 echo
 echo "Existing configuration will be stored in:"
 echo " ${SRC}/WTF.beforemigration"
else
 echo "Preparing to migrate WTF from:"
 echo " ${SRC}/WTF"
 echo "to"
 echo " ${DST}/WTF"
fi
echo
echo "Changes will be made to the World of Warcraft account: ${ACCT}"
echo
echo "Realm name will be changed:"
echo " ${FROM} -> ${TO}"
echo
echo "Characters renamed:"
I=0
while [[ ${I} -lt ${NUM_OLD} ]]; do
 echo " ${OLD[I]} -> ${NEW[I]}"
 I=$((${I}+1))
done
echo "========================================"
echo
echo "Press any key to continue"
read
# create the temporary directory for a single installation
if [[ ${SINGLEINSTALL} -eq 1 ]]; then
 DST="${SRC}/migtemp"
 mkdir "${DST}"
 chkrc ${?} "Error during creation of ${DST}, exiting"
fi
# copy the entire WTF folder, obliterating what was there
if [[ -d "${DST}/WTF" ]]; then
 echo "Removing ${DST}/WTF"
 rm -rf "${DST}/WTF"
 chkrc ${?} "Error during remove, exiting"
fi
echo "Copying ${SRC}/WTF/config.wtf, ${SRC}/WTF/Account to ${DST}/WTF"
mkdir "${DST}/WTF"
cp -r "${SRC}/WTF/config.wtf" "${SRC}/WTF/Account" "${DST}/WTF"
chkrc ${?} "Error during copy, exiting"
# change the realm folder name
echo "Changing realm folder name from ${FROM} to ${TO}"
mv "${DST}/WTF/Account/${ACCT}/${FROM}" "${DST}/WTF/Account/${ACCT}/${TO}"
chkrc ${?} "Error during rename, exiting"
# change the character folder name
I=0
while [[ ${I} -lt ${NUM_OLD} ]]; do
 echo "Changing character folder name from ${OLD[I]} to ${NEW[I]}"
 mv "${DST}/WTF/Account/${ACCT}/${TO}/${OLD[I]}" "${DST}/WTF/Account/${ACCT}/${TO}/${NEW[I]}"
 chkrc ${?} "Error during rename, exiting"
 I=$((${I}+1))
done
# build a sed command file
SED_FILE="${DST}/WTF/sed_cmd.txt"
echo "s/\"${FROM}/\"${TO}/g" >> ${SED_FILE}
echo "s/${FROM}\"/${TO}\"/g" >> ${SED_FILE}
I=0
while [[ ${I} -lt ${NUM_OLD} ]]; do
 echo "s/\"${OLD[I]}/\"${NEW[I]}/g" >> ${SED_FILE}
 echo "s/${OLD[I]}\"/${NEW[I]}\"/g" >> ${SED_FILE}
 echo "s/\"char\/${OLD[I]}/\"char\/${NEW[I]}/g" >> ${SED_FILE}
 I=$((${I}+1))
done
# perform replacements on each lua file in SavedVariables
cd "${DST}/WTF/Account/${ACCT}/SavedVariables"
modifyLUA "${SED_FILE}"
# perform replacements for each character in the realm
cd "${DST}/WTF/Account/${ACCT}/${TO}"
for D in *; do
 if [[ -d ${D} ]]; then
 cd ${D}
 modifyLUA "${SED_FILE}"
 if [[ -d SavedVariables ]]; then
 cd SavedVariables
 modifyLUA "${SED_FILE}"
 cd ..
 fi
 cd ..
 fi
done
# create the temporary directory for a single installation
if [[ ${SINGLEINSTALL} -eq 1 ]]; then
 echo "Moving orignal WTF to ${SRC}/WTF.beforemigration"
 mv "${SRC}/WTF" "${SRC}/WTF.beforemigration"
 chkrc ${?} "Error during rename, exiting"
 echo "Moving new WTF to ${SRC}/WTF"
 mv "${DST}/WTF" "${SRC}"
 chkrc ${?} "Error during move, exiting"
 rmdir "${DST}"
 chkrc ${?} "Error during removal of ${DST}, exiting"
fi
Category:Technical support