#!/bin/bash

ROOT="."

EXT="txt asm c cpp h hpp s"

if [[ $# -ge 1 ]] ; then
	ROOT="$1"
fi

echo "Recoding directory: $ROOT"

r=0

function inext(){
	local j
	for j in $EXT; do
		if [[ "$1" == "$j" ]] ; then
			r=0
			return 
		fi
	done
	r=1
	return 
}

function recode_file(){
	cat "$1" | enconv - | tr -d '\r' > "$1"_RECODE
	mv -f "$1"_RECODE "$1"
}

function recode(){
	cd "$1"
	local i
	for i in *; do
		if [[ -d "$1/$i" ]]; then
			local d=`pwd`
			recode "$d/$i"
			cd "$d"
		else
			local f="$1/$i"
			local e=${f##*.}
			# echo FILE: $f EXT: $e
			inext $e
			# echo $r
			if [[ $r == 0 ]] ; then
				echo "Recode $1/$i"
				recode_file "$i"
			fi
		fi
	done
}

recode "$ROOT"
