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

#define present working dir
WD=$(pwd)

#define the two input directories 
INDIR01="PDVout06"
INDIR02="PDVout07"

OUTDI01="PDVout10"
rm -rf "$OUTDI01"
mkdir "$OUTDI01"

#define the number of hours required for treeanotator
slurm_hours="1"



# Declare an array of string with type
declare -a StringArray=(
"$INDIR01"
"$INDIR02"
	 )
 
# Iterate the string array using for loop
for val in ${StringArray[@]}; do
echo "$val"



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

# https://stackoverflow.com/questions/2107945/how-to-loop-over-directories-in-linux
#loop over dirs in directory
for dir in "$WD"/"$val"/*/     # list directories in the form "/tmp/dirname/"
do
    dir=${dir%*/}      # remove the trailing "/"
    DIRNM=$(echo ${dir##*/})    # print everything after the final "/"
	#echo "$DIRNM"
	#change directory
	cd "$dir"
	for file in "$DIRNM".comb.tre
	do 
	#write a slurm submission file
	cat "$file" > "$WD"/"$OUTDI01"/"$file"
	printf "%s\n" "
begin figtree;
	set appearance.backgroundColorAttribute=\"Default\";
	set appearance.backgroundColour=#ffffff;
	set appearance.branchColorAttribute=\"posterior\";
	set appearance.branchColorGradient=true;
	set appearance.branchLineWidth=2.0;
	set appearance.branchMinLineWidth=0.0;
	set appearance.branchWidthAttribute=\"Fixed\";
	set appearance.foregroundColour=#000000;
	set appearance.hilightingGradient=false;
	set appearance.selectionColour=#2d3680;
	set branchLabels.colorAttribute=\"User selection\";
	set branchLabels.displayAttribute=\"posterior\";
	set branchLabels.fontName=\"Arial\";
	set branchLabels.fontSize=12;
	set branchLabels.fontStyle=0;
	set branchLabels.isShown=true;
	set branchLabels.significantDigits=2;
	set colour.scheme.posterior=\"posterior:InterpolatingContinuous{{false,false,0.0,0.0},#-2536895,#-16777216,#-12492071}\";
	set layout.expansion=446;
	set layout.layoutType=\"RECTILINEAR\";
	set layout.zoom=0;
	set legend.attribute=null;
	set legend.fontSize=10.0;
	set legend.isShown=false;
	set legend.significantDigits=4;
	set nodeBars.barWidth=4.0;
	set nodeBars.displayAttribute=\"height_95%_HPD\";
	set nodeBars.isShown=true;
	set nodeLabels.colorAttribute=\"User selection\";
	set nodeLabels.displayAttribute=\"Node ages\";
	set nodeLabels.fontName=\"Arial\";
	set nodeLabels.fontSize=12;
	set nodeLabels.fontStyle=0;
	set nodeLabels.isShown=false;
	set nodeLabels.significantDigits=4;
	set nodeShapeExternal.colourAttribute=null;
	set nodeShapeExternal.isShown=false;
	set nodeShapeExternal.minSize=10.0;
	set nodeShapeExternal.scaleType=Width;
	set nodeShapeExternal.shapeType=Circle;
	set nodeShapeExternal.size=4.0;
	set nodeShapeExternal.sizeAttribute=null;
	set nodeShapeInternal.colourAttribute=null;
	set nodeShapeInternal.isShown=false;
	set nodeShapeInternal.minSize=10.0;
	set nodeShapeInternal.scaleType=Width;
	set nodeShapeInternal.shapeType=Circle;
	set nodeShapeInternal.size=4.0;
	set nodeShapeInternal.sizeAttribute=null;
	set polarLayout.alignTipLabels=false;
	set polarLayout.angularRange=0;
	set polarLayout.rootAngle=0;
	set polarLayout.rootLength=100;
	set polarLayout.showRoot=true;
	set radialLayout.spread=0.0;
	set rectilinearLayout.alignTipLabels=false;
	set rectilinearLayout.curvature=0;
	set rectilinearLayout.rootLength=100;
	set scale.offsetAge=0.0;
	set scale.rootAge=1.0;
	set scale.scaleFactor=1.0;
	set scale.scaleRoot=false;
	set scaleAxis.automaticScale=true;
	set scaleAxis.fontSize=14.0;
	set scaleAxis.isShown=true;
	set scaleAxis.lineWidth=1.0;
	set scaleAxis.majorTicks=50.0;
	set scaleAxis.minorTicks=25.0;
	set scaleAxis.origin=0.0;
	set scaleAxis.reverseAxis=true;
	set scaleAxis.showGrid=true;
	set scaleBar.automaticScale=true;
	set scaleBar.fontSize=12.0;
	set scaleBar.isShown=true;
	set scaleBar.lineWidth=1.0;
	set scaleBar.scaleRange=0.0;
	set tipLabels.colorAttribute=\"User selection\";
	set tipLabels.displayAttribute=\"Names\";
	set tipLabels.fontName=\"Arial\";
	set tipLabels.fontSize=12;
	set tipLabels.fontStyle=0;
	set tipLabels.isShown=true;
	set tipLabels.significantDigits=4;
	set trees.order=true;
	set trees.orderType=\"decreasing\";
	set trees.rooting=false;
	set trees.rootingType=\"User Selection\";
	set trees.transform=false;
	set trees.transformType=\"cladogram\";
end;
" >> "$WD"/"$OUTDI01"/"$file"
done
	#change directory
	cd "$WD"/"$val"
done

done

#change the directory
cd "$WD"/"$OUTDI01"
#remove unwanted files
rm *E.comb.tre
rm *ss.comb.tre
rm *ss.comb.tre
rm *bdmnoe*.comb.tre
#change the directory
cd "$WD"
#compress the directory with all the trees
tar -zcf  "$OUTDI01".tar.gz "$OUTDI01"


# #
# #
