#! /bin/bash

if [ $# -lt 2 ]; then
    echo "usage $0 old_name new_name"
    exit 1
fi

OLD=$1
NEW=$2
OLD_L=$(echo $OLD | tr '[:upper:]' '[:lower:]')
NEW_L=$(echo $NEW | tr '[:upper:]' '[:lower:]')

WHERE="core android desktop ios html build.gradle run"

./gradlew clean

echo "replace '$OLD with $NEW'"
for F in $(ack -l $OLD $WHERE); do
    sed -i "s/$OLD/$NEW/g" $F
done

echo "replace '$OLD_L' with '$NEW_L'"
for F in $(ack -l $OLD_L $WHERE); do
    sed -i "s/$OLD_L/$NEW_L/g" $F
done

echo "move '$OLD_L' directories into '$NEW_L'"
for D in $(find .  -type d -name $OLD_L); do
    mv $D ${D%/*}/$NEW_L
done

echo "move '$OLD.java' files into '$NEW.java'"
for F in $(find .  -type f -name $OLD.java); do
    mv $F ${F%/*}/$NEW.java
done

echo "mv core/src/${OLD}.gwt.xml core/src/${NEW}.gwt.xml"
mv core/src/${OLD}.gwt.xml core/src/${NEW}.gwt.xml

vim $(find desktop/src/ -name DesktopLauncher.java)