#!/bin/bash

echo "-------------------------------------------------"
echo " UPGRADING SCRIPT:"
echo "All data XML files in FARM/data folder are going to be upgraded"
echo "Master files will not be modified but the upgraded files will be stored in FARM/script/upgraded_data"
echo "After this process, upgraded data should be copied to data folder and overwrite the files with earlier versions"
echo "-------------------------------------------------"

rm -Rf ./upgraded_data
mkdir ./upgraded_data


for f in `cd ../data && ls *.xml`
do
	is_version_1_0=`cat ../data/"$f" | grep -c 'http://gilmere.upf.edu/theharvestingday/schemas/clarin_bamdes.xsd'`  
	if [ "$is_version_1_0" == "1" ]
	then
		echo "Upgrading $f from 1.0 to 1.1"
		xsltproc ./lib/bamdes10_to_bambes11.xsl ../data/$f > ./upgraded_data/$f 
	else
		echo "Copy unmodified $f to upgraded_data folder."
		cp ../data/$f ./upgraded_data/$f
	fi
done

echo "-------------------------------------------------"
echo " NEXT STEP:"
echo "All upgraded data is in folder FARM/script/upgraded_data . "
echo "Please copy the content of this folder into the FARM/data folder to finish the upgrade. "
echo "Just in case keep a copy of the old data XML files so you can undo the changes."
echo "-------------------------------------------------"
