# Top-level CMakeLists.txt for the CMake-based build system
# of the docbook-xml software.

# Copyright (C) 2013-2014 Alan W. Irwin

# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library General Public License as published
# by the Free Software Foundation; version 2 of the License.
#
# This file is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public License
# along with this file; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA

project(docbook-xml NONE)

message(STATUS "CMake version = ${CMAKE_VERSION}")
message(STATUS "CMAKE_SYSTEM = ${CMAKE_SYSTEM}")
message(STATUS "CMAKE_GENERATOR = ${CMAKE_GENERATOR}")
message(STATUS "CMAKE_INSTALL_PREFIX = ${CMAKE_INSTALL_PREFIX}")

cmake_minimum_required(VERSION 3.0.2 FATAL_ERROR)

# CATALOG_CMAKE_INSTALL_PREFIX prepends a "/" to CMAKE_INSTALL_PREFIX if the
# latter doesn't start with "/", (i.e, the drive letter
# on Microsoft systems).  This makes the URI file:/// form come out
# right for catalog entries.
if(CMAKE_INSTALL_PREFIX MATCHES "^[^/]")
  set(CATALOG_CMAKE_INSTALL_PREFIX /${CMAKE_INSTALL_PREFIX})
else(CMAKE_INSTALL_PREFIX MATCHES "^[^/]")
  set(CATALOG_CMAKE_INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX})
endif(CMAKE_INSTALL_PREFIX MATCHES "^[^/]")
message(STATUS "CMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}")
message(STATUS "CATALOG_CMAKE_INSTALL_PREFIX=${CATALOG_CMAKE_INSTALL_PREFIX}")

