# Top-level CMakeLists.txt for the CMake-based build system
# of the docbook-xsl 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-xsl 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-xsl.html to
# install files that are in the source tree.

find_program(INSTALL_EXECUTABLE install)
find_program(CP_EXECUTABLE cp)
find_program(LN_EXECUTABLE ln)

set(path_list
  ${CMAKE_SOURCE_DIR}/VERSION
  ${CMAKE_SOURCE_DIR}/common
  ${CMAKE_SOURCE_DIR}/eclipse
  ${CMAKE_SOURCE_DIR}/epub
  ${CMAKE_SOURCE_DIR}/extensions
  ${CMAKE_SOURCE_DIR}/fo
  ${CMAKE_SOURCE_DIR}/highlighting
  ${CMAKE_SOURCE_DIR}/html
  ${CMAKE_SOURCE_DIR}/htmlhelp
  ${CMAKE_SOURCE_DIR}/images
  ${CMAKE_SOURCE_DIR}/javahelp
  ${CMAKE_SOURCE_DIR}/lib
  ${CMAKE_SOURCE_DIR}/manpages
  ${CMAKE_SOURCE_DIR}/params
  ${CMAKE_SOURCE_DIR}/profiling
  ${CMAKE_SOURCE_DIR}/roundtrip
  ${CMAKE_SOURCE_DIR}/slides
  ${CMAKE_SOURCE_DIR}/template
  ${CMAKE_SOURCE_DIR}/tests
  ${CMAKE_SOURCE_DIR}/tools
  ${CMAKE_SOURCE_DIR}/webhelp
  ${CMAKE_SOURCE_DIR}/website
  ${CMAKE_SOURCE_DIR}/xhtml
  ${CMAKE_SOURCE_DIR}/xhtml-1_1
)

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

install(CODE "
execute_process(
COMMAND ${CP_EXECUTABLE} -v -R ${path_list}
\"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/share/xml/docbook/xsl-stylesheets-1.78.1\")
"
  )

install(CODE "
execute_process(
COMMAND ${LN_EXECUTABLE} -s VERSION
\"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/share/xml/docbook/xsl-stylesheets-1.78.1/VERSION.xsl\")
"
  )

install(CODE "
execute_process(
COMMAND ${INSTALL_EXECUTABLE} -v -m644 -D ${CMAKE_SOURCE_DIR}/README
\"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/share/doc/docbook-xsl-1.78.1/README.txt\")
"
  )

set(globs
  ${CMAKE_SOURCE_DIR}/RELEASE-NOTES*
  ${CMAKE_SOURCE_DIR}/NEWS*
  )
# Make elements of path_list absolute PATH names.
file(GLOB path_list
  ${globs}
  )
install(CODE "
execute_process(
COMMAND ${INSTALL_EXECUTABLE} -v -m644 ${path_list}
\"\$ENV{DESTDIR}\${CMAKE_INSTALL_PREFIX}/share/doc/docbook-xsl-1.78.1\")
"
  )

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
  "rewriteSystem" "http://docbook.sourceforge.net/release/xsl/1.78.1" "file://${CATALOG_CMAKE_INSTALL_PREFIX}/share/xml/docbook/xsl-stylesheets-1.78.1"
  "rewriteURI" "http://docbook.sourceforge.net/release/xsl/1.78.1" "file://${CATALOG_CMAKE_INSTALL_PREFIX}/share/xml/docbook/xsl-stylesheets-1.78.1"
  "rewriteSystem" "http://docbook.sourceforge.net/release/xsl/current" "file://${CATALOG_CMAKE_INSTALL_PREFIX}/share/xml/docbook/xsl-stylesheets-1.78.1"
  "rewriteURI" "http://docbook.sourceforge.net/release/xsl/current" "file://${CATALOG_CMAKE_INSTALL_PREFIX}/share/xml/docbook/xsl-stylesheets-1.78.1"
)

install_xml_catalog(commands "catalog")

# LFS instructions at
# http://www.linuxfromscratch.org/blfs/view/svn/pst/docbook-xsl.html
# state what to do if other previous versions of docbook-xsl are
# required.  Essentially, the steps are to install the older
# docbook-xsl package as well, then run xmlcatalog --add <type> <orig>
# <replace> where <type> is first rewriteSystem then rewriteURI.  Note
# it is a different scenario then what is done for docbook-xml where
# old versions of docbook-xml do not have to be installed.  With luck,
# we won't need to install different versions of docbook-xsl so ignore
# these docbook-xsl multiversion instructions for now.
