# Top-level CMakeLists.txt for the CMake-based build system
# of the xmlcatalog-wrapper 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

# This project is one method of beating the argument mangling issue
# for xmlcatalog when xmlcatalog is run directly from a shell (such
# as bash.exe) which is linked to the MSYS runtime.
project(xmlcatalog-wrapper 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)

# Do nothing unless WIN32 AND NOT CYGWIN
# Jury is still out whether this is required for Cygwin, but I
# presume not until proven otherwise.

if(WIN32 AND NOT CYGWIN)
  # Configure xmlcatalog-wrapper.sh.in to xmlcatalog and install it.
  # The xmlcatalog name of the configured script does not clash with
  # the name of the executable xmlcatalog.exe on Windows platforms and
  # will be executed in preference to xmlcatalog.exe whenever an
  # autotools configure script or other shell environment runs the
  # command "xmlcatalog".
  set(XMLCATALOG_EXECUTABLE ${CMAKE_INSTALL_PREFIX}/bin/xmlcatalog.exe)
  set(FILTER_ARGUMENTS_SCRIPT_LOCATION ${CMAKE_INSTALL_PREFIX}/share/xmlcatalog-wrapper/filter_arguments.cmake)
  configure_file(
    ${CMAKE_SOURCE_DIR}/xmlcatalog-wrapper.sh.in
    ${CMAKE_BINARY_DIR}/xmlcatalog
    @ONLY
    )

  # Use relative (to CMAKE_INSTALL_PREFIX) installation locations
  # which must be consistent with the above XMLCATALOG_EXECUTABLE
  # and FILTER_ARGUMENTS_SCRIPT_LOCATION variables.
  install(
    PROGRAMS ${CMAKE_BINARY_DIR}/xmlcatalog
    DESTINATION bin
    )
  install(
    FILES ${CMAKE_SOURCE_DIR}/filter_arguments.cmake
    DESTINATION share/xmlcatalog-wrapper
    )

endif(WIN32 AND NOT CYGWIN)