#!/bin/bash

#  syntax: galdoxy package

#  create the C documentation for GALAHAD.
#  currently this will produce html, pdf and man

#  Nick Gould, August 21st 2021

#  does the package exist?

export package=$1
if [ -d "$GALAHAD/src/$package" ]; then 
  export PACKAGE=${package^^}
elif [ $package == "galahad" ]; then
  export PACKAGE=
else
  echo "Error: package $package does not exist."
  exit 1
fi

#  make sure that required subdirectories exist

mkdir -p $package
mkdir -p $GALAHAD/html/C_doxy/$package

#  build the pdf and html documentation

echo "building pdf and html"

if [ $package == "galahad" ]; then
  cp $GALAHAD/include/$package.h $package/.
  #doxygen Doxyfile
  ( cd ./$package ; doxygen ../Doxyfile )
  #mogrify -trim $GALAHAD/html/C/$package/*.png
  ( cd ./$package/latex ; make ; make )
  cp ./$package/latex/refman.pdf $GALAHAD/doc/"$package"_c.pdf
  #cp $GALAHAD/html/C_doxy/$package/index.html $GALAHAD/html/C_doxy/$package/$package.html
  rm $package/$package.h
else
  cp $GALAHAD/include/galahad_$package.h $package/.
  #doxygen Doxyfile
  ( cd ./$package ; doxygen ../Doxyfile )
  #mogrify -trim $GALAHAD/html/C/$package/*.png
  ( cd ./$package/latex ; make ; make )
  cp ./$package/latex/refman.pdf $GALAHAD/doc/"$package"_c.pdf
  #cp $GALAHAD/html/C_doxy/$package/index.html $GALAHAD/html/C_doxy/$package/$package.html
  rm $package/galahad_$package.h
fi

#  build the man pages; the C header page is adapted so that doxygen works!

echo "building man"
if [ $package == "galahad" ]; then
  sed -f man.sed $GALAHAD/include/$package.h > $package/$package.h
  ( cd ./$package ; doxygen ../Doxyfile_man )
  mv $GALAHAD/man/man3/$package.h.3 $GALAHAD/man/man3/"$package"_c.3
  rm $package/$package.h
else
  sed -f man.sed $GALAHAD/include/galahad_$package.h > $package/galahad_$package.h
  ( cd ./$package ; doxygen ../Doxyfile_man )
  mv $GALAHAD/man/man3/galahad_$package.h.3 $GALAHAD/man/man3/"$package"_c.3
  rm $package/galahad_$package.h
fi
