#!/bin/bash

# This file is part of the NML build framework
# NML build framework is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
# NML build framework is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with NML build framework. If not, see <http://www.gnu.org/licenses/>.

# This script can help to fix NML files where case for graphics files is not observed
# which happens often when they're developed on windoze.
# It will try to fix the case of the graphics files in all source pnml files

GFX_FILES=`hg st -A | grep -i 'png' | cut -d\  -f2`
PNML_FILES=`hg st -A | grep -i 'pnml' | cut -d\  -f2`

echo Graphics files: $GFX_FILES
echo ""

for i in $PNML_FILES; do
	for j in $GFX_FILES; do
		GFX_FILE=`basename $j`
		GFX_NAMED_IN_PNML=`grep -o -i ${GFX_FILE} $i | tail -n1`
		if [ -n "${GFX_NAMED_IN_PNML}" ]; then
			if [ "${GFX_NAMED_IN_PNML}" = "${GFX_FILE}" ]; then
				echo "File: $i uses ${GFX_FILE} correctly"
			else
				echo "File: $i uses graphics ${GFX_NAMED_IN_PNML} instead of ${GFX_FILE}. Fixing..."
				sed -i "s/$GFX_NAMED_IN_PNML/${GFX_FILE}/g" $i
			fi
		fi
	done;
done;