#!/bin/bash -e

. ui-libopt.sh
ui_opt_init "Interactively update a project using sp-gxmlcpp to ui-gxmlcpp.
Just run this from your project's top level."
ui_opt_parse "$@"

replace()
{
	local f="$1"
	local s="$2"
	local d="$3"
	local f_tmp="$f.ui-gxmlcpp-sp2ui.tmp"

	cp "${f}" "${f_tmp}"
	sed "s/${s}/${d}/g" "${f_tmp}" >"${f}"
	rm "${f_tmp}"
}

echo "Assuming project using old sp-gxmlcpp in wd."
read -p "RETURN to update project (changes only local files), C-c to cancel."

ui-auto-uvc check_sync

for f in `find . -type f ! -wholename "*/.svn/*" ! -wholename "*/CVS/*" ! -name "*~" ! -name "ChangeLog" ! -name "NEWS"  ! -name "*sp2ui*"`; do
	echo "Updating ${f}..."
	replace "$f" "sp-gxmlcpp" "ui-gxmlcpp"
	replace "$f" "sp_gxmlcpp" "ui_gxmlcpp"
	replace "$f" "namespace SP" "namespace UI"
	replace "$f" "SP::" "UI::"
	replace "$f" "SP_GXMLCPP" "UI_GXMLCPP"
done

echo
echo "=> Project updated; remaining TODOS for you:"
echo
echo "* Verify all changed files via VCS. You should only see sp->ui naming conversions for ui-gxmlcpp related strings."
echo "* Run \"ui-auto-strap && ./configure && make distcheck\" to test changes."
echo "* VC: You may need to fix up 'ignore settings'."

exit 0
