#!/bin/bash

SYSTEM_JOIN=~/src/coreutils-8.23/src/join

for source in `ls *.{c,1}`
do 
	./sort $source > ${source}_SORTED_1.txt
	./sort $source > ${source}_SORTED_2.txt

	LANG="C" ${SYSTEM_JOIN} ${source}_SORTED_1.txt ${source}_SORTED_2.txt > ${source}_out1
	./join ${source}_SORTED_1.txt ${source}_SORTED_2.txt > ${source}_out2
	echo "=== ./join ${source}_SORTED_1.txt ${source}_SORTED_2.txt ==="
	diff -u ${source}_out1 ${source}_out2

	LANG="C" ${SYSTEM_JOIN} -e 'asdf' ${source}_SORTED_1.txt ${source}_SORTED_2.txt > ${source}_out1
	./join -e 'asdf' ${source}_SORTED_1.txt ${source}_SORTED_2.txt > ${source}_out2
	echo "=== ./join -e 'asdf' ${source}_SORTED_1.txt ${source}_SORTED_2.txt ==="
	diff -u ${source}_out1 ${source}_out2

	./sort -k 1b,1 $source > ${source}_SORTED_1.txt
	./sort -k 2b,2 $source > ${source}_SORTED_2.txt

	LANG="C" ${SYSTEM_JOIN} -1 1 -2 2 ${source}_SORTED_1.txt ${source}_SORTED_2.txt > ${source}_out1
	./join -1 1 -2 2 ${source}_SORTED_1.txt ${source}_SORTED_2.txt > ${source}_out2
	echo "=== ./join -1 1 -2 2 ${source}_SORTED_1.txt ${source}_SORTED_2.txt ==="
	diff -u ${source}_out1 ${source}_out2

	./sort -k 3b,3 $source > ${source}_SORTED_1.txt
	./sort -k 5b,5 $source > ${source}_SORTED_2.txt

	LANG="C" ${SYSTEM_JOIN} -1 3 -2 5 ${source}_SORTED_1.txt ${source}_SORTED_2.txt > ${source}_out1
	./join -1 3 -2 5 ${source}_SORTED_1.txt ${source}_SORTED_2.txt > ${source}_out2
	echo "=== ./join -1 3 -2 5 ${source}_SORTED_1.txt ${source}_SORTED_2.txt  ==="
	diff -u ${source}_out1 ${source}_out2


	rm ${source}_SORTED_1.txt
	rm ${source}_SORTED_2.txt
	rm ${source}_out1 ${source}_out2
done

