#!/bin/bash

infile=$1
haseg=`grep endplumedfile $infile | wc -l | awk '{print $1}'`
if [ $haseg -eq 0 ] ; then
   echo No examples in $infile
   exit 0
fi
echo Testing examples in $infile
splits=`echo $infile | sed -e 's/\// /g'`
nf=`echo $splits | awk '{print NF}'`
fname=`echo $splits | awk -v n=$nf '{print $n}'`
cat $infile |
awk 'BEGIN{inp=0;}{
       if( $1=="\\endplumedfile" ){ print ">>END OF EXAMPLE>>"; inp=0; }
       else if( $1=="\\plumedfile" ){ inp=1; }
       else if( inp==1 ){ print $0; }  
     }' > example-check/$fname.dat
# Count the number of examples found in the input
neg=`grep ">>END OF EXAMPLE>>" example-check/$fname.dat | wc -l | awk '{print $1}'`
if [ $neg -gt 0 ] ; then
   # Make a directory to hold all the inputs
   mkdir example-check/$fname
   # Now split the examples found into the set of example input files in the documentation
   sline=0
   for ((i=1;i<=$neg;++i)) ; do
        ffname=$(printf "%02d" $i)
        eline=`grep -n ">>END OF EXAMPLE>>" example-check/$fname.dat | head -n $i | tail -n 1 | awk '{print $1}' | sed -e 's/:>>END//g'`
        head -n $(($eline-1)) example-check/$fname.dat | tail -n $(($eline-1-$sline)) > example-check/$fname/$ffname.dat
        wrongc=`grep "this is wrong" example-check/$fname/$ffname.dat | wc -l | awk '{print $1}'`
        if [ $wrongc -gt 0 ] ; then
             rm example-check/$fname/$ffname.dat
        else
           # This deals with include files
           hasname=`grep "#SETTINGS" example-check/$fname/$ffname.dat | grep "FILENAME=" | wc -l | awk '{print $1}'`
           if [ $hasname -gt 0 ] ; then
                filename=`grep "#SETTINGS" example-check/$fname/$ffname.dat  | awk '{for(i=1;i<=NF;++i){if(match($i,"FILENAME=")){print $i}}}' | sed -e 's/FILENAME=//'`
                cp example-check/$fname/$ffname.dat example-check/$fname/$filename
           fi
        fi
        sline=$eline
   done
   # Check for auxiliary files
   cat $infile |
   awk 'BEGIN{inp=0;}{
         if( $1=="\\endauxfile" ){ print ">>END OF EXAMPLE>>"; inp=0; }
         else if( match($0,"auxfile") ){ inp=1; }
         else if( inp==1 ){ print $0; }  
       }' > example-check/$fname.aux
   naux=`grep ">>END OF EXAMPLE>>" example-check/$fname.aux | wc -l | awk '{print $1}'`
   # And split the set of include files
   sline=0
   for ((i=1;i<=$naux;++i)) ; do
        ifname=`grep "auxfile" $infile | grep -v "endauxfile" | head -n $i | tail -n 1 | sed -e 's/\\\auxfile{//' | sed -e 's/}//'`
        eline=`grep -n ">>END OF EXAMPLE>>" example-check/$fname.aux | head -n $i | tail -n 1 | awk '{print $1}' | sed -e 's/:>>END//g'`
        head -n $(($eline-1)) example-check/$fname.aux | tail -n $(($eline-1-$sline)) > example-check/$fname/$ifname
        sline=$eline
   done
   rm example-check/$fname.aux
fi
rm example-check/$fname.dat
echo Extracted examples from $infile

