#! /bin/bash

##################################################################################
##this script assumes that retrieveData has already put examples into two folders
##record the paths to these images and send them to my parent folder
##if the examples are not PNG, convert them first
##run this script every time the examples are updated or *.allIhave are modified
##################################################################################

sets="
train
test
"

suffix="allIhave"

in_format="gif"
out_format="png"
here=$(pwd)

for set in $sets
do
	cd $set
	if [[ $set == test ]]
	then
		negative="test_negatives.$suffix"
		positive="test_positives.$suffix"
	else
		negative="negatives.$suffix"
		positive="positives.$suffix"
	fi

	#same treatment for both face and nonface
	for dir in $(find $PWD -type d -name "*face*")
	do
		echo -e "\n$dir"
		cd $dir
		echo -e "file count : $(ls * | wc -l)"
		toConvert=$(find . -type f -name "*$in_format" | wc -l)
		echo -e "$in_format count : $toConvert"
		if [ "$toConvert" -ne 0 ] 
		then
			echo -e "Converting ..."
			mogrify -format $out_format *.$in_format
			rm -f *.$in_format
		else
			echo -e "Nothing to convert!"
		fi
		
		#create the path file
		if [[ $dir == *non* ]]
		then
			echo -e "Creating Negative Paths ..."
			find $PWD -type f | sort > $here/$negative 
			#sanity check
			echo -e "$(cat $here/$negative | wc -l) samples recorded in $negative!"
		else
			echo -e "Creating Positive Paths ..."
			find $PWD -type f | sort > $here/$positive
			echo -e "$(cat $here/$positive | wc -l) samples recorded in $positive!"
		fi

		#move up
		mv $here/*.$suffix ../../..
		cd ..
	done
	cd ..
done

echo -e "\nJob Done!"
