# cleaning tmp directory
rm -fr tmp/*
mkdir tmp/split

echo '****************************************************************'
echo '*** look for the near-factorizations of a given group        ***'
echo '****************************************************************'

echo
echo '************************* input section ************************'

# input nbProcess
echo 'give number of processes to be used for the main computation [1]'
read nbProcess
if [ 'a'$nbProcess = 'a' ] # concatenation of 'a' to avoid problem of empty string
then
nbProcess=1
fi

# input : n
echo 'give the order of the group'
read n
echo $n > tmp/n
if [ ! -f ./groups/group.$n.number ];
then
echo '! this order is not available !'
echo 'please first use the script preprocessing_with_gap (GAP is required) !'
exit 0 ;
fi 

# input : i
i=1
echo $i > tmp/i
read number < ./groups/group.$n.number
echo 'there are '$number' distinct groups of order '$n
if [ ! $number -eq 1 ];
then
echo 'give the number of the group of order '$n
read i
echo $i > tmp/i
if [ $i -gt $number ]
then
echo 'number out of range, exiting'
exit 0 ;
fi
fi

# input : omega
echo 'give the size of A of the near-factorizations (A,B)'
read omega
echo $omega > tmp/omega
if [ ! $(($n % $omega)) -eq 1 ];
then
echo '! input size of A is not correct : exiting'
exit 0 ;
fi 

read tmpsym < groups/group.$n.$i.abelian_flag
if [ $tmpsym -eq 1 ]
then 
echo 'warning: abelian group!'
fi

echo
echo '********* selection of filters to be applied *******'

# input : cut of search space
choixCut='n'
factorCut=0
echo 'Cut of search space? [n]'
read ans
if [ 'a'$ans = 'ay' ] # concatenation of 'a' to avoid problem of empty string
then
    choixCut='y'
    echo 'Input max number of entries in input file [1000]'
    read ansbis
    factorCut=$ansbis
    if [ 'a'$ansbis = 'a' ] 
    then
    factorCut=1000
    fi
fi

# input : equipartition filter
choix='y'
if [ -f ../precalculated_groups/group.$n.$i.equipartition_filter ]
then
    echo 'use equipartition filter (y/n)? [y]'
    read ans
    if [ 'a'$ans = 'an' ] # concatenation of 'a' to avoid problem of empty string
    then 
    choix='n'
    fi
fi

# input : dispersion filter
choixbis='n'
if [ -f ../precalculated_groups/group.$n.$i.dispersion_filter ]
then
echo 'use dispersion filter (y/n)? [n]'
read ans
if [ 'a'$ans = 'ay' ]
then 
choixbis='y'
fi
fi

# input : automorphism filter
choixbisbis='n'
if [ -f ../precalculated_groups/group.$n.$i.automorphisms ]
then
echo 'warning: automorphism filter is a very expensive filter: use it only for very small values of the order of the group and size of A'
echo 'use automorphisms filter (y/n)? [n]'
read ans
if [ 'a'$ans = 'ay' ]
then 
choixbisbis='y'
fi
fi

# input : intersection filter
choixbisbisbis='n'
read cyc < ./groups/group.$n.$i.cyclic_flag 
if 
[ $cyc -eq 1 ] 
then
echo 
echo 'Group number '$i' of order '$n' is a cyclic group'
echo 'Warning: Intersection filter keeps subsets A such that INT(A)<=|A|-4, it is of interest only when looking for a counter-example to Grinstead s Conjecture: if applied near-factorizations (A,B) such that INT(A)>=|A|-3 will be missing'
echo 'use intersection filter (y/n)? [n]'
read ans
if [ 'a'$ans = 'ay' ]
then 
choixbisbisbis='y'
fi
fi

# input : force symmetry
sym=0
echo 'look only symmetric near-factorizations (y/n)? [n]'
read ans
if [ 'a'$ans = 'ay' ]
then 
sym=1
fi
echo $sym > tmp/sym

echo
echo '******** output storage selection ******'
echo '0' > tmp/redirection
echo 'store results in a file (y/n)? [n]'
read redirection
if [ 'a'$redirection = 'ay' ]
then
echo '1' > tmp/redirection
fi

rm -f tmp/erreur

# write selections in file tmp/datas

if [ $choixCut = 'y' ]
then
echo '1' > tmp/datas # randomized cut of search space, flag
else
echo '0' > tmp/datas # randomized cut of search space, flag
fi
echo $factorCut >> tmp/datas

echo $sym >> tmp/datas # symmetric restriction flag

if [ $choix = 'y' ]
then 
echo '1' >> tmp/datas # use equipartition filter
else
echo '0' >> tmp/datas # do not use equipartition filter
fi

if [ $choixbis = 'y' ]
then 
echo '1' >> tmp/datas # use dispersion filter
else
echo '0' >> tmp/datas # do not use dispersion filter
fi

if [ $choixbisbisbis = 'y' ]
then 
echo '1' >> tmp/datas # use intersection filter
else
echo '0' >> tmp/datas # do not use intersection filter
fi

echo $n >> tmp/datas # write order of the group
echo $omega >> tmp/datas # write size of A

echo
echo '************************* output section ************************'
echo 'group number '$i' of order '$n', size of A='$omega
if [ $sym -eq 1 ]
then 
echo 'restriction to symmetric subsets activated'
fi
if [ $choix = 'y' ]
then
echo 'equipartition filter activated'
fi
if [ $choixbis = 'y' ]
then
echo 'dispersion filter activated'
fi
if [ $choixbisbis = 'y' ]
then
echo 'automorphisms filter activated'
fi
if [ $choixbisbisbis = 'y' ]
then
echo 'intersection filter activated'
fi

# input file generation (candidates for A)
echo 'generating input file'

cat ./groups/group.$n.$i >> tmp/datas # Cayley table of group processed

if [ $choix = 'y' ] # equipartition filter must be applied
then 
cat ../precalculated_groups/group.$n.$i.equipartition_filter >> tmp/datas
fi

if [ $choixbis = 'y' ] # dispersion filter must be applied
then 
cat ../precalculated_groups/group.$n.$i.dispersion_filter >> tmp/datas
fi

cat tmp/datas | ./bin/generate_input_file > tmp/input_file
echo 'input file generated: number of cases to be investigated='
./basic_scripts/print_number_of_lines tmp/input_file
rm -f tmp/datas

# input file traitment

if [ $choixbisbis = 'y' ] # automorphisms filter must be applied
then
echo $n > tmp/datas
echo $omega >> tmp/datas
cat ../precalculated_groups/group.$n.$i.automorphisms >> tmp/datas
cat tmp/input_file >> tmp/datas
rm -f tmp/input_file
./bin/automorphisms_filter < tmp/datas > tmp/input_file 
rm -f tmp/datas
echo 'input file after automorphisms filter: number of cases to be investigated='
./basic_scripts/print_number_of_lines tmp/input_file
fi

cd tmp
echo 'splitting input file in small blocks'
../basic_scripts/split_input_file input_file
echo 'split over'

echo 'preprocessing input file for multiprocessing'
echo $nbProcess > nbProcess
../basic_scripts/multiprocessing_init

touch output.$n.$i # create file output 




