#!/bin/bash
#
# THIS FILE IS NOT GENERATED OR SUPPORTED BY ACTIVISION.
#
# Fix case of Civ:CTP2 data file names on case sensitive file systems.
#
# Just cd into the ctp2_data directory and call this script or pass the path
# to the ctp2_data directory as argument to this script.
#
# Written by Marc Haisenko <marc@darkdust.net>

if [ -n "$1" -a -d "$1" ] ; then
	cd "$1"
fi

if [ -d "ctp2_data" ] ; then
	cd "ctp2_data"
fi

if [ ! -d "default/gamedata" ] ; then
	echo -n "You have to cd into the ctp2_data directory before calling "
	echo " this script !"
	exit 1
fi

# List of language directories
LANGUAGES="english french german italian japanese spanish"

# List of files to rename. Each string consists of the relativ path to the
# file, followed by a colon and the new file name. If the path starts with
# "LANGUAGE", that file is renamed in all directories defined in the LANGUAGES
# variable.
NAMES=(	"LANGUAGE/gamedata/Strings.txt:strings.txt"
	"LANGUAGE/uidata/layouts/SpriteEditor.ldl:spriteeditor.ldl"
	"default/aidata/AdvanceLists.txt:advancelists.txt"
	"default/aidata/BuildingBuildLists.txt:buildingbuildlists.txt"
	"default/aidata/BuildListSequences.txt:buildlistsequences.txt"
	"default/aidata/Goals.txt:goals.txt"
	"default/aidata/ImprovementLists.txt:improvementlists.txt"
	"default/aidata/UnitBuildLists.txt:unitbuildlists.txt"
	"default/aidata/WonderBuildLists.txt:wonderbuildlists.txt"
	"default/gamedata/Advance.txt:advance.txt"
	"default/gamedata/EndGameObjects.txt:endgameobjects.txt"
	"default/gamedata/Orders.txt:orders.txt"
	"default/gamedata/Units.txt:units.txt"
	"default/gamedata/Wonder.txt:wonder.txt"
	"default/graphics/cursors/UC001.tga:uc001.tga"
	"default/graphics/cursors/UC002.tga:uc002.tga"
	"default/graphics/cursors/UC003.tga:uc003.tga"
	"default/graphics/cursors/UC004.tga:uc004.tga"
	"default/graphics/cursors/UC005.tga:uc005.tga"
	"default/graphics/cursors/UC006.tga:uc006.tga"
	"default/graphics/cursors/UC014.tga:UC014.TGA"
	"default/graphics/cursors/UC015.tga:UC015.TGA"
	"default/graphics/cursors/UC016.tga:UC016.TGA"
	"default/graphics/cursors/Uc030.tga:UC030.TGA"
	"default/graphics/cursors/Uc031.tga:UC031.TGA"
	"default/graphics/cursors/UC032.tga:UC032.TGA"
	"default/graphics/cursors/UC033.tga:UC033.TGA"
	"default/graphics/cursors/UC034.tga:UC034.TGA"
	"default/graphics/cursors/UC035.tga:UC035.TGA"
	"default/graphics/cursors/UC036.tga:UC036.TGA"
	"default/graphics/cursors/UC037.tga:UC037.TGA"
	"default/graphics/cursors/UC038.tga:UC038.TGA"
	"default/graphics/cursors/UC039.tga:UC039.TGA"
	"default/graphics/cursors/UC040.tga:UC040.TGA"
	"default/graphics/cursors/UC041.tga:UC041.TGA"
	"default/graphics/cursors/UC042.tga:UC042.TGA"
	"default/graphics/cursors/UC043.tga:UC043.TGA"
	"default/graphics/cursors/UC044.tga:UC044.TGA"
	"default/graphics/cursors/UC045.tga:UC045.TGA"
	"default/graphics/cursors/UC046.tga:UC046.TGA"
	"default/graphics/cursors/UC047.tga:UC047.TGA"
	"default/graphics/cursors/UC048.tga:UC048.TGA"
	"default/graphics/cursors/UC049.tga:UC049.TGA"
	"default/graphics/cursors/UC050.tga:UC050.TGA"
	"default/graphics/cursors/UC051.tga:UC051.TGA"
	"default/graphics/cursors/UC052.tga:UC052.TGA"
	"default/graphics/cursors/UC053.tga:UC053.TGA"
	"default/graphics/cursors/UC054.tga:UC054.TGA"
	"default/graphics/cursors/UC055.tga:UC055.TGA"
	"default/graphics/cursors/UC056.tga:UC056.TGA"
	"default/graphics/cursors/UC057.tga:UC057.TGA"
	"default/graphics/cursors/UC058.tga:UC058.TGA"
	"default/graphics/cursors/UC059.tga:UC059.TGA"
	"default/graphics/cursors/UC060.tga:UC060.TGA"
	"default/graphics/cursors/UC061.tga:UC061.TGA"
	"default/graphics/cursors/UC062.tga:UC062.TGA"
	"default/graphics/cursors/UC063.tga:UC063.TGA"
	"default/graphics/cursors/UC064.tga:UC064.TGA"
	"default/graphics/cursors/UC065.tga:UC065.TGA"
	"default/graphics/cursors/UC066.tga:UC066.TGA"
	"default/graphics/cursors/UC067.tga:UC067.TGA"
	"default/graphics/cursors/UC068.tga:UC068.TGA"
	"default/graphics/cursors/UC069.tga:UC069.TGA"
	"default/graphics/cursors/UC070.tga:UC070.TGA"
	"default/graphics/cursors/UC071.tga:UC071.TGA"
	"default/graphics/cursors/UC072.tga:UC072.TGA"
	"default/graphics/cursors/UC073.tga:UC073.TGA"
	"default/graphics/cursors/UC074.tga:UC074.TGA"
	"default/graphics/cursors/UC075.tga:UC075.TGA"
	"default/graphics/cursors/UC076.tga:UC076.TGA"
	"default/graphics/cursors/UC077.tga:UC077.TGA"
	"default/graphics/cursors/UC078.tga:UC078.TGA"
	"default/graphics/cursors/UC079.tga:UC079.TGA"
	"default/graphics/cursors/UC080.tga:UC080.TGA"
	"default/graphics/cursors/UC081.tga:UC081.TGA"
	"default/graphics/cursors/UC082.tga:UC082.TGA"
	"default/graphics/cursors/UC083.tga:UC083.TGA"
	"default/graphics/cursors/UC084.tga:UC084.TGA"
	"default/graphics/cursors/UC085.tga:UC085.TGA"
	"default/graphics/cursors/UC086.tga:UC086.TGA"
	"default/graphics/cursors/UC087.tga:UC087.TGA"
	"default/graphics/cursors/UC088.tga:UC088.TGA"
	"default/graphics/cursors/UC089.tga:UC089.TGA"
	"default/graphics/pictures/UG026.TGA:ug026.tga"
	"default/graphics/pictures/UG027.TGA:ug027.tga"
	"default/graphics/sprites/Gc066.spr:GC066.SPR"
	"default/graphics/sprites/Gc067.spr:GC067.SPR"
	"default/graphics/sprites/Gc068.spr:GC068.SPR"
	"default/graphics/sprites/Gc069.spr:GC069.SPR"
	"default/graphics/sprites/Gc070.spr:GC070.SPR"
	"default/graphics/sprites/Gc071.spr:GC071.SPR"
	"default/graphics/sprites/Gc072.spr:GC072.SPR"
	"default/graphics/sprites/Gc084.spr:GC084.SPR"
	"default/graphics/sprites/Gc085.spr:GC085.SPR"
	"default/graphics/sprites/Gc086.spr:GC086.SPR"
	"default/graphics/sprites/Gc087.spr:GC087.SPR"
	"default/graphics/sprites/Gc088.spr:GC088.SPR"
	"default/graphics/sprites/Gc097.spr:GC097.SPR"
	"default/graphics/sprites/Gc098.spr:GC098.SPR"
	"default/graphics/sprites/Gc099.spr:GC099.SPR"
	"default/graphics/sprites/Gc100.spr:GC100.SPR"
	"default/graphics/sprites/Gc101.spr:GC101.SPR"
	"default/graphics/sprites/Gc102.spr:GC102.SPR"
	"default/graphics/sprites/Gc103.spr:GC103.SPR"
	"default/graphics/sprites/Gc104.spr:GC104.SPR"
	"default/graphics/sprites/Gc105.spr:GC105.SPR"
	"default/graphics/sprites/Gc106.spr:GC106.SPR"
	"default/graphics/sprites/Gc107.spr:GC107.SPR"
	"default/graphics/sprites/Gc108.spr:GC108.SPR"
	"default/graphics/sprites/Gc109.spr:GC109.SPR"
	"default/graphics/sprites/Gc110.spr:GC110.SPR"
	"default/graphics/sprites/Gc111.spr:GC111.SPR"
	"default/graphics/sprites/Gc112.spr:GC112.SPR"
	"default/graphics/sprites/Gc113.spr:GC113.SPR"
	"default/graphics/sprites/Gc114.spr:GC114.SPR"
	"default/graphics/sprites/Gc115.spr:GC115.SPR"
	"default/graphics/sprites/Gc116.spr:GC116.SPR"
	"default/graphics/sprites/Gc117.spr:GC117.SPR"
	"default/graphics/sprites/Gc118.spr:GC118.SPR"
	"default/graphics/sprites/Gc119.spr:GC119.SPR"
	"default/graphics/sprites/Gc120.spr:GC120.SPR"
	"default/graphics/sprites/Gc121.spr:GC121.SPR"
	"default/graphics/sprites/Gc122.spr:GC122.SPR"
	"default/graphics/sprites/Gc123.spr:GC123.SPR"
	"default/graphics/sprites/Gc124.spr:GC124.SPR"
	"default/graphics/sprites/Gc125.spr:GC125.SPR"
	"default/graphics/sprites/Gc126.spr:GC126.SPR"
	"default/graphics/sprites/Gc127.spr:GC127.SPR"
	"default/graphics/sprites/Gc128.spr:GC128.SPR"
	"default/graphics/sprites/Gc129.spr:GC129.SPR"
	"default/graphics/sprites/Gc130.spr:GC130.SPR"
	"default/graphics/sprites/Gc131.spr:GC131.SPR"
	"default/graphics/sprites/Gc132.spr:GC132.SPR"
	"default/graphics/sprites/Gc133.spr:GC133.SPR"
	"default/graphics/sprites/Gc134.spr:GC134.SPR"
	"default/graphics/sprites/Gc135.spr:GC135.SPR"
	"default/graphics/sprites/Gc136.spr:GC136.SPR"
	"default/graphics/sprites/Gc137.spr:GC137.SPR"
	"default/graphics/sprites/Gc138.spr:GC138.SPR"
	"default/graphics/sprites/Gc139.spr:GC139.SPR"
	"default/graphics/sprites/Gc140.spr:GC140.SPR"
	"default/graphics/sprites/Gc141.spr:GC141.SPR"
	"default/graphics/sprites/Gc142.spr:GC142.SPR"
	"default/graphics/sprites/Gc143.spr:GC143.SPR"
	"default/graphics/sprites/Gc144.spr:GC144.SPR"
	"default/graphics/sprites/Gc145.spr:GC145.SPR"
	"default/graphics/sprites/Gc146.spr:GC146.SPR"
	"default/graphics/sprites/Gc147.spr:GC147.SPR"
	"default/graphics/sprites/Gc148.spr:GC148.SPR"
	"default/graphics/sprites/Gc149.spr:GC149.SPR"
	"default/graphics/sprites/Gc150.spr:GC150.SPR"
	"default/graphics/sprites/Gc151.spr:GC151.SPR"
	"default/graphics/sprites/Gc152.spr:GC152.SPR"
	"default/graphics/sprites/Gc153.spr:GC153.SPR"
	"default/graphics/sprites/Gc154.spr:GC154.SPR"
	"default/graphics/sprites/Gc155.spr:GC155.SPR"
	"default/graphics/sprites/Gc156.spr:GC156.SPR"
	"default/graphics/sprites/Gc157.spr:GC157.SPR"
	"default/graphics/sprites/Gc158.spr:GC158.SPR"
	"default/graphics/sprites/Gc159.spr:GC159.SPR"
	"default/graphics/sprites/Gc160.spr:GC160.SPR"
	"default/graphics/sprites/Gc161.spr:GC161.SPR"
	"default/graphics/sprites/Gc162.spr:GC162.SPR"
	"default/graphics/sprites/Gc163.spr:GC163.SPR"
	"default/graphics/sprites/Gc164.spr:GC164.SPR"
	"default/graphics/sprites/Gc165.spr:GC165.SPR"
	"default/graphics/sprites/Gc166.spr:GC166.SPR"
	"default/graphics/sprites/Gc167.spr:GC167.SPR"
	"default/graphics/sprites/Gc168.spr:GC168.SPR"
	"default/graphics/sprites/Gc169.spr:GC169.SPR"
	"default/graphics/sprites/Gc170.spr:GC170.SPR"
	"default/graphics/sprites/Gc171.spr:GC171.SPR"
	"default/graphics/sprites/Gc172.spr:GC172.SPR"
	"default/graphics/sprites/Gc173.spr:GC173.SPR"
	"default/graphics/sprites/Gc174.spr:GC174.SPR"
	"default/graphics/sprites/Gc175.spr:GC175.SPR"
	"default/graphics/sprites/Gc176.spr:GC176.SPR"
	"default/graphics/sprites/Gc177.spr:GC177.SPR"
	"default/graphics/sprites/Gc178.spr:GC178.SPR"
	"default/graphics/sprites/Gc179.spr:GC179.SPR"
	"default/graphics/sprites/Gc180.spr:GC180.SPR"
	"default/graphics/sprites/Gc181.spr:GC181.SPR"
	"default/graphics/sprites/Gc182.spr:GC182.SPR"
	"default/graphics/sprites/Gc183.spr:GC183.SPR"
	"default/graphics/sprites/Gc184.spr:GC184.SPR"
	"default/graphics/sprites/Gc185.spr:GC185.SPR"
	"default/graphics/sprites/Gc186.spr:GC186.SPR"
	"default/graphics/sprites/Gc187.spr:GC187.SPR"
	"default/graphics/sprites/Gg025.spr:GG025.SPR"
	"default/graphics/sprites/Gg026.spr:GG026.SPR"
	"default/graphics/sprites/Gg027.spr:GG027.SPR"
	"default/graphics/sprites/Gg028.spr:GG028.SPR"
	"default/graphics/sprites/Gg029.spr:GG029.SPR"
	"default/graphics/sprites/Gg030.spr:GG030.SPR"
	"default/graphics/sprites/Gg031.spr:GG031.SPR"
	"default/graphics/sprites/Gg032.spr:GG032.SPR"
	"default/graphics/sprites/Gg033.spr:GG033.SPR"
	"default/graphics/sprites/Gg034.spr:GG034.SPR"
	"default/graphics/sprites/Gg035.spr:GG035.SPR"
	"default/graphics/sprites/Gg036.spr:GG036.SPR"
	"default/graphics/sprites/Gg037.spr:GG037.SPR"
	"default/graphics/sprites/Gg038.spr:GG038.SPR"
	"default/graphics/sprites/Gg039.spr:GG039.SPR"
	"default/graphics/sprites/Gg040.spr:GG040.SPR"
	"default/graphics/sprites/Gg041.spr:GG041.SPR"
	"default/graphics/sprites/Gg042.spr:GG042.SPR"
	"default/graphics/sprites/Gg043.spr:GG043.SPR"
	"default/graphics/sprites/Gg044.spr:GG044.SPR"
	"default/graphics/sprites/Gg045.spr:GG045.SPR"
	"default/graphics/sprites/Gg046.spr:GG046.SPR"
	"default/graphics/sprites/Gg047.spr:GG047.SPR"
	"default/graphics/sprites/Gg048.spr:GG048.SPR"
	"default/graphics/sprites/Gg049.spr:GG049.SPR"
	"default/graphics/sprites/Gg050.spr:GG050.SPR"
	"default/graphics/sprites/Gg051.spr:GG051.SPR"
	"default/graphics/sprites/Gg052.spr:GG052.SPR"
	"default/graphics/sprites/Gg054.spr:GG054.SPR"
	"default/graphics/sprites/Gg055.spr:GG055.SPR"
	"default/graphics/sprites/Gg056.spr:GG056.SPR"
	"default/graphics/sprites/Gg057.spr:GG057.SPR"
	"default/graphics/sprites/Gu002.spr:GU002.SPR"
	"default/graphics/sprites/Gu008.spr:GU008.SPR"
	"default/graphics/sprites/Gu077.spr:GU077.SPR"
	"default/graphics/sprites/Gu079.spr:GU079.SPR"
	"default/graphics/sprites/Gu080.spr:GU080.SPR"
	"default/graphics/sprites/Gu083.spr:GU083.SPR"
	"default/graphics/sprites/Gu09.spr:GU09.SPR"
	"default/graphics/sprites/Gx03.SPR:GX03.SPR"
	"default/graphics/sprites/Gx20.SPR:GX20.SPR"
	"default/graphics/sprites/Gx99.SPR:GX99.SPR"
)

# Renames a file if it exists.
function rename_file() {
		dir="$1"
		oldname="$2"
		newname="$3"

		if [ ! -e "$dir/$oldname" ] ; then
			return 
		fi
		
		echo "Renaming \"$oldname\" to \"$newname\" in \"$dir\""
		mv -f "$dir/$oldname" "$dir/$newname"
}

##############################################################################

# Iterate over all entries in the NAMES array
for entry in "${NAMES[@]}" ; do
	# Split the entry into smaller pieces.
	tmpname="${entry%%:*}"
	dir="${tmpname%/*}"
	oldname="${tmpname##*/}"
	newname="${entry##*:}"

	if [ "${dir%%/*}" = "LANGUAGE" ] ; then
		# If the directory name has the string "LANGUAGE" in it then
		# iterate over the LANGUAGES variable and insert its entries
		# at the place of "LANGUAGE" in the directory name.
		for language in $LANGUAGES ; do
			tmpdir="$language/${dir#*/}"
			rename_file "$tmpdir" "$oldname" "$newname"
		done
	else
		# Normal directory name, just rename
		rename_file "$dir" "$oldname" "$newname"
	fi
done

