#! /bin/bash

source ../sourceme.sh

if plumed --no-mpi config mpiexec ; then
   mpirun=$(plumed --no-mpi config mpiexec)
else
   mpirun=mpirun
fi

LISTMORE=`ls *PP.md | grep -wvFf basic.list`

LIST="
 IntroductionPP.md
 ChangeLogPP.md
 ../CHANGES
 InstallationPP.md
 GettingStartedPP.md
 CollectiveVariablesPP.md
 GroupPP.md
 FunctionsPP.md
 AnalysisPP.md
 BiasPP.md
 AdditionalModulesPP.md
 $LISTMORE
 CommandLineToolsPP.md
 MiscelaneousPP.md
 RegexPP.md
 FilesPP.md
 ModulesPP.md
 TutorialsPP.md
 PerformancesPP.md
 GlossaryPP.md
 automatic"

echo $LIST

{
  cat Doxyfile
  echo "PROJECT_NUMBER = \"$(../src/lib/plumed --no-mpi info --long-version)\""
  echo "INPUT+=" $LIST
  # add this to manually control layout:
  # echo "LAYOUT_FILE=PlumedLayout.xml"
} | doxygen -

version=v$(../src/lib/plumed --no-mpi info --version)

# in the online manual better to put the branch name
# which is available through the TRAVIS_BRANCH env var
if test "$TRAVIS" == true ; then
  version="${TRAVIS_BRANCH}"
fi

if test "$GITHUB_ACTIONS" == true ; then
  if [[ "$GITHUB_REF" == "refs/heads/"* ]] ; then
    version="${GITHUB_REF#refs/heads/}"
  fi
fi

# THIS IS FOR DOXYGEN <= 1.8.10
# I leave it here for the moment
mkdir build_examples
cd build_examples
mkdir build
cd build
for file in ../../html/*.html
do
echo final processing of html for file: $file
gawk -v version="$version" '{
  if(match($0,"<span>Main&#160;Page</span>")){
    sub("Main","Home",$0);
    sub("Page","("version")",$0);
    print
  } else if(match($0,"<span>Related&#160;Pages</span>")){
    print "      <li><a href=\"_syntax.html\"><span>Getting&#160;Started</span></a></li>"
    print "      <li><a href=\"tutorials.html\"><span>Tutorials</span></a></li>"
    print "      <li><a href=\"glossary.html\"><span>Index&#160;of&#160;Actions</span></a></li>"
  } else if(match($0,"<link href=\"doxygen.css\"")){
    print 
    print "<script>"
    print "   var redpath = \"\";"
    print "   "
    print "   function showPath(eg,name) {"
    print "     var i; var y = document.getElementsByName(redpath);"
    print "     for (i=0; i < y.length; i++ ) { y[i].style.color=\"black\"; }"
    print "     var x = document.getElementsByName(name); redpath=name;"
    print "     for (i = 0; i < x.length; i++) { x[i].style.color=\"red\"; }"
    print "     var valid=\"value_details_\".concat(eg);" 
    print "     var valueField = document.getElementById(valid);"
    print "     var dataField = document.getElementById(name);"
    print "     valueField.innerHTML = dataField.innerHTML;"
    print "   }"
    print "   function swapInput(name) {"
    print "     var btn = document.getElementById(name + \"_button\");"
    print "     var mydiv = document.getElementById(\"input_\" + name);" 
    print "     if( btn.textContent==\"expand shortcuts\" ) {" 
    print "         btn.textContent = \"contract shortcuts\";"
    print "         var dataField = document.getElementById(name + \"long\");" 
    print "         mydiv.innerHTML = dataField.innerHTML;"
    print "     } else if( btn.textContent==\"contract shortcuts\" ) {"
    print "         btn.textContent = \"expand shortcuts\";" 
    print "         var dataField = document.getElementById(name + \"short\");"
    print "         mydiv.innerHTML = dataField.innerHTML;"
    print "     }"
    print "   }"
    print "</script>"
  } else print
}' $file |
gawk -v mpirun="$mpirun" -f ../../plumed-syntax.awk > $file.tmp
if [ $? -ne 0 ]; then
   echo "ERROR IN RUNNING PLUMED SYNTAX COMMAND ON " $file
   exit
fi
mv $file.tmp $file
rm -fr *
done
cd ../../
rm -rf build_examples
# end of DOXYGEN <=1.8.10

# Same stuff for DOXYGEN >=1.8.12
cat > html/menudata.js << EOF
var menudata={children:[
{text:'Home ($version)',url:'index.html'},
{text:'Getting Started',url:'_syntax.html'},
{text:'Tutorials',url:'tutorials.html'},
{text:'Index of Actions',url:'glossary.html'}]}
EOF
# end of DOXYGEN >=1.8.12

cat html/doxygen.css plumed.css > html/doxygen.css.tmp
mv html/doxygen.css.tmp html/doxygen.css

if [ "$make_pdfdoc" != yes ] ; then
  exit
fi

cd latex

# this is a workaround for a problem on the linux workstation
# where I compile the manual - GB
sed -ie 's/pdfencoding=unicode/unicode/g' refman.tex
make -i
cp refman.pdf ../manual.pdf

