#------------------------------------------------------------------------------------------ **
# CONFIGURATION    (Edit this section)                                                  --- **
#------------------------------------------------------------------------------------------ **

T1_Dir='/data/proj/app/img/Owl_Oriented'      # DIR containing T1 images
T1_file_extension='.nii.gz'                   # File extension of the T1 image

# Regex pattern to recognize a unique ScanID in the T1 filename
pattern="([0-9]{6}-[0-9]{3}_?[0-9]?p?(_NAPP)?)_OwlOrient"    

HaltOnUnrecognizedScanID="true"     	      # Halt the batch if a ScanID isn't recognized?

# DIR contain Auto_EACSF executable 
BinDir='/data/proj/app/pipeline/eacsf/Auto_EACSF_v1.4.1/bin/'

# JSON file containing AutoEaCSF params
ParamsFile='/data/proj/app/pipeline/eacsf/config_17Aug2019.json' 

# Main Output dir, results store in subfolder named for ScanId
OutputDir='/data/proj/app/pipeline/eacsf/Outputs'             

#------------------------------------------------------------------------------------------*
#  End Configuration Section
#------------------------------------------------------------------------------------------ |





# Per AutoEaCSF's README, we add the lib directory to LD_LIBRARY_PATH
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$BinDir/../lib"

# Loop over every filename in the T1_Dir that ends with the T1_file_extension:
for T1_filename in $T1_Dir/*$T1_file_extension ; do 

    #  Attempt to find the ScanID within T1_filename 
    if [[ $T1_filename =~ $pattern ]] ; then
         # ScanID _was_ found!    
         scanid="${BASH_REMATCH[1]}"
	 
         # Get the Start Time	
	 start_time=`date +%s`
	 start_String=`date -d @$start_time +%Y%m%d_%H%M%S%Z `

         echo -e "Beginning $scanid at $start_String"


         # Set the OutputDir based on the ScanID, create the dir
         SubjectsOutputDir="$OutputDir/$scanid"
         mkdir $SubjectsOutputDir

	 # Unzip the .gz before we begin.  
         # Otherwise, ABC_CLI gets confused and creates a "_labels_EMS.nrrd" file 
         # Instead of a "_stripped_labels_EMS.nrrd" file
         T1_Unzipped=$SubjectsOutputDir/${scanid}_OwlOrient.nii
         echo "gunzip -c $T1_filename > $T1_Unzipped"
         gunzip -c $T1_filename > $T1_Unzipped


	 # Change Dir to BinDir before running Auto_EACSF
         cd $BinDir
         ./Auto_EACSF --t1 $T1_Unzipped --param $ParamsFile --output_dir $SubjectsOutputDir 

         # Change Dir back to where we were before.
         cd ~-

         end_time=`date +%s`
	 end_String=`date -d @$end_time +%Y%m%d_%H%M%S%Z `

         echo -e "Ended $scanid at $end_String"
         runtime=$((end_time-start_time))
	 echo -e "That subject took $runtime seconds, from $start_time to $end_time. \n"



  else  #  ScanID was not found in T1_Filename.  Complain

     echo -e "***************************"
     echo -e "Error!  Unable to find the ScanId in the filename: \n   $T1_filename"
     echo -e "while using regex pattern:\n     $pattern"
     if [ $HaltOnUnrecognizedScanID == "true" ] ; then 
	echo -e "Because the ScanID was unrecognized, script is halting...\n\n"
     	exit
     fi  # EndIf HaltOnUnrecognizeScan == true
  fi   # EndElse Regex Fails

done # EndFor filename in T1_Dir

echo "Congratulations, the script has concluded.  'My work here is done.'"
