Skip to content

Instantly share code, notes, and snippets.

@ProGM
Last active October 29, 2015 09:11
Show Gist options
  • Save ProGM/7aa1d6317c7fc5c359b7 to your computer and use it in GitHub Desktop.
Save ProGM/7aa1d6317c7fc5c359b7 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
# Extracts all guid from an animation
extract_id_list() {
grep -o "guid: [0-9a-z]\+" $1 | grep -o "[0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z]\+" | xargs -n1 | sort -u | xargs
}
# Gives a meta file from it's guid
find_by_id() {
grep -HLIlr "guid: $1" . --include *.meta
}
# Finds the guid given a meta file
find_by_name() {
grep -o "guid: [0-9a-z]\+" $1 | grep -o "[0-9a-z][0-9a-z][0-9a-z][0-9a-z][0-9a-z]\+"
}
if [ "$#" -ne 3 ]; then
echo "Illegal number of parameters"
echo "Usage: replace_animation.sh \"path/to/animation.anim\" \"OldGraphicsName\" \"NewGraphicsName\""
echo "It will replace all graphics that contains \"OldGraphicsName\" string with \"NewGraphicsName\""
fi
animation=$1
old_name=$2
new_name=$3
echo $animation
echo $old_name
echo $new_name
file_list=($(extract_id_list $animation))
for i in "${file_list[@]}"
do
echo "Starting with file id: $i"
current_file_name=$(find_by_id $i)
echo $current_file_name
new_file_name=$(echo $(basename $current_file_name) | sed "s/$old_name/$new_name/g")
echo $new_file_name
new_file_path=$(find . -name $new_file_name)
if [[ $new_file_path ]]; then
echo "OK"
else
echo "File not found. Terminating"
exit 1
fi
echo $new_file_path
new_id=$(find_by_name $new_file_path)
sed -i'' "s/$i/$new_id/g" $animation
echo
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment