#!/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
   
    # 0) number of hits
    for y in 2
    do

        # A) "err_snp" model
        model_i=0;

        for model in "1e_1snp" "2e_1snp" "3e_1snp"
        do
            model_i=$(($model_i + 1));
            x=$(($model_i + 2))

            # B) number of seeds
            for n in 1 2 3 4
            do

                # C) seed weight
                for w in 10 11 12 13 14
                do

                    # set seed span
                    sm=$((${w}+0));
                    s=$((${w}+17-${n}));
                    mx=$((${l} - ${w}));

                    # a) non symetric seed
                    echo "$HOSTNAME: lossless ${model} n:${n} w:${w} y:${y}";
                    filename="results_lossless_${model}_n${n}_w${w}_y${y}.txt";
                    touch $filename;
                    if [[ `cat $filename` =~ "lossless" ]]
                    then
                        echo "$filename already found lossless";
                    else
                        nice -n +19 ./iedera_lossless -spaced -b 0.7,0.3 -p ${p} -l ${l} -n ${n} -w ${w},${w}  -s ${sm},${s} -r ${r} -k -L 1,0 -X ${x} -fF autogen_lossless_${model}.txt -e ${filename} -e backup/${filename} -o ${filename} -y ${y} 2> /dev/null
                    fi

                    # b) symetric seed
                    echo "$HOSTNAME: lossless ${model} n:${n} w:${w} y:${y} sym";
                    filename="results_lossless_${model}_n${n}_w${w}_y${y}_sym.txt";
                    touch $filename;
                    if [[ `cat $filename` =~ "lossless" ]]
                    then
                        echo "$filename already found lossless";
                    else
                        nice -n +19 ./iedera_lossless -spaced -x -b 0.7,0.3 -p ${p} -l ${l} -n ${n} -w ${w},${w}  -s ${sm},${s} -r ${r} -k -L 1,0 -X ${x} -fF autogen_lossless_${model}.txt -e ${filename} -e backup/${filename} -o ${filename} -y ${y} 2> /dev/null
                    fi


                    # c) non symetric position restricted seed
                    filename="results_lossless_${model}_n${n}_w${w}_y${y}.txt";
                    touch $filename;
                    if [[ `cat $filename` =~ "lossless" ]]
                    then
                        # position restricted lengths
                        for q in 64 40 32 24 20 16 14 12 11 10 9 8 7 6 5 4 3 2 1
                        do
                            filename="results_lossless_${model}_n${n}_w${w}_y${y}_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 ${model} n:${n} w:${w} y:${y} q:${q} = ${q_s} : ABORD";
                                else
                                    echo "$HOSTNAME: lossless ${model} n:${n} w:${w} y:${y} q:${q} = ${q_s} : CONTINUE";
                                    nice -n +19 ./iedera_lossless -spaced -b 0.7,0.3 -p ${p} -l ${l} -n ${n} -w ${w},${w}  -s ${sm},${s} -c 34 -q ${q_s} -r ${r} -k -L 1,0 -X ${x} -fF autogen_lossless_${model}.txt -e ${filename} -e backup/${filename} -o ${filename} -y ${y} 2> /dev/null;
                                    break;
                                fi
                            fi
                        done
                    fi


                    # d) symetric position restricted seed
                    filename="results_lossless_${model}_n${n}_w${w}_y${y}_sym.txt";
                    touch $filename;
                    if [[ `cat $filename` =~ "lossless" ]]
                    then
                        # position restrited lengths
                        for q in 64 40 32 24 20 19 16 14 12 11 10 9 8 7 6 5 4 3 2 1
                        do
                            filename="results_lossless_${model}_n${n}_w${w}_y${y}_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 ${model} n:${n} w:${w} y:${y} q:${q} = ${q_s} sym : ABORD";
                                else
                                    echo "$HOSTNAME: lossless ${model} n:${n} w:${w} y:${y} q:${q} = ${q_s} sym : CONTINUE";
                                    nice -n +19 ./iedera_lossless -spaced -b 0.7,0.3 -p ${p} -x -l ${l} -n ${n} -w ${w},${w}  -s ${sm},${s} -c 34 -q ${q_s} -r ${r} -k -L 1,0 -X ${x} -fF autogen_lossless_${model}.txt -e ${filename} -e backup/${filename} -o ${filename} -y ${y} 2> /dev/null;
                                    break;
                                fi
                            fi
                        done
                    fi
                done
            done
        done
    done
done
