find_program (PERL perl)
find_package (LATEX)

set (CHAPTERS FDL.chapt GPL.chapt cl-options.chapt config.chapt addons.chapt YacasDebugger.chapt ABIN-grammar.chapt SimpleTools.chapt CVS-Howto.chapt YacasDocs.chapt wester-1994.chapt new.chapt paper.chapt wordproblems.chapt BuildSystem.chapt arith.chapt calc.chapt complex.chapt solvers.chapt simplify.chapt ode.chapt linalg.chapt univar.chapt lists.chapt functional.chapt controlflow.chapt preds.chapt logic.chapt const.chapt vars.chapt io.chapt numtheory.chapt strings.chapt transforms.chapt probability-and-statistics.chapt glossary.chapt glossary.chapt algo-basic.chapt algo-numapprox.chapt algorithms-elemfunc.chapt algorithms-specfunc.chapt algorithms-numtheory.chapt algorithms-integration.chapt algorithms-multivar.chapt SturmSequences.chapt algo-refs.chapt algo-contfrac.chapt algorithms-transforms.chapt numerics.chapt)
foreach (_chapter ${CHAPTERS})
  add_custom_command (
    OUTPUT ${_chapter}
    COMMAND ${PERL} ${PROJECT_SOURCE_DIR}/manmake/txt2yacasdoc.pl
    ARGS < ${PROJECT_SOURCE_DIR}/manmake/${_chapter}.txt > ${_chapter}
    MAIN_DEPENDENCY manmake/${_chapter}.txt
    DEPENDS manmake/txt2yacasdoc.pl)
endforeach()

set (BOOKS intro.book coding.book ref.book refprog.book essays.book Algo.book)
foreach (_book ${BOOKS})
  add_custom_command (
    OUTPUT ${_book}
    COMMAND ${PERL} ${PROJECT_SOURCE_DIR}/manmake/txt2yacasdoc.pl
    ARGS < ${PROJECT_SOURCE_DIR}/manmake/${_book}.txt > ${_book}
    MAIN_DEPENDENCY manmake/${_book}.txt
    DEPENDS manmake/txt2yacasdoc.pl)
endforeach()

add_custom_target (docs DEPENDS ${BOOKS} ${CHAPTERS})

foreach (_book ${BOOKS})
  add_custom_command (
    OUTPUT ${_book}.tex
    COMMAND ${YACAS} --rootdir ${PROJECT_SOURCE_DIR}/scripts:${PROJECT_BINARY_DIR}/scripts -i "[ Use(\"${PROJECT_SOURCE_DIR}/manmake/book2TeX.ys\"); Load(\"${_book}\"); TeXFinishUp(); ];" > ${_book}.tex
    MAIN_DEPENDENCY ${_book}
    DEPENDS docs yacas
    VERBATIM)
endforeach()

set (TEX_BOOKS)
foreach (_book ${BOOKS})
  list (APPEND TEX_BOOKS ${_book}.tex)
endforeach()

add_custom_target (tex_docs DEPENDS ${TEX_BOOKS})

foreach (_book ${BOOKS})
  add_custom_command ( 
    OUTPUT    ${PROJECT_BINARY_DIR}/${_book}.pdf
    COMMAND   ${PDFLATEX_COMPILER}
    ARGS      ${PROJECT_BINARY_DIR}/${_book}.tex
    DEPENDS   ${PROJECT_BINARY_DIR}/${_book}.tex
    COMMENT   "LaTeX"
)
endforeach()

set (PDF_BOOKS)
foreach (_book ${BOOKS})
  list (APPEND PDF_BOOKS ${_book}.pdf)
endforeach()

add_custom_target (pdf_docs ALL DEPENDS ${PDF_BOOKS})

set (PDF_BOOK_PATHS)
foreach (_book ${PDF_BOOKS})
  list (APPEND PDF_BOOK_PATHS ${PROJECT_BINARY_DIR}/${_book})
endforeach()

install (FILES ${PDF_BOOK_PATHS} DESTINATION share/yacas/documentation COMPONENT doc)

foreach (_book ${BOOKS})
  string (REPLACE .book "" _book ${_book})
  add_custom_command (
    OUTPUT ${_book}manual.html
    COMMAND ${YACAS} --rootdir ${PROJECT_SOURCE_DIR}/scripts:${PROJECT_BINARY_DIR}/scripts:${PROJECT_BINARY_DIR}/manmake -i "[ DefaultDirectory(\"${PROJECT_SOURCE_DIR}/manmake\"); Use(\"${PROJECT_SOURCE_DIR}/manmake/styleplain\"); Use(\"${PROJECT_SOURCE_DIR}/manmake/manualmaker\"); GenerateBook(\"${_book}\"); ];"
    MAIN_DEPENDENCY ${_book}.book
    DEPENDS docs yacas
    VERBATIM)
endforeach()

add_custom_command (
  OUTPUT books.html
  COMMAND ${YACAS} --rootdir ${PROJECT_SOURCE_DIR}/scripts:${PROJECT_BINARY_DIR}/scripts:${PROJECT_BINARY_DIR}/manmake -i "[ DefaultDirectory(\"${PROJECT_SOURCE_DIR}/manmake\"); Use(\"${PROJECT_SOURCE_DIR}/manmake/styleplain\"); Use(\"${PROJECT_SOURCE_DIR}/manmake/indexmaker\"); GenerateIndex(); ];"
  DEPENDS docs yacas
  VERBATIM)

set (HTML_BOOKS)
foreach (_book ${BOOKS})
  string (REPLACE .book "" _book ${_book})
  list (APPEND HTML_BOOKS ${CMAKE_BINARY_DIR}/${_book}manual.html)
endforeach()

set (HTML_CHAPTERS)
foreach (_chapter RANGE 1 6)
  list (APPEND HTML_CHAPTERS "${CMAKE_BINARY_DIR}/introchapter${_chapter}.html")
endforeach ()

foreach (_chapter RANGE 1 9)
  list (APPEND HTML_CHAPTERS "${CMAKE_BINARY_DIR}/codingchapter${_chapter}.html")
endforeach ()

foreach (_chapter RANGE 1 8)
  list (APPEND HTML_CHAPTERS "${CMAKE_BINARY_DIR}/Algochapter${_chapter}.html")
endforeach ()

foreach (_chapter RANGE 1 8)
  list (APPEND HTML_CHAPTERS "${CMAKE_BINARY_DIR}/essayschapter${_chapter}.html")
endforeach ()

foreach (_chapter RANGE 1 32)
  list (APPEND HTML_CHAPTERS "${CMAKE_BINARY_DIR}/refchapter${_chapter}.html")
endforeach ()

foreach (_chapter RANGE 1 10)
  list (APPEND HTML_CHAPTERS "${CMAKE_BINARY_DIR}/refprogchapter${_chapter}.html")
endforeach ()

list (APPEND HTML_BOOKS ${CMAKE_BINARY_DIR}/books.html)

add_custom_target (html_docs ALL DEPENDS ${HTML_BOOKS})

install (FILES ${HTML_BOOKS} ${HTML_CHAPTERS} DESTINATION share/yacas/documentation COMPONENT doc)

