#!/bin/bash
# -*- coding: utf-8 -*-

WD=$(pwd)
#define output directory
OUTD01="PDVout06"
#remove output directory
rm -rf "$OUTD01"
rm -rf "$OUTD01"_many_HKY.tar.gz
#make output directory
mkdir "$OUTD01"
#remove previous versions of the file
rm *ss.xml
#itearet over files 
for file in *HKY*.xml
do
	#modify the file names
	nwfnm=$(echo $file | sed 's/\.xml/ss\.xml/g')
	#get the model names in the file names
	modnm=$(echo $file | sed -E 's/PDVpos12_(.c*.*)\.xml/\1/g')
	#define individual directories
	OUTD02NM=$(echo $modnm | sed -E 's/^/PDVpos12_/g')
	OUTD03NM=$(echo "$OUTD02NM" | sed 's/$/ss/g')
	#echo "$OUTD03NM"
	#make a new directory for each xml file
	mkdir "$WD"/"$OUTD01"/"$OUTD02NM"
	mkdir "$WD"/"$OUTD01"/"$OUTD03NM"
	#copy the input file to this directory
	cp "$file" "$WD"/"$OUTD01"/"$OUTD02NM"/.
	#write a new output file to write to
  	touch "$WD"/"$OUTD01"/"$OUTD03NM"/"$nwfnm"
  	iconv -f UTF-8 -t UTF-8 "$WD"/"$OUTD01"/"$OUTD03NM"/"$nwfnm"


  	# #write a new output tmp file to write to
  	# touch "$WD"/"$OUTD01"/tmplist.txt
  	# iconv -f UTF-8 -t UTF-8 "$WD"/"$OUTD01"/tmplist.txt
  	# #fill this file with file names
  	# echo $file >> "$WD"/"$OUTD01"/tmplist.txt
  	# echo $nwfnm >> "$WD"/"$OUTD01"/tmplist.txt
  	#define different paths for files to be run in
	varpath="/home/sknu003/no_backup_uoa00029/${OUTD01}/${OUTD03NM}/"
	#echo $varpath
	#modify the xml file
	# read individual  file line by line
	#while read LINE
      	#Notice you need the read line function to also read white space to keep indentation
      	# see this website: https://unix.stackexchange.com/questions/131766/why-does-my-shell-script-choke-on-whitespace-or-other-special-characters
    while IFS= read -r line  	
	#start loop over each line in file
	do
		if [[ $line == "</run>"* ]] # if line starts with '</run>''
			then
				#write two new lines
				printf "%s\n" "</mcmc>" >> "$WD"/"$OUTD01"/"$OUTD03NM"/"$nwfnm"
				printf "%s\n" "</run>" >> "$WD"/"$OUTD01"/"$OUTD03NM"/"$nwfnm"
		elif [[ $line == '<run id="mcmc" spec="MCMC"'* ]]; then
			#write a new set of lines
			#notice how $ signs are escaped and " marks also are escaped with backslash
			printf  "%s\n" "<run spec=\"beast.inference.PathSampler\" 
        chainLength=\"1600000\" 
        alpha=\"0.3\" 
        rootdir=\""$varpath"\" 
        burnInPercentage=\"10\" 
        preBurnin=\"10\" 
        deleteOldLogs=\"true\" 
        nrOfSteps=\"112\">
    cd \$(dir) 
    java -cp \$(java.class.path) beast.app.beastapp.BeastMain \$(resume/overwrite) -java -seed \$(seed) beast.xml" >> "$WD"/"$OUTD01"/"$OUTD03NM"/"$nwfnm"
    			#modify the line that has run
				printf "%s\n" "$line" | sed 's/<run/<mcmc/g' >> "$WD"/"$OUTD01"/"$OUTD03NM"/"$nwfnm"
		else # if line doesn't start wsith >, then write the line
			printf "%s\n" "$line" >> "$WD"/"$OUTD01"/"$OUTD03NM"/"$nwfnm"
	  fi
	#end loop over each line in file
	done  < ${file}

	#modify modelname to get new filenames for the slurm submission scripts
	# one slurm submission script for the ordinary xml file
	slurmsubm_fnm01=$(echo $modnm | sed -E 's/^/slurm_submis_nesi_/g' | sed -E 's/$/.sh/g')
	# one slurm submission script for the stepping stone xml file
	slurmsubm_fnm02=$(echo $modnm | sed -E 's/^/slurm_submis_nesi_/g' | sed -E 's/$/ss.sh/g')
	#make a model name for the stepping stone model
	modnmss=$(echo $modnm | sed -E 's/$/ss/g')
	
	ENDCH=$(echo -n $modnm | tail -c -1 )
	#echo $slurmsubm_fnm02
	# write slurm submission scripts to go with each xml file
