#!/bin/bash
p="0.2,0.8"

# SEED, SOFTWARE, AND SEARCH PARAMETERS
n_min=4
n_max=4
w_min=24
w_max=24
l_min=1
l_max=64
r=1

AWK='awk'
SED='sed'
CAT='cat'
GREP='grep'
SORT='sort'
UNIQ='uniq'

for ((n=${n_min}; n<=${n_max}; n++))
do
	for ((w=${w_min}; w<=${w_max}; w+=2))
	do
		for ((l=${l_min}; l<=${l_max}; l++))
		do

	                s=$(($w + 20));

	                if [ ${s}  -gt  ${l} ];
        	        then
				s=${l}
	                fi
			if [ ${s} -le ${w} ];
			then
				continue
			fi

	                # run iedera in incremental (-r -k ) / full mode (no -r and no -k) / reoptimization mode (find seeds from other alignment lengths ... and reinject them)
        	        outfile="./_n${n}_w${w}_l${l}.ied";
                        w_1=$(($w - 1))
                        n_1=$(($n - 1))
                        ${CAT} ./experiments_current_1/_n${n}_w${w}_l*.ied ./experiments_current_2/_n${n}_w${w}_l*.ied | ${GREP} -E "^1(0*1){${w_1}}(,1(0*1){${w_1}}){${n_1}}\s" |  ${GREP} -vE "^$" | ${SED} 's/^\([10,]*\).*$/\1/' | ${SORT} | ${UNIQ} | (
                                while read line;
                                do
                                    echo "reoptimize from seed \"${line}\" with probability \"${p}\""
                                    nice -n+19 ./iedera -spaced -BSymbols '01' -f ${p} -b 0.5,0.5 -n ${n} -w ${w},${w} -s ${w},${s} -l ${l} -r ${r} -k -e ${outfile} -o ${outfile} -p -m ${line} 2> /dev/null
                                done
                            )
			mv -f ${outfile} ./experiments_current_2
		done
	done
done
