#!/bin/bash

xcode=0
onlyBase=0

cd "${0%/*}"

for var in "$@"
do
	if [ "$var" == "-xcode" ]; then
		xcode=1
		continue
	elif [ "$var" == "-onlyBase" ]; then
		onlyBase=1
		continue
	fi
done

if [ $xcode -eq 0 ]; then
	GREEN='\033[0;32m'
	ORANGE='\033[0;33m'
	RED='\033[0;31m'
	YELLOW='\033[1;31m'
	WHITE='\033[1;37m'
	NC='\033[0m' # No Color
fi

someError=0
someNoUTF8=0

function checkLanguageFolders {
	f="${1}"
	if [ ! -d ${f} ]; then
		continue
	fi
	if [ $onlyBase -eq 1 ] && [[ ${f} != "Base.strings" ]]; then
		continue
	fi
	for t in ${f}/*.strings; do
		convertStringsFile "${f}" "${t}" &
	done
	wait
}

function convertStringsFile {
	f="${1}"
	t="${2}"
	md5="$(md5 "${t}" | cut -d'=' -f 2)"
	md5="${md5## }"
	md5file=${f}/.${t#${f}/}.conversion.md5
	if [ ! -f "${md5file}" ]; then
		echo $md5 > "${md5file}"
	else
		md5old="$(cat "${md5file}")"
		if [[ $md5 == $md5old ]]; then
			unset md5old
			continue
		else
			echo $md5 > "${md5file}"
		fi
	fi
	n=${t#$f/} # file name
	text="${f}/${n}"
	enc="$( file -I ${t} )"
	charset="$( echo ${enc} | cut -d'=' -f2 )"
	noUTF8=0	
	if [[ ${charset} != "utf-8" ]]; then
		noUTF8=1
		text="${text} > ${ORANGE}${charset}${NC}"
		convFrom=""
		if [[ "$charset" != "binary" ]]; then
			convFrom="-f ${charset}"
		fi
		conv="$( { iconv $convFrom -t utf-8 < ${t} > ${t}.tmp; } 2>&1 )"
		echo $md5 > "${md5file}"
		if [ $? -eq 0 ]; then
			mv -f ${t}.tmp ${t}
			text="${text} > ${GREEN}UTF-8${NC}"
		else
			someNoUTF8=1
			rm "${md5file}"
			text="${text} > ${RED}${charset} !!!${NC} > ${ORANGE}${conv}${NC}"
		fi
		echo -e "${text}"
	else
		continue
	fi
	error=0
	if [[ ${enc} = *"regexec error"* ]]; then
		error=1
		someError=1
		rm "${md5file}"
		encErr="$( echo ${enc#*"ERROR: "} | cut -d';' -f1 )"
		echo -e "${text} > ${RED}Error: ${ORANGE}${encErr}${NC}"
	fi
}

for fff in *.*; do
	checkLanguageFolders "${fff}" &
done
wait

if [ $someNoUTF8 -eq 1 ] && [ $someError -eq 1 ]; then
	echo -e "${RED}There are some files not encoded in UTF-8 and with errors${NC}"
	exit 1
elif [ $someNoUTF8 -eq 1 ]; then
	echo -e "${RED}There are some files not encoded in UTF-8${NC}"
	exit 1
elif [ $someError -eq 1 ]; then
	echo -e "${RED}There are some files with some errors${NC}"
	exit 1
fi

exit