if [[ "$ENDCH" == "F" ]]
then
	#echo "$ENDCH"
	slurm_hours="24"
	#echo "$slurm_hours"
else
	slurm_hours="6"
	#echo "$slurm_hours"
fi


	printf "#!/bin/bash
#SBATCH -J "$modnm"
#SBATCH -A uoa00029         # Project Account
#SBATCH --time="$slurm_hours":00:00     # Walltime
#SBATCH --mem-per-cpu=8192  # memory/cpu (in MB)
#SBATCH --cpus-per-task=1
#SBATCH --ntasks=1
# # SBATCH --mail-type=ALL
# #SBATCH --mail-user=sknu003@aucklanduni.ac.nz
#SBATCH -o stdout_"$modnm".txt
#SBATCH -e stderr_"$modnm".txt

module purge
module load Java/1.7.0_51
##module load BEAST/2.5.2
module load BEAST/2.6.3

#define input file name
FNAME=\""$file"\"
#start beast run with 'srun'
srun beast -threads 1 -overwrite \$FNAME 
" > "$WD"/"$OUTD01"/"$OUTD02NM"/"$slurmsubm_fnm01"

# write slurm submission scripts to go with each ss xml file
	printf "#!/bin/bash
#SBATCH -J "$modnmss"
#SBATCH -A uoa00029         # Project Account
#SBATCH --time="$slurm_hours":00:00     # Walltime
#SBATCH --mem-per-cpu=12G  # memory/cpu (in MB)
#SBATCH --cpus-per-task=8
#SBATCH --ntasks=1
# # SBATCH --mail-type=ALL
# #SBATCH --mail-user=sknu003@aucklanduni.ac.nz
#SBATCH -o stdout_"$modnmss".txt
#SBATCH -e stderr_"$modnmss".txt

module purge
module load Java/1.7.0_51
##module load BEAST/2.5.2
module load BEAST/2.6.3

#define input file name
FNAME=\""$nwfnm"\"
#start beast run with 'srun'
srun beast -threads 8 -overwrite \$FNAME 
" > "$WD"/"$OUTD01"/"$OUTD03NM"/"$slurmsubm_fnm02"

done

#change directory
cd "$WD"/"$OUTD01"/

#write a single bash script to submit all 
printf	"#!/bin/bash
# -*- coding: utf-8 -*-

WorkDir=\$(pwd)
# Declare an array of string with type
declare -a StringArray=(" > "$WD"/tmp01.txt

for dir in *
do
	cd $dir 
	slsubm=$(ls *.sh)
	 #slsubm2=$(echo $slsubm | sed -E 's/slurm_submis_nesi_(.*).sh/PDVpos12_\1/g') 
	 #echo "$slsubm2"
	cd "$WD"/"$OUTD01"/	
	echo "\"$slsubm\"" >> "$WD"/tmp01.txt
done
printf "	 )
 
# Iterate the string array using for loop
for val in \${StringArray[@]}; do
	slsubm2=\$(echo \$val | sed -E 's/slurm_submis_nesi_(.*).sh/PDVpos12_\\\1/g') 
	cd \$slsubm2

	sbatch \"\$val\"
	cd \$WorkDir
	#echo \"\$val\"
done" >> "$WD"/tmp01.txt


#cat "$WD"/tmp01.txt
mv "$WD"/tmp01.txt "$WD"/"$OUTD01"/bash06b_slurmsubmit_many.sh
chmod 755 "$WD"/"$OUTD01"/bash06b_slurmsubmit_many.sh

cd "$WD"
#cat "$WD"/"$OUTD01"/bash06b_slurmsubmit_many.sh
tar -zcf "$OUTD01"_many_HKY.tar.gz "$OUTD01"
#cat /home/hal9000/Iben_Stockholm_PDV/PDVout06/PDVpos12_rc_bdmHKYE/slurm_submis_nesi_rc_bdmHKYE.sh
#cat /home/hal9000/Iben_Stockholm_PDV/PDVout06/PDVpos12_rc_bdmHKYF/slurm_submis_nesi_rc_bdmHKYF.sh

#cat /home/hal9000/Iben_Stockholm_PDV/PDVout06/PDVpos12_rc_bdmHKYFss/slurm_submis_nesi_rc_bdmHKYFss.sh
#cat /home/hal9000/Iben_Stockholm_PDV/PDVout06/PDVpos12_rc_bdmHKYEss/slurm_submis_nesi_rc_bdmHKYEss.sh
#