#!/bin/sh

# All attack animations are currently categorised as Melee.
# Some units may have multiple types though, such as Ranged and Slaughter.
# This batch edit sets up the contents of actor files accordingly.
#
# INSTRUCTIONS FOR CORRECT USE
#
# - RUN THIS SHELL SCRIPT FROM WITHIN THE binaries/mods/public/art/actors FOLDER!
#   It will automatically find and edit files in subfolders as well.
# - Avoid running this from the /public/ folder directly, as it will also falsely
#   adjust simulation entity XML files.
# - It will edit the files in place, and add <name>.bu backup files. If anything
#   goes wrong those provide a way out. Anyway, OS X requires this argument for
#   the sed -i command to work, although it can be left as empty string ('').
#   The backups can be removed with: find . -name "*.xml.bu" -type f -delete
#
# SOME CAVEATS
#
# 1. Only actor files that are appropriately named can be given their correct
#    animation, such as attack_ranged. Special actors, heroes, and champion actors
#    are often not labelled with their specific ability (e.g., javelinist).
#    Those actors will remain with the default attack_melee, if not manually adjusted
#    below (list should be exhaustive, but not guaranteed).
# 2. Actors that feature multiple capabilities are not set properly, although
#    it presents no change from the current situation with only Melee available.
# 3. The engine will give errors for those actors that have a ranged attack but no
#    properly set projectile attachment, hinting that for this actor its
#    actor file needs some manual work to correctly set attack_ranged.
# 4. Since the engine defaults to the idle animation if no animation with a given
#    name is found, any problems should be easy to spot.

# before starting, check if indeed in actors directory (avoids a mess)
if [ ${PWD##*/} != "actors" ]
	then
		echo "ERROR: make sure you are in the <mod name>/art/actors folder"
		exit 1
fi

# first, set all actors that have a melee animation to the attack_melee type.
find . -name "*.xml" -print | xargs sed -i '.bu' 's/name="[m|M]elee/name="attack_melee/g'

# also fix some (older?) actors that have an unspecified 'Attack' animation
# note: the more complex regex (requiring -E) also captures attack_melee here to avoid
#       ending up with weird stuff like attack_melee_melee.
find . -name "*.xml" -print | xargs sed -E -i '.bu' 's/name="[a|A]ttack(_[a-z]+)?/name="attack_melee/g'

# now, adjust ranged actor animations to attack_ranged
find . -name "*archer*.xml" -print | xargs sed -i '.bu' 's/name="attack_melee/name="attack_ranged/g'
find . -name "*javelinist*.xml" -print | xargs sed -i '.bu' 's/name="attack_melee/name="attack_ranged/g'
find . -name "*ranged.xml" -print | xargs sed -i '.bu' 's/name="attack_melee/name="attack_ranged/g'
find . -name "*slinger*.xml" -print | xargs sed -i '.bu' 's/name="attack_melee/name="attack_ranged/g'
find . -name "*skirmisher.xml" -print | xargs sed -i '.bu' 's/name="attack_melee/name="attack_ranged/g'

# individual actor adjustments: melee->ranged
sed -i '.bu' 's/name="attack_melee/name="attack_ranged/g' props/special/palisade_rocks_fort.xml
sed -i '.bu' 's/name="attack_melee/name="attack_ranged/g' units/athenians/iphicrates.xml
sed -i '.bu' 's/name="attack_melee/name="attack_ranged/g' units/carthaginians/siege_rock.xml
sed -i '.bu' 's/name="attack_melee/name="attack_ranged/g' units/celts/boudicca_*.xml
sed -i '.bu' 's/name="attack_melee/name="attack_ranged/g' units/celts/champion_unit_4*.xml
sed -i '.bu' 's/name="attack_melee/name="attack_ranged/g' units/hellenes/champion_unit_2.xml
sed -i '.bu' 's/name="attack_melee/name="attack_ranged/g' units/hellenes/siege_fireraiser.xml
sed -i '.bu' 's/name="attack_melee/name="attack_ranged/g' units/hellenes/xenophon.xml
sed -i '.bu' 's/name="attack_melee/name="attack_ranged/g' units/iberians/champion_unit_2*.xml
sed -i '.bu' 's/name="attack_melee/name="attack_ranged/g' units/macedonians/thureophoros.xml
sed -i '.bu' 's/name="attack_melee/name="attack_ranged/g' units/mauryans/hero_chariot*.xml
sed -i '.bu' 's/name="attack_melee/name="attack_ranged/g' units/persians/hero_darius_chariot*.xml
sed -i '.bu' 's/name="attack_melee/name="attack_ranged/g' units/persians/pers_chariot_darius_h1.xml
sed -i '.bu' 's/name="attack_melee/name="attack_ranged/g' units/persians/hero_xerxes*.xml
sed -i '.bu' 's/name="attack_melee/name="attack_ranged/g' units/romans/siege_rock.xml
sed -i '.bu' 's/name="attack_melee/name="attack_ranged/g' units/thebans/siege_fireraiser.xml

# for all structures that do have attack capabilities, it is of type attack_ranged
# note: must be tested to exist, so unit-only mods can apply this as well
#       if structures folder does not exist, all actors set to attack_ranged, world will end.
if [ -d "structures" ]
	then
		cd structures
		find . -name "*.xml" -print | xargs sed -i '.bu' 's/name="attack_melee/name="attack_ranged/g'
		# now revert siege_ram.xml files because those are melee
		find . -name "*_ram.xml" -print | xargs sed -i '.bu' 's/name="attack_ranged/name="attack_melee/g'
	else
		echo "Skipping structures folder: does not exist"
fi

echo '==> DONE <=='