## Scale the cafemol input file by x,y,z values and submit runs based on new scaling
## Usage: bash setup_scaleRun.sh "a b [p,q,dx] c templt_folder"
## a,b scale factor for x,y
## p,q lower and upperbound for scale factor for z, dx is the increment interval
## c is the cutoff value for z, any value of z below cutoff is not scaled
## Example: bash setup_scaleRun.sh "1 1 [1,1.5,0.1] 10 k1"

###############################################################################
# Help                                                                         #
################################################################################
Help()
{
   # Display Help
   echo "Generate copies of a template folder to run cafemol runs at various XY scale values"
   echo "and submit all the runs"
   echo
   echo "Usage: "
   echo "      bash setup_scaleRun_XY.sh '[p,q,dx] r c templt_folder'"
   echo 
   echo "      p,q,dx                p,q are lower and upperbound for scale factor for X and Y, dx is the increment interval"
   echo "      r                     scale factor for z"
   echo "      c                     cutoff value for z, any value of z below cutoff is set to 0"
   echo "      templt_folder         template folder that has a complete set up for the cafemol run"
   echo 
   echo "      This script also generates a file called Zscale.txt that contain all scale factors for X and Y"
   echo "      X and Y pixel are assumed to be of same size and same scale factor is applied to X and Y pixel values"
   echo
}
# Get the options
while getopts ":h" option; do
   case $option in
      h) # display Help
         Help
         exit;;
   esac
done
# While no arguments
if [[ $# -eq 0 ]] ; then
    Help
    exit 0
fi


module load R/4.0.0

cmd_args=$1

## Parse the arguments
arrIN=(${cmd_args// / })
#echo $((${arrIN[0]}*${arrIN[1]}))
scaleRange_z=${arrIN[0]}
scale_zz=${arrIN[1]}
cutoff_z=${arrIN[2]}
tmpl_folder=${arrIN[3]}

#echo $scale_x $scale_y $scaleRange_z $cutoff_z $tmplt_folder
scaleRange_z="${scaleRange_z#*\[}"
scaleRange_z="${scaleRange_z%\]*}"
scaleRange_z=(${scaleRange_z//,/ })

b=$(echo "${scaleRange_z[1]}-${scaleRange_z[0]}"|bc)
#echo $b
c=$(echo "$b/${scaleRange_z[2]}"|bc)

rm -f "Zscale.txt"
scale_z=${scaleRange_z[0]}
for i in $(eval echo "{0..$c}")
do
  echo $scale_z >> "Zscale.txt"
  folder_name="Zscale_"$scale_z
  if [[ $tmpl_folder != $folder_name ]]; then
    rm -rf $folder_name
    cp -r $tmpl_folder $folder_name
  fi
  cd $folder_name
  rm -rf "output"
  mkdir "output"
  echo "currently working in directory: " $(pwd)

  ## edit the cafemol input file to scale the z values
  Rscript $analysisPath"/analysis/scaleAFM_XY_CafeInput.R" "input.inp" [$scale_z,$scale_z,$scale_zz] $cutoff_z
  mv -f input_scaled.inp input.inp  ## rename the new input file made by Rscript to original name
  ## submit the batch job based on the info in qsub1.sh file
  ## Ensuring that OMP_NUM_THREADS is not set will make your jobs much faster
  unset OMP_NUM_THREADS
  ## cut -c 2- removes first character # of the line
  grep "#sbatch" qsub1.sh | cut -c 2- > submit.sh
  bash submit.sh

  scale_z=$(echo "$scale_z + ${scaleRange_z[2]}"|bc) 
  cd ../
 
done
