#!/bin/bash

# parameters
dir_in="experiments_current_1"
# cost parameters
max_job_cost=20000
const_job_cost=80000000

for pv in "25" "30" "35" "40" "45" "50" "55" "60" "65" "70" "75" "80" "85" "90" "95"
do
	upv=$((100 - ${pv}))
	p="0.${upv},0.${pv}"
	echo ${p}
	# main
	for ext in 9 # Extension
	do
		fileno=0
		current_job_cost=0
		fileout_prefix="experiments_current1_input_${pv}_t${ext}"
		echo "::CHECK: starting ..." >> ${fileout_prefix}_${fileno}.ied
		for f in `ls ${dir_in}/*.ied`
		do
			if [[ "$f" =~ .*_n([0-9]*)_w([0-9]*).*_l([0-9]*)\.ied.*$ ]]; 
			then
				echo "<- Read from $f"
				n=${BASH_REMATCH[1]};
				w=${BASH_REMATCH[2]};
				#s=${BASH_REMATCH[3]}; # -->
				s=$((20+${w})) # 2^20 = 1Mega de case memoires (2 entiers de 64 bits)
				l=${BASH_REMATCH[3]};
				if [[ "${s}" -gt "${l}" ]] ;
				then
					s=${l}
				fi

				if [[ ${n} -gt "3" && ${w} -gt "9" && ${w} -lt "17" && ${l} -gt "45" && ${l} -lt "65" ]] ;
				then
					# approx waiting-time in r :
					r=$((${const_job_cost}/(${w}*${n}*${n}*${l})))

					# establish a cost limit for each run :
					current_job_cost=$((${current_job_cost} + ${w}*${n}*${l}))
					if [[ "$current_job_cost" -gt "$max_job_cost" ]] ;
					then
						current_job_cost=0
						fileno=$(($fileno+1))
						echo "-> Output to file ${fileout_prefix}_${fileno}.ied"
						echo "::CHECK: starting ..." >> ${fileout_prefix}_${fileno}.ied
					fi

					# inject the best results
					#FIXME >>
					cat ${f} >> ${fileout_prefix}_${fileno}.ied
					#FIXME <<
					echo ": -spaced -BSymbols 01 -f ${p} -b 0.5,0.5 -n ${n} -w ${w},${w} -s ${w},${s} -l ${l} -p -r ${r} -z 10 " >> ${fileout_prefix}_${fileno}.ied 
				fi
			else
				echo "::Error : Not proper format"
			fi
		done
	done
done
