#!/bin/bash

# iedera number of trials for each run
r=15000;

# number of process on the computer for the program to sleep
# (this script is run on student computers, so it must stop when someone logs in)
p=105;

# read length
l=34;

# number of runs
for run in $(seq 1 1000)
do

    # A) number of seeds
    for n in 1 2 3 4
    do
	
        # B) seed weight
        for w in 10 11 12 13 14
        do

            # set seed span
            s=$((${w}+17-${n}));
	    mx=$((${l} - ${w}));
	    
            # a) non symetric seed
            echo "$HOSTNAME: lossless indel n:${n} w:${w}";
            filename="results_lossless_indel_n${n}_w${w}.txt";
            touch $filename;
            if [[ `cat $filename` =~ "lossless" ]]
            then
                echo "$filename already found lossless";
            else
                nice -n +19 ./iedera_lossless_indel  -A 3 -B 2 -M \{\{0,1,1\},\{0,0,1\}\} -b 0,0.7,0.3 -BSymbols '-#' -p ${p} -l ${l} -n ${n} -w ${w},${w}  -s ${w},${s} -r ${r} -k -L 4,1,0 -X 7 -fF autogen_lossless_indel.txt -e ${filename} -e backup/${filename} -o ${filename} #2> /dev/null
            fi
	    
            # b) symetric seed
            echo "$HOSTNAME: lossless indel n:${n} w:${w} sym";
            filename="results_lossless_indel_n${n}_w${w}_sym.txt";
            touch $filename;
            if [[ `cat $filename` =~ "lossless" ]]
            then
                echo "$filename already found lossless";
            else
                nice -n +19 ./iedera_lossless_indel  -A 3 -B 2 -M \{\{0,1,1\},\{0,0,1\}\} -b 0,0.7,0.3 -BSymbols '-#' -x -p ${p} -l ${l} -n ${n} -w ${w},${w}  -s ${w},${s} -r ${r} -k -L 4,1,0 -X 7 -fF autogen_lossless_indel.txt -e ${filename} -e backup/${filename} -o ${filename} #2> /dev/null
            fi
	    
	    
            # c) non symetric position restricted seed
            filename="results_lossless_indel_n${n}_w${w}.txt";
            touch $filename;
            if [[ `cat $filename` =~ "lossless" ]]
            then
                # position restricted lengths
                for q in 64 40 32 24 16 14 12 11 10 9 8 7 6 5 4 3 2 1
                do
		    filename="results_lossless_indel_n${n}_w${w}_q${q}.txt";
		    touch $filename;
                    if [[ `cat $filename` =~ "lossless" ]]
                    then
                        echo "$filename already found lossless";
                    else			    
                        # q_s "mixer"
                        q_r=${q};q_s="";
                        for i in $(seq 2 ${n})
                        do
                            rx=$(($RANDOM % ${q_r}));
                            while [ ${rx} -gt ${mx} ]; do
                                rx=$(($RANDOM % ${q_r}));
                            done
                            q_s="${q_s},${rx}";
                            q_r=$((${q_r}-${rx}));
                        done;
                        q_s="${q_r}${q_s}"
			
                        if [ ${q_r} -gt ${mx} ]; then
                            echo "$HOSTNAME: lossless indel n:${n} w:${w} q:${q} = ${q_s} : ABORD";
			    break;
                        else
                            echo "$HOSTNAME: lossless indel n:${n} w:${w} q:${q} = ${q_s} : CONTINUE";
                            nice -n +19 ./iedera_lossless_indel  -A 3 -B 2 -M \{\{0,1,1\},\{0,0,1\}\} -b 0,0.7,0.3 -BSymbols '-#' -p ${p} -l ${l} -n ${n} -w ${w},${w}  -s ${w},${s} -c 34 -q ${q_s} -r ${r} -k -L 4,1,0 -X 7 -fF autogen_lossless_indel.txt -e ${filename} -e backup/${filename} -o ${filename} #2> /dev/null;
                            break;
                        fi
                    fi
                done
            fi
	    
	    
            # d) symetric position restricted seed
            filename="results_lossless_indel_n${n}_w${w}_sym.txt";
            touch $filename;
            if [[ `cat $filename` =~ "lossless" ]]
            then
                # position restrited lengths
                for q in 64 40 32 24 16 14 12 11 10 9 8 7 6 5 4 3 2 1
                do
		    filename="results_lossless_indel_n${n}_w${w}_q${q}_sym.txt";
		    touch $filename;
                    if [[ `cat $filename` =~ "lossless" ]]
                    then
                        echo "$filename already found lossless";
                    else			    
                        # q_s "mixer"
                        q_r=$q;q_s="";
                        for i in $(seq 2 ${n})
                        do
			    rx=$(($RANDOM % ${q_r}));
			    while [ ${rx} -gt ${mx} ]; do
                                rx=$(($RANDOM % ${q_r}));
			    done
			    q_s="${q_s},${rx}";
			    q_r=$((${q_r}-${rx}));
                        done;
                        q_s="${q_r}${q_s}"
			
                        if [ ${q_r} -gt ${mx} ]; then
			    echo "$HOSTNAME: lossless indel n:${n} w:${w} q:${q} = ${q_s} sym : ABORD";
			    break;
                        else
			    echo "$HOSTNAME: lossless indel n:${n} w:${w} q:${q} = ${q_s} sym : CONTINUE";
                            nice -n +19 ./iedera_lossless_indel  -A 3 -B 2 -M \{\{0,1,1\},\{0,0,1\}\} -b 0,0.7,0.3 -BSymbols '-#' -x -p ${p} -l ${l} -n ${n} -w ${w},${w}  -s ${w},${s} -c 34 -q ${q_s} -r ${r} -k -L 4,1,0 -X 7 -fF autogen_lossless_indel.txt -e ${filename} -e backup/${filename} -o ${filename} #2> /dev/null;
                            break;
                        fi
                    fi
                done
            fi
        done
    done
done
