#!/bin/bash

###################################################################
##this script prepare .txt for train for run on
##because at times, you'd rather have only a subset of examples
##.txt is a subset of .allIhave 
##it depends on prepareExamples.sh
###################################################################

function showCommon {
	result=$(diff $1 $2 --new-line-format="" --old-line-format="" --unchanged-line-format="%L" | wc -l)
	if [ $result -gt 0 ]
	then
		echo -e "Error: there is an example used by both $1 and $2."
		exit
	fi
}

make clean

ncn=newlyCollectedNegatives
echo -e "\nRemove and create $ncn to record negatives."
if [ -d "$ncn" ]
then
	rm -rf $ncn	
fi 
mkdir $ncn

cd trainImages 
./nonFaceImages.sh
cd ..
cd validImages
./nonFaceImages.sh
cd ..

testP="test_positives"
testN="test_negatives"
trainP="positives"
trainN="negatives"
original="allIhave"
toUse="txt"

rm -f *.$toUse
rm -f *.$original

here=$(pwd)
cd $here/database
./prepareExamples.sh
cd $here

#the two most important parameters
read -p "how many positives among $(cat positives.$original | wc -l) for learning in training? " psize
read -p "how many negatives among $(cat negatives.$original | wc -l) for learning in training? " nsize
read -p "how many positives among $(cat test_positives.$original | wc -l) for validation in training? " tpsize
read -p "how many negatives among $(cat test_negatives.$original | wc -l) for validation in training? " tnsize

#extract a few lines

#train file
sed -n 1,${psize}p $trainP.$original > $trainP.$toUse
sed -n 1,${nsize}p $trainN.$original > $trainN.$toUse
sed -n 1,${tpsize}p $testP.$original > $testP.$toUse
sed -n 1,${tnsize}p $testN.$original > $testN.$toUse

#sanity check so there's multiple use of the same example
showCommon $trainN.$toUse $testN.$toUse
showCommon $trainN.$toUse $trainP.$toUse
showCommon $trainN.$toUse $testP.$toUse
showCommon $trainP.$toUse $testP.$toUse
showCommon $trainP.$toUse $testN.$toUse
showCommon $testN.$toUse $testP.$toUse

echo -e "\nStart Compiling\n"
make OMP=1 
./train > trainLog 2>&1 &
#./train
#mv Detector.cxx Detector.cpp