for field in 1 2 5 1000
do
	for source in `ls *.{c,1}`
	do 
		./sort -k ${field}b,${field} $source > ${source}_SORTED.txt
		./sort -t { -k ${field}b,${field} $source > ${source}_SORTED2.txt

		LANG="C" ${SYSTEM_JOIN} -1 ${field} -2 ${field} ${source}_SORTED.txt ${source}_SORTED.txt > ${source}_out1
		./join -1 ${field} -2 ${field} ${source}_SORTED.txt ${source}_SORTED.txt > ${source}_out2
		echo "=== ./join -1 ${field} -2 ${field} ${source}_SORTED.txt ${source}_SORTED.txt ==="
		diff -u ${source}_out1 ${source}_out2

		LANG="C" ${SYSTEM_JOIN} -1 ${field} -2 ${field} -a 1 ${source}_SORTED.txt ${source}_SORTED.txt > ${source}_out1
		./join -1 ${field} -2 ${field} -a 1 ${source}_SORTED.txt ${source}_SORTED.txt > ${source}_out2
		echo "=== ./join -1 ${field} -2 ${field} -a 1 ${source}_SORTED.txt ${source}_SORTED.txt ==="
		diff -u ${source}_out1 ${source}_out2

		LANG="C" ${SYSTEM_JOIN} -1 ${field} -2 ${field} -a 1 -t { ${source}_SORTED2.txt ${source}_SORTED2.txt > ${source}_out1
		./join -1 ${field} -2 ${field} -a 1 -t { ${source}_SORTED2.txt ${source}_SORTED2.txt > ${source}_out2
		echo "=== ./join -1 ${field} -2 ${field} -a 1 -t { ${source}_SORTED2.txt ${source}_SORTED2.txt ==="
		diff -u ${source}_out1 ${source}_out2

		LANG="C" ${SYSTEM_JOIN} -e 'asdf' -1 ${field} -2 ${field} -a 1 -t { ${source}_SORTED2.txt ${source}_SORTED2.txt > ${source}_out1
		./join -e 'asdf' -1 ${field} -2 ${field} -a 1 -t { ${source}_SORTED2.txt ${source}_SORTED2.txt > ${source}_out2
		echo "=== ./join -e 'asdf' -1 ${field} -2 ${field} -a 1 -t { ${source}_SORTED2.txt ${source}_SORTED2.txt ==="
		diff -u ${source}_out1 ${source}_out2

		LANG="C" ${SYSTEM_JOIN} -1 ${field} -2 ${field} -a 2 ${source}_SORTED.txt ${source}_SORTED.txt > ${source}_out1
		./join -1 ${field} -2 ${field} -a 2 ${source}_SORTED.txt ${source}_SORTED.txt > ${source}_out2
		echo "=== ./join -1 ${field} -2 ${field} -a 2 ${source}_SORTED.txt ${source}_SORTED.txt ==="
		diff -u ${source}_out1 ${source}_out2

		rm ${source}_SORTED.txt
		rm ${source}_SORTED2.txt
		rm ${source}_out1 ${source}_out2
	done
done

./sort -k3b,3 tr.c > tr.c_SORTED.txt
./sort -k5b,5 xargs.c > xargs.c_SORTED.txt
LANG="C" ${SYSTEM_JOIN} -1 3 -2 5 tr.c_SORTED.txt xargs.c_SORTED.txt > out1
./join -1 3 -2 5 tr.c_SORTED.txt xargs.c_SORTED.txt > out2
echo "=== ./join -1 3 -2 5 tr.c_SORTED.txt xargs.c_SORTED.txt ==="
diff -u out1 out2

LANG="C" ${SYSTEM_JOIN} -1 3 -2 5 -a 1 tr.c_SORTED.txt xargs.c_SORTED.txt > out1
./join -1 3 -2 5 -a 1 tr.c_SORTED.txt xargs.c_SORTED.txt > out2
echo "=== ./join -1 3 -2 5 -a 1 tr.c_SORTED.txt xargs.c_SORTED.txt ==="
diff -u out1 out2

LANG="C" ${SYSTEM_JOIN} -1 3 -2 5 -a 2 tr.c_SORTED.txt xargs.c_SORTED.txt > out1
./join -1 3 -2 5 -a 2 tr.c_SORTED.txt xargs.c_SORTED.txt > out2
echo "=== ./join -1 3 -2 5 -a 2 tr.c_SORTED.txt xargs.c_SORTED.txt ==="
diff -u out1 out2

rm tr.c_SORTED.txt xargs.c_SORTED.txt out1 out2

./sort -k1b,1 sort.c > sort.c_SORTED.txt
./sort -k1b,1 ls.c > ls.c_SORTED.txt
LANG="C" ${SYSTEM_JOIN} sort.c_SORTED.txt ls.c_SORTED.txt > out1
./join sort.c_SORTED.txt ls.c_SORTED.txt > out2
echo "=== ./join sort.c_SORTED.txt ls.c_SORTED.txt ==="
diff -u out1 out2
rm sort.c_SORTED.txt ls.c_SORTED.txt out1 out2

cat *.{c,1} | ./sort -k 1b,1 > sources_SORTED.txt
LANG="C" ${SYSTEM_JOIN} sources_SORTED.txt sources_SORTED.txt > all_out1
./join sources_SORTED.txt sources_SORTED.txt > all_out2
echo "=== ./join sources_SORTED.txt sources_SORTED.txt ==="
diff -u all_out1 all_out2
rm sources_SORTED.txt all_out1 all_out2
