#!/bin/bash
r=5000
create_jobs_flag=false
inject_flag=false
reoptbest_flag=true
reoptbest_shuffle_flag=true

cdir="fixed_current_1"
pdir="${HOME}/projects/iedera/"
odir="${pdir}sample_results/"

# if jobs have to be created, a batch id is needed for the full set
if ${create_jobs_flag} ;
then
	uniq=$(cat /dev/urandom | tr -dc '0-9' | fold -w 8 | head -n 1)
	date=$(date)
	echo "$date  ->  batch=$uniq r=$r" >> batch_ids.txt
fi

for params in n4_w10_l50_f75 n4_w10_l50_f80 n4_w10_l50_f85 n4_w11_l50_f75 n4_w11_l50_f80 n4_w11_l50_f85 n4_w12_l50_f80 n4_w12_l50_f85 n4_w12_l50_f90 n4_w16_l50_f85 n4_w16_l50_f90 n4_w16_l50_f95 n4_w18_l50_f85 n4_w18_l50_f90 n4_w18_l50_f95 n16_w11_l64_f70 n16_w11_l64_f75 n16_w11_l64_f80 n10_w22_l50_f85 n10_w22_l50_f90 n10_w22_l50_f95
#for params in n4_w10_l50_f75 n4_w10_l50_f80 n4_w10_l50_f85 n4_w11_l50_f75 n4_w11_l50_f80 n4_w11_l50_f85 n4_w12_l50_f80 n4_w12_l50_f85 n4_w12_l50_f90 n4_w16_l50_f85 n4_w16_l50_f90 n4_w16_l50_f95 n4_w18_l50_f85 n4_w18_l50_f90 n4_w18_l50_f95
#for params in n16_w11_l64_f70 n16_w11_l64_f75 n16_w11_l64_f80 n10_w22_l50_f85 n10_w22_l50_f90 n10_w22_l50_f95
#for params in n4_w12_l50_f80
do
	if [[ ${params} =~ ^n([0-9]*)_w([0-9]*)_l([0-9]*)_f([0-9]*)$ ]];
	then
		n=${BASH_REMATCH[1]}
		w=${BASH_REMATCH[2]}
		l=${BASH_REMATCH[3]}
		pv=${BASH_REMATCH[4]}
		upv=$((100 - ${pv}))
		printf -v upv "%02d" $upv
		p="0.${upv},0.${pv}"
		s=$((${w} + 20))

		# Merge previous results to cdir (remove ":" from each file before merging) and clean the file each time
		echo "n:${n} w:${w} l:${l} p:${pv}"
		cat ${cdir}/_n${n}_w${w}_l${l}_f0.${pv}.txt
		for srfile in `ls ${odir}*fixed_n${n}_w${w}_l${l}_f0.${pv}* 2> /dev/null`
		do
			zcat ${srfile} | grep -v ":" >> ${srfile}.tmp
			mv -f ${srfile}.tmp ${srfile}
			./iedera -spaced -BSymbols 01 -n ${n} -w ${w},${w} -s ${w},${s} -b 0.5,0.5 -f ${p} -l ${l} -r 1 -k -e ${srfile} -e ${cdir}/_n${n}_w${w}_l${l}_f0.${pv}.txt -o ${cdir}/_n${n}_w${w}_l${l}_f0.${pv}.txt 2> /dev/null 1> /dev/null
			rm -f ${srfile}
		done
		cat ${cdir}/_n${n}_w${w}_l${l}_f0.${pv}.txt

		if ${create_jobs_flag} ;
		then
			for tag in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
			do
				fileout="fixed_n${n}_w${w}_l${l}_f0.${pv}_r${r}_inject${inject_flag}_reopt${reoptbest_flag}_batch${uniq}_t${tag}"
				rm -f ${pdir}${fileout}

				# Create the command line
				echo "::CHECK: starting ..." >> ${pdir}${fileout}

				# injecting or not injecting previous results ... here, that is the question
				if ${inject_flag} ;
				then
					cat  ${cdir}/*_n${n}_w${w}_l${l}_f0.${pv}.txt >> ${pdir}${fileout}
				fi

				# with the previous seed before to reopt first, or start from any seed
				if ${reoptbest_flag} ;
				then
					# keep only the seed part
					seed=$(cat ${cdir}/*_n${n}_w${w}_l${l}_f0.${pv}.txt | sed 's/^\([01,]*\).*$/\1/')
					# and shuffle seed order and elements
					seed=$(echo ${seed} | tr ',' '\n' | shuf | tr '\n' ',') # shuffle seed order
					seed=${seed%?}
					if ${reoptbest_shuffle_flag} ;
					then
						r1=$(( $RANDOM % ${w} + 1));
						r2=$(( $RANDOM % ${w} + 1));
						ev=$(( $RANDOM % 2 ));
						if [[ ${ev} -eq "0" ]] ;
						then
							seed=$(echo ${seed} | sed 's/^\([01]\{'"${r1}"'\}[0]*\)[1]\([01]\{'"${r2}"'\}[1]*\)[0]/\10\21/')
						else
							seed=$(echo ${seed} | sed 's/^\([01]\{'"${r1}"'\}[1]*\)[0]\([01]\{'"${r2}"'\}[0]*\)[1]/\11\20/')
						fi
					fi
					echo ": -spaced -BSymbols 01 -n ${n} -w ${w},${w} -s ${w},${s} -b 0.5,0.5 -f ${p} -l ${l} -r ${r} -k -m ${seed}" >> ${pdir}${fileout}
				else
					echo ": -spaced -BSymbols 01 -n ${n} -w ${w},${w} -s ${w},${s} -b 0.5,0.5 -f ${p} -l ${l} -r ${r} -k" >> ${pdir}${fileout}
				fi

				# Send the job
				cd ${pdir}
				./bin/stage_file ${pdir}${fileout}
				./bin/create_work --batch ${uniq} -appname iedera -min_quorum 1 -target_nresults 1 -wu_name ${fileout} -wu_template templates/wrapper_in -result_template templates/wrapper_result ${fileout}
				cd ${HOME}
			done
		fi
	else
		echo "Not proper format";
	fi
done
