#!/bin/bash
#Author Martin Prudek

#this script renames all datafiles - usually for changin posion of codes
#in the name
#This script takes no arguments and should be run from its directory
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m'

if [ "$1" == "-d" ]; then
	dry=true;
else
	dry=false;
fi

#startBN=1;

	for file in `ls ../data/*/*.rdat`; do
                filename=`echo "${file}" | sed 's:.*/::'`;
		. ./parse_name.sh "${filename}";

		if [  ${#nm[t]} -eq 0 ]; then 	nm[t]="lat"; fi
		if [  ${#nm[hw]} -eq 0 ]; then   nm[hw]="RPi-v3"; fi
		if [  ${#nm[lo]} -eq 0 ]; then 	nm[lo]="No"; fi
		if [  ${#nm[k]} -eq 0 ]; then 	nm[k]="Unknown"; fi
		if [  ${#nm[p]} -eq 0 ]; then 	nm[p]="40"; fi
		if [  ${#nm[ert]} -eq 0 ]; then  nm[ert]="MTW"; fi
					 	nm[m]="CF";
		if [  ${#nm[cn]} -eq 0 ]; then 	nm[cn]="No"; fi
		if [  ${#nm[si]} -eq 0 ]; then 	nm[si]="Unknown"; fi
		if [  ${#nm[sa]} -eq 0 ]; then   nm[sa]="Unknown"; fi
		if [  ${nm[ert]} = "CTU" ]; then
			nm[m]='';
			nm[cn]='';
		fi
		if [ "`echo "${nm[n]}" | grep -o "ert-linux"`" = "ert-linux" ]; then
			nm[m]='';
			nm[cn]='';
			nm[ert]="CTU";
		fi
		#add default kernel version for measurements taken before kernel 4.9
		if [  ${#nm[vk]} -eq 0 ]; then
			if [  ${nm[k]} = "RT" ];	then   nm[vk]="4.6"; fi
			if [  ${nm[k]} = "non-RT" ]; 	then   nm[vk]="4.4"; fi
		fi

# Zync Linux version correction
#		if [  ${nm[hw]} == "Zynq" ]; then
#			nm[vk]="4.9";
#			if [  ${#nm[n]}   -ne 0 ]; then
#				nm[n]="NTP";
#			else
#				nm[n]="${#nm[n]},NTP";
#			fi
#		fi

# Increase the old benchmarks number by 100
#		if [ ! ${#nm[bn]} -eq 0 ]; then
#			nm[bn]=`expr ${nm[bn]} + 100`;
#		fi

# Add benchmarks numbers to file where they were not present
#               if [ ${#nm[bn]} -eq 0 ]; then
#                       	nm[bn]=${startBN};
#			startBN=`expr ${startBN} + 1`;
#               fi


		newname='';
               	if [  ${#nm[t]}   -ne 0 ]; then   newname="${newname}t${nm[t]}";     fi
                if [  ${#nm[hw]}  -ne 0 ]; then   newname="${newname}_hw${nm[hw]}";   fi
                if [  ${#nm[lo]}  -ne 0 ]; then   newname="${newname}_lo${nm[lo]}";   fi
                if [  ${#nm[k]}   -ne 0 ]; then   newname="${newname}_k${nm[k]}";     fi
		if [  ${#nm[vk]}  -ne 0 ]; then   newname="${newname}_vk${nm[vk]}";   fi
                if [  ${#nm[p]}   -ne 0 ]; then   newname="${newname}_p${nm[p]}";     fi
                if [  ${#nm[ert]} -ne 0 ]; then   newname="${newname}_ert${nm[ert]}"; fi
                if [  ${#nm[m]}   -ne 0 ]; then   newname="${newname}_m${nm[m]}";     fi
                if [  ${#nm[cn]}  -ne 0 ]; then   newname="${newname}_cn${nm[cn]}";   fi
                if [  ${#nm[bn]}  -ne 0 ]; then   newname="${newname}_bn${nm[bn]}";   fi
		if [  ${#nm[si]}  -ne 0 ]; then   newname="${newname}_si${nm[si]}";   fi
                if [  ${#nm[sa]}  -ne 0 ]; then   newname="${newname}_sa${nm[sa]}";   fi
		if [  ${#nm[n]}   -ne 0 ]; then   newname="${newname}_n${nm[n]}";     fi
		newname="${newname}_.rdat";
		dir="`echo "${file}" | awk -F / '{print $3}'`";

		if [ ! "${filename}" == "${newname}" ]; then
			if [ -e ../data/${dir}/${newname} ]; then
				echo -e "${RED}!! file with name $dir/${newname} already exists!! Skipping.";
				echo -e "OLD=${filename}${NC}";
			else
				git ls-files "${file}" --error-unmatch >/dev/null 2>/dev/null;
				if [ $? -eq 0 ]; then
					if  ! ${dry}; then
						git mv "../data/${dir}/${filename}" "../data/${dir}/${newname}";
					fi
					echo -e "${GREEN}------GIT TRACKED:---------------------------------"
					echo -e "OLD=${filename}";
					echo -e "NEW=${newname}${NC}";
				else
					if  ! ${dry}; then
						mv -n  "../data/${dir}/${filename}" "../data/${dir}/${newname}";
					fi
					echo "------NOT TRACKED:---------------------------------"
					echo "OLD=${filename}";
					echo "NEW=${newname}";
				fi
			fi
		fi
       	done