# Define useful function.
find_program(XMLCATALOG_EXECUTABLE xmlcatalog)
function(install_xml_catalog arguments_LIST3 catalog)
  # Install an XML catalog using the information supplied in
  # arguments_LIST3, which is a list of arguments grouped by
  # threes where each group of three arguments correspond to
  # the three arguments of the "xmlcatalog --add" command.
  
  # Create the catalog if it does not exist.
  install(CODE "
if(NOT EXISTS \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/etc/xml/${catalog}\")
message(STATUS \"Creating \${CMAKE_INSTALL_PREFIX}/etc/xml/${catalog}\")
execute_process(COMMAND ${XMLCATALOG_EXECUTABLE} --noout 
  --create 
  \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/etc/xml/${catalog}\")
endif(NOT EXISTS \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/etc/xml/${catalog}\")
")

  list(LENGTH ${arguments_LIST3} narguments_LIST3)
  set(group_size 3)
  set(remove_indices 0 1 2)
  math(EXPR max_index "(${narguments_LIST3}/${group_size}) - 1")
  foreach(index RANGE 0 ${max_index})
    list(GET ${arguments_LIST3} 0 type)
    list(GET ${arguments_LIST3} 1 orig)
    list(GET ${arguments_LIST3} 2 replace)
    list(REMOVE_AT ${arguments_LIST3} ${remove_indices})
    install(CODE "
message(STATUS \"Updating \${CMAKE_INSTALL_PREFIX}/etc/xml/${catalog}\")
execute_process(COMMAND ${XMLCATALOG_EXECUTABLE} --noout
  --add \"${type}\" \"${orig}\" \"${replace}\" 
  \"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/etc/xml/${catalog}\")
"
      )
  endforeach(index RANGE 0 ${max_index})
endfunction(install_xml_catalog)

# Nothing to build.  Just follow directions at
# http://www.linuxfromscratch.org/blfs/view/svn/pst/DocBook.html to
# install files that are in the source tree.

# Install top-level source tree files.
set(globs
  ${CMAKE_SOURCE_DIR}/docbook.cat
  ${CMAKE_SOURCE_DIR}/*.dtd
  ${CMAKE_SOURCE_DIR}/*.mod
  )
# Make elements of path_list absolute PATH names.
file(GLOB path_list
  ${globs}
  )
# Add the ent subdirectory to path_list
list(APPEND path_list ${CMAKE_SOURCE_DIR}/ent)

find_program(INSTALL_EXECUTABLE install)
find_program(CP_EXECUTABLE cp)

install(CODE "
message(STATUS \"Installing \${CMAKE_INSTALL_PREFIX}/share/xml/docbook/xml-dtd-4.5\")
execute_process(
COMMAND ${INSTALL_EXECUTABLE} -v -d -m755 
\"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/share/xml/docbook/xml-dtd-4.5\")
"
  )

install(CODE "
execute_process(
COMMAND ${CP_EXECUTABLE} -v -af ${path_list}
\"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/share/xml/docbook/xml-dtd-4.5\")
"
  )

install(CODE "
message(STATUS \"Installing \${CMAKE_INSTALL_PREFIX}/etc/xml\")
execute_process(
COMMAND ${INSTALL_EXECUTABLE} -v -d -m755 
\"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/etc/xml\")
"
  )

set(commands
"public" "-//OASIS//DTD DocBook XML V4.5//EN" "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd"
"public" "-//OASIS//DTD DocBook XML CALS Table Model V4.5//EN" "file://${CATALOG_CMAKE_INSTALL_PREFIX}/share/xml/docbook/xml-dtd-4.5/calstblx.dtd"
"public" "-//OASIS//DTD XML Exchange Table Model 19990315//EN" "file://${CATALOG_CMAKE_INSTALL_PREFIX}/share/xml/docbook/xml-dtd-4.5/soextblx.dtd"
"public" "-//OASIS//ELEMENTS DocBook XML Information Pool V4.5//EN" "file://${CATALOG_CMAKE_INSTALL_PREFIX}/share/xml/docbook/xml-dtd-4.5/dbpoolx.mod"
"public" "-//OASIS//ELEMENTS DocBook XML Document Hierarchy V4.5//EN" "file://${CATALOG_CMAKE_INSTALL_PREFIX}/share/xml/docbook/xml-dtd-4.5/dbhierx.mod"
"public" "-//OASIS//ELEMENTS DocBook XML HTML Tables V4.5//EN" "file://${CATALOG_CMAKE_INSTALL_PREFIX}/share/xml/docbook/xml-dtd-4.5/htmltblx.mod"
"public" "-//OASIS//ENTITIES DocBook XML Notations V4.5//EN" "file://${CATALOG_CMAKE_INSTALL_PREFIX}/share/xml/docbook/xml-dtd-4.5/dbnotnx.mod"
"public" "-//OASIS//ENTITIES DocBook XML Character Entities V4.5//EN" "file://${CATALOG_CMAKE_INSTALL_PREFIX}/share/xml/docbook/xml-dtd-4.5/dbcentx.mod"
"public" "-//OASIS//ENTITIES DocBook XML Additional General Entities V4.5//EN" "file://${CATALOG_CMAKE_INSTALL_PREFIX}/share/xml/docbook/xml-dtd-4.5/dbgenent.mod"
"rewriteSystem" "http://www.oasis-open.org/docbook/xml/4.5" "file://${CATALOG_CMAKE_INSTALL_PREFIX}/share/xml/docbook/xml-dtd-4.5"
"rewriteURI" "http://www.oasis-open.org/docbook/xml/4.5" "file://${CATALOG_CMAKE_INSTALL_PREFIX}/share/xml/docbook/xml-dtd-4.5")

install_xml_catalog(commands "docbook")

# Do this for every pattern above so using the catalog file as the catalog
# is equivalent to using the docbook file as the catalog.
set(commands
"delegatePublic" "-//OASIS//DTD DocBook XML" "file://${CATALOG_CMAKE_INSTALL_PREFIX}/etc/xml/docbook"
"delegatePublic" "-//OASIS//DTD XML Exchange" "file://${CATALOG_CMAKE_INSTALL_PREFIX}/etc/xml/docbook"
"delegatePublic" "-//OASIS//ENTITIES DocBook XML" "file://${CATALOG_CMAKE_INSTALL_PREFIX}/etc/xml/docbook"
"delegatePublic" "-//OASIS//ELEMENTS DocBook XML" "file://${CATALOG_CMAKE_INSTALL_PREFIX}/etc/xml/docbook"
"delegateSystem" "http://www.oasis-open.org/docbook/" "file://${CATALOG_CMAKE_INSTALL_PREFIX}/etc/xml/docbook"
"delegateURI" "http://www.oasis-open.org/docbook/" "file://${CATALOG_CMAKE_INSTALL_PREFIX}/etc/xml/docbook"
)

install_xml_catalog(commands "catalog")

# LFS instructions at 
# http://www.linuxfromscratch.org/blfs/view/svn/pst/DocBook.html
# are open to interpretation for this part, but I think it means
# these previous versions should be dropped if they are handled
# a different way.  But I am not doing that so leave them in
# unless some issue is found in the future that I am not aware of
# now.

set(previous_versions
  4.1.2
  4.2
  4.3
  4.4
  )

foreach(DTDVERSION ${previous_versions})
  set(commands
    "public" "-//OASIS//DTD DocBook XML V${DTDVERSION}//EN" "http://www.oasis-open.org/docbook/xml/${DTDVERSION}/docbookx.dtd"
    "rewriteSystem" "http://www.oasis-open.org/docbook/xml/${DTDVERSION}" "file://${CATALOG_CMAKE_INSTALL_PREFIX}/share/xml/docbook/xml-dtd-4.5"
    "rewriteURI" "http://www.oasis-open.org/docbook/xml/${DTDVERSION}" "file://${CATALOG_CMAKE_INSTALL_PREFIX}/share/xml/docbook/xml-dtd-4.5"
    )
  install_xml_catalog(commands "docbook")

  # Do this for every pattern above except the "public" one which has already
  # been taken care of so using the catalog file as the catalog
  # is equivalent to using the docbook file as the catalog.
  set(commands
    "delegateSystem" "http://www.oasis-open.org/docbook/xml/${DTDVERSION}/" "file://${CATALOG_CMAKE_INSTALL_PREFIX}/etc/xml/docbook"
    "delegateURI" "http://www.oasis-open.org/docbook/xml/${DTDVERSION}/" "file://${CATALOG_CMAKE_INSTALL_PREFIX}/etc/xml/docbook"
    )
  install_xml_catalog(commands "catalog")

endforeach(DTDVERSION ${previous_versions})
