#! /bin/bash

source ../sourceme.sh

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 tutorials tutorials/others"

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

# THIS IS FOR DOXYGEN <= 1.8.10
# I leave it here for the moment
for file in html/*.html
do
awk -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 print
}' $file |
awk -f plumed-syntax.awk > $file.tmp
mv $file.tmp $file
done
# 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

