#!/bin/bash


# Path to the folder containing your input files (and nothing else)
INPUT_FOLDER=in
# Path to the folder the output files (grid arrangements) shall be written into
OUTPUT_FOLDER=out
# Path to the folder containing the grid arrangement executable
EXE_FOLDER=bin
# Name of the grid arrangement executable file (default: grid)
EXE=grid

# Check if paths are correct
if [ ! -d $INPUT_FOLDER ]
then
   echo "Input folder $INPUT_FOLDER specified in this script does not exist."
   exit
fi
if [ ! -d $OUTPUT_FOLDER ]
then
   mkdir $OUTPUT_FOLDER
   if [ ! -d $OUTPUT_FOLDER ]
   then
      echo "Output folder $OUTPUT_FOLDER specified in this script does not exist."
      exit
   fi
fi
if [ ! -f $EXE_FOLDER/$EXE ]
then
   echo "The folder $EXE_FOLDER specified in this script is not the location of $EXE."
   exit
fi



# Execute the grid arranger
cd $INPUT_FOLDER
typeset -i nFilesToProcess counter errorInOutputFile nErrorInputFiles nSuccessfullyProcessed nOptimal nNoGuarantee nWithGuarantee
nFilesToProcess=$(ls -l | grep -v ^l | wc -l)
nFilesToProcess=$nFilesToProcess-1 
counter=1
nErrorInputFiles=0
nOptimal=0
nNoGuarantee=0
guaranteeSum=0
cd ..
echo "For the following files, no OUT-files could be produced (please try again):" > $OUTPUT_FOLDER/error.log


for inputfile in $(ls $INPUT_FOLDER)
do
   # initialize
   errorInOutputFile=0
   echo -e "\n($(date)): Process $inputfile ($counter/$nFilesToProcess)..."

   # run grid arranger
   cd $EXE_FOLDER
   ./$EXE ../$INPUT_FOLDER/$inputfile 2>/dev/null
   cd ..

   # test validity of produced output file
   if [ -f $INPUT_FOLDER/$inputfile.out -o -f $INPUT_FOLDER/$inputfile.component1.out ]
   then
      for outfile in $(ls $INPUT_FOLDER/$inputfile*out)
      do
         if [ "-1" == $(awk 'NR == 6 {print $1}' $outfile) ]
         then
            errorInOutputFile=$errorInOutputFile+1
         fi
      done
   else
      nErrorInputFiles=$nErrorInputFiles+1
      echo "ERROR_NO_SOLUTION_FILE_FOUND"
      echo "$inputfile" >> $OUTPUT_FOLDER/error.log
   fi

   # error handling for invalid output files
   if (( $errorInOutputFile >= 1 ))
   then
      echo "ERROR_IN_SOLUTION_FILE"
      nErrorInputFiles=$nErrorInputFiles+1
      rm $INPUT_FOLDER/$inputfile*out
      echo "$inputfile" >> $OUTPUT_FOLDER/error.log
   fi

   # copy valid output files to output folder
   if [ "$(ls -A $INPUT_FOLDER/*.out 2>/dev/null)" ] # if there is any .out file...
   then
      outfile=$INPUT_FOLDER/$inputfile.out
      if [ -f $INPUT_FOLDER/$inputfile.component1.out ]
      then 
         outfile=$INPUT_FOLDER/$inputfile.component1.out
      fi
      if [ "optimum" == $(awk 'NR == 4 {print $1}' $outfile) ]
      then
          nOptimal=$nOptimal+1
      fi
      if [ "none" == $(awk 'NR == 4 {print $1}' $outfile) ]
      then
         nNoGuarantee=$nNoGuarantee+1
      fi
      if [ "optimum" != $(awk 'NR == 4 {print $1}' $outfile) -a "none" != $(awk 'NR == 4 {print $1}' $outfile) ]
      then
         guaranteeSum=`echo "$guaranteeSum + $(awk 'NR == 4 {print $1}' $outfile)" | bc`
      fi
      mv $INPUT_FOLDER/*.out $OUTPUT_FOLDER
   fi
   counter=$counter+1
done


# create statistics file
cd $OUTPUT_FOLDER
echo -e "Statistics on run on files in $INPUT_FOLDER, finished on $(date) \n" > statistics.log 
echo "Number of files in input folder: $nFilesToProcess" >> statistics.log
nSuccessfullyProcessed=$nFilesToProcess-$nErrorInputFiles
echo "Number of files successfully processed: $nSuccessfullyProcessed" >> statistics.log
echo "Number of files that were optimally solved: $nOptimal" >> statistics.log
echo "Number of files with no solution guarantee: $nNoGuarantee" >> statistics.log
nWithGuarantee=$nSuccessfullyProcessed-$nNoGuarantee
if (( $nWithGuarantee > 0 ))
then
   avgGuarantee=`echo "$guaranteeSum / $nWithGuarantee" | bc`
   echo -e "Average solution guarantee: $avgGuarantee % ( 0 % = opt ) \n" >> statistics.log
fi
cat error.log >> statistics.log
rm error.log
cd ..

# quit
echo -e "\n\n------------------------------------------------------------"
echo " FINAL STATISTICS "
echo -e "------------------------------------------------------------"
cat $OUTPUT_FOLDER/statistics.log
