#!/bin/bash
##
# Use: make_jobs.sh $1 $2
# where $1 is the total number of simulations (TOTALSIM) and $2 is the number of simulations in each job (SIMSJOB)

# prepare files
rm class_submit.sh Outputs/class_merge.sh
echo cat >tmp1.tmp

# bash while loop, divide TOTALSIM simulations into batches with SIMSJOB number of simulations inside
COUNT=0
XSUB=1
TOTALSIM=$1
SIMSJOB=$2
while [ $COUNT -lt $TOTALSIM ]; do
	let COUNT0=COUNT+1
	let COUNT=COUNT+SIMSJOB
        sed -n "$COUNT0","$COUNT"p matrix.txt > matrix$XSUB.tmp
        cat class_GSA.job.top matrix$XSUB.tmp class_GSA.job.bottom  > class_GSA$XSUB.tmp
        sed s/CACA/$XSUB/g < class_GSA$XSUB.tmp >class_GSA$XSUB.job
	echo Value of count is: $XSUB $COUNT0 $COUNT
	echo 'sbatch' class_GSA$XSUB.job >>class_submit.sh 
	echo class_GSA$XSUB.out >>class_merge.tmp
	let XSUB=XSUB+1
done 
echo 'cat ' > head.tmp
echo '>output.all' >>class_merge.tmp
cat head.tmp class_merge.tmp | tr '\n' ' ' > Outputs/class_merge.sh
echo -e "\nsed 's/;/ /g' <output.all>output_all.txt" >> Outputs/class_merge.sh
# clean up!
rm *.tmp
