#! /bin/bash

RESULTDIR="./results"

SOURCES_DIR=$1


function generate_uniq_file() {

	local source_target_dir=$1

	local source_dir="${source_target_dir%/*}" 

	local eval_dir="${RESULTDIR}/${source_dir##*/}/${source_target_dir##*/}"
	
	local filename="${source_dir##*/}---${source_target_dir##*/}.uniq.txt"
	
	mkdir -p $eval_dir

	local eval_filepath="${eval_dir}/${filename}"

	local targets=`find $source_target_dir -type f -not -name '.*'`

	LC_ALL=C cat ${targets} | sed -r 's/(<.*?>) (<.*?>) <(.*?)> \./\3/g' | LC_ALL=C sort | uniq -c > $eval_filepath
}

sources_dirs=`find $SOURCES_DIR -maxdepth 1 -type d -name "source=*"`

echo "Found sources: ${sources_dirs}"

for sourcedir in $sources_dirs; do


	source_target_dirs=`find $sources_dirs -maxdepth 1 -type d -name "target=*"`
	
	for st_dir in $source_target_dirs; do

		echo "Handling target: ${st_dir}"
		generate_uniq_file $st_dir 

	done
done