cd example-check/$fname
for file in *.dat ; do
   # echo Checking example named $dir/$file
   # This checks if we need a structure file for a MOLINFO command and gets one if it is required
   molfile=`grep "MOLINFO" $file | wc -l | awk '{print $1}'`
   nfill=`grep "__FILL__" $file | wc -l | awk '{print $1}'`
   if [ $molfile -gt 0 ] && [ $nfill -eq 0 ] ; then
        hasmol=`grep "#SETTINGS" $file | grep "MOLFILE=" | wc -l | awk '{print $1}'`
        if [ $hasmol -eq 0 ] ; then
             echo For example $file in $dir you need to include a MOLFILE= keyword in the SETTINGS line
             exit 1
        fi
        molname=`grep "#SETTINGS" $file | awk '{for(i=1;i<=NF;++i){if(match($i,"MOLFILE=")){print $i}}}' | sed -e 's/MOLFILE=//'`
        inpf=`grep "STRUCTURE=" $file | awk '{for(i=1;i<=NF;++i){ if($i ~ /STRUCTURE=/){ print $i; }}}' | sed -e 's/STRUCTURE=//'`
        cp ../../../$molname $inpf
   fi

   # This checks if we need an auxiliary file or folder and copy it 
   auxfile=`grep "#SETTINGS" $file | awk '{for(i=1;i<=NF;++i){if(match($i,"AUXFILE=")){print $i}}}' | sed -e 's/AUXFILE=//'`
   if [ ! -z $auxfile ] ; then
      cp ../../../$auxfile . 
   fi
   auxfolder=`grep "#SETTINGS" $file | awk '{for(i=1;i<=NF;++i){if(match($i,"AUXFOLDER=")){print $i}}}' | sed -e 's/AUXFOLDER=//'`
   if [ ! -z $auxfolder ] ; then
      cp -r ../../../$auxfolder . 
   fi

   # Now running test with PLUMED
   egn=`echo $file | cut -c1-2`
   echo Testing example $egn in $file for $infile
   nload=`grep "LOAD" $file | wc -l | awk '{print $1}'`
   if [ $nload -eq 0 ] && [ $nfill -eq 0 ] ; then
       hasrep=0
       settings=`grep "#SETTINGS" $file | wc -l | awk '{print $1}'`
       if [ $settings -gt 0 ] ; then
            hasrep=`grep "#SETTINGS" $file | grep "NREPLICAS=" | wc -l | awk '{print $1}'`
       fi
       if [ $hasrep -gt 0 ] ; then
           if ../../../src/lib/plumed --no-mpi config mpiexec ; then
              mpirun=$(../../../src/lib/plumed --no-mpi config mpiexec)
           else
              mpirun=mpirun
           fi
           nrep=`grep "#SETTINGS" $file | awk '{for(i=1;i<=NF;++i){if(match($i,"NREPLICAS=")){print $i}}}' | sed -e 's/NREPLICAS=//'`
           $mpirun -np $nrep ../../../src/lib/plumed driver --natoms 100000 --parse-only --kt 2.49 --plumed $file --multi $nrep > check.log
       else
           ../../../src/lib/plumed driver --natoms 100000 --parse-only --kt 2.49 --plumed $file > check.log
       fi
       if [ $? -eq 0 ] ; then
           nerr=`grep "PLUMED error" check.log | wc -l | awk '{print $1}'`
       else
           nerr=1
       fi
       if [ $nerr -gt 0 ] ; then
           ferr=`grep $infile ../../example_errors | wc -l | awk '{print $1}'`
           if [ $ferr -eq 0 ] ; then
              echo -n "Found broken examples in $infile  \\n" >> ../../example_errors
           fi
           awk -v eg=$egn -v data=$fname '{if(match($0,"plumedfile") && !match($0,"endplumedfile") && n+1==eg){ n++; print $0 " broken DATADIR=example-check/" data; }else if(match($0,"plumedfile") && !match($0,"endplumedfile")){ n++; print $0;}else{print $0;}}' ../../$infile > ../../$infile.e
       else 
           awk -v eg=$egn -v data=$fname '{if(match($0,"plumedfile") && !match($0,"endplumedfile") && n+1==eg){ n++; print $0 " working DATADIR=example-check/" data; }else if(match($0,"plumedfile") && !match($0,"endplumedfile")){ n++; print $0;}else{print $0;}}' ../../$infile > ../../$infile.e
       fi
   elif [ $nload -gt 0 ] ; then
       awk -v eg=$egn -v data=$fname '{if(match($0,"plumedfile") && !match($0,"endplumedfile") && n+1==eg){ n++; print $0 " loads DATADIR=example-check/" data; }else if(match($0,"plumedfile") && !match($0,"endplumedfile")){ n++; print $0;}else{print $0;}}' ../../$infile > ../../$infile.e
   elif [ $nfill -gt 0 ] ; then
       awk -v eg=$egn -v data=$fname '{if(match($0,"plumedfile") && !match($0,"endplumedfile") && n+1==eg){ n++; print $0 " incomplete DATADIR=example-check/" data; }else if(match($0,"plumedfile") && !match($0,"endplumedfile")){ n++; print $0;}else{print $0;}}' ../../$infile > ../../$infile.e
   fi
   mv ../../$infile.e ../../$infile
done

cd ../../

