#------------------------------------------------------------------------
# -*- mode: cmake -*-
#------------------------------------------------------------------------
#
# LDASTools Frame Library  - Frame library
#
# Copyright (C) 2018 California Institute of Technology
#
# LDASTools Frame Library  is free software; you may redistribute
# it and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2 (GPLv2)
#  of the License or at your discretion, any later version.
#
# LDASTools Frame Library is distributed in the hope that it will
# be useful, but without any warranty or even the implied warranty of
# merchantability or fitness for a particular purpose. See the GNU
# General Public License (GPLv2) for more details.
#
# Neither the names of the California Institute of Technology (Caltech),
# The Massachusetts Institute of Technology (M.I.T), The Laser
# Interferometer Gravitational-Wave Observatory (LIGO), nor the names
# of its contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# You should have received a copy of the licensing terms for this
# software included in the file LICENSE located in the top-level
# directory of this package. If you did not, you can view a copy at
# http://dcc.ligo.org/M1500244/LICENSE
#
#------------------------------------------------------------------------
cmake_minimum_required(VERSION 3.2)

set( LDAS_TOOLS_FRAMECPP_PACKAGE_NAME ldas-tools-framecpp )
set( LDAS_TOOLS_FRAMECPP_VERSION      2.8.1 )

set( LDAS_TOOLS_CMAKE_VERSION         1.2.3 )
set( LDAS_TOOLS_AL_VERSION            2.6.7 )

set( FRAME_SPEC_VERSION 8 )

set(
  PROJECT_DESCRIPTION
  "This provides the libraries for the framecpp library."
  )
set(
  PROJECT_DESCRIPTION_LONG
  "This provides a collection of classes and fuctions"
  "needed to manipulate gravitational wave frame files"
  "as used by the LDAS Tools Suite."
  )

set( BOOST_MINIMUM_VERSION 1.67 )

#------------------------------------------------------------------------
#------------------------------------------------------------------------

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
set(CMAKE_CXX_EXTENSIONS NO)
if ( NOT CMAKE_OSX_ARCHITECTURES )
  set( CMAKE_OSX_ARCHITECTURES x86_64 )
endif( )

#------------------------------------------------------------------------
# Locate the standard LDAS Tools cmake macro directory
#------------------------------------------------------------------------

find_package(
  LDASToolsCMake
  CONFIG
  REQUIRED
  HINTS ${CMAKE_INSTALL_PREFIX}/share/ldas-tools/cmake/Modules
  )

#========================================================================
# Start pulling in the LDAS Tools macros for easy setup
#========================================================================

find_package(AxLDASTools)

cm_init(
  ${LDAS_TOOLS_FRAMECPP_PACKAGE_NAME}
  "${LDAS_TOOLS_FRAMECPP_VERSION}"
  ${LDAS_TOOLS_BUG_REPORT_URL}
  ${LDAS_TOOLS_FRAMECPP_PACKAGE_NAME}
  ${LDAS_TOOLS_HOMEPAGE_URL}
  DESCRIPTION "${PROJECT_DESCRIPTION}"
  )

#dnl =====================================================================
#dnl  Prepare a header file to store the glorious configuration info
#dnl =====================================================================
set( PROJECT_CONFIG_FILENAME
     "${CMAKE_CURRENT_BINARY_DIR}/framecpp_config.h" )

ch_top(
  "/*"
  " * This software impliments the LIGO / Virgo frame specification"
  " *"
  " * Copyright (C) 2018 California Institute of Technology"
  " *"
  " *  This program is free software; you may redistribute it and/or modify"
  " *  it under the terms of the GNU General Public License as published by"
  " *  the Free Software Foundation; either version 2 (GPLv2) of the"
  " *  License or at your discretion, any later version."
  " *"
  " *  This program is distributed in the hope that it will be useful, but"
  " *  without any warranty or even the implied warranty of merchantability"
  " *  or fitness for a particular purpose. See the GNU General Public"
  " *  License (GPLv2) for more details."
  " *"
  " *  Neither the names of the California Institute of Technology (Caltech),"
  " *  The Massachusetts Institute of Technology (M.I.T), The Laser"
  " *  Interferometer Gravitational-Wave Observatory (LIGO), nor the names"
  " *  of its contributors may be used to endorse or promote products derived"
  " *  from this software without specific prior written permission."
  " *"
  " *  You should have received a copy of the licensing terms for this"
  " *  software included in the file LICENSE located in the top-level"
  " *  directory of this package. If you did not, you can view a copy at"
  " *  http://dcc.ligo.org/M1500244/LICENSE"
  " *"
  " */"
  " "
  "#ifndef FRAMECPP_CONFIG_H"
  "#define FRAMECPP_CONFIG_H"
  " "
  )
ch_bottom(
  " "
  "#if HAVE__PRAGMA"
  "#define DO_PRAGMA(x) _Pragma(#x)"
  "#else /* HAVE__PRAGMA */"
  "#define DO_PRAGMA(x)"
  "#endif /* HAVE__PRAGMA */"

  "#if HAVE_PRAGMA_WIMPLICIT_FALLTHROUGH"
  "#define BEGIN_WIMPLICIT_FALLTHROUGH \\"
  "DO_PRAGMA(GCC diagnostic push) \\"
  "DO_PRAGMA(GCC diagnostic ignored \"-Wimplicit-fallthrough\")"
  "#define END_WIMPLICIT_FALLTHROUGH \\"
  "DO_PRAGMA(GCC diagnostic pop)"
  "#else /* HAVE_PRAGMA_WIMPLICIT_FALLTHROUGH */"
  "#define BEGIN_WIMPLICIT_FALLTHROUGH"
  "#define END_WIMPLICIT_FALLTHROUGH"
  "#endif /* HAVE_PRAGMA_WIMPLICIT_FALLTHROUGH */"

  "#endif /* FRAMECPP_CONFIG_H */"
   )
#
if ( APPLE )
  cm_msg_notice( "Setting RPATH for APPLE" )
  set( CMAKE_INSTALL_RPATH "@loader_path/../${CMAKE_INSTALL_LIBDIR}" )
elseif ( UNIX )
  cm_msg_notice( "Setting RPATH for LINUX" )
  set( CMAKE_INSTALL_RPATH "$ORIGIN/../${CMAKE_INSTALL_LIBDIR}:$$ORIGIN/:${CMAKE_INSTALL_FULL_LIBDIR}" )
  #set( CMAKE_SKIP_BUILD_RPATH TRUE )
  #set( CMAKE_BUILD_WITH_INSTALL_RPATH FALSE )
  #set( CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_FULL_LIBDIR}" )
  #set( CMKAE_INSTALL_RPATH_USE_LINK_PATH TRUE )
  #set( CMAKE_BUILD_WITH_INSTALL_RPATH FALSE )
  #list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${CMAKE_INSTALL_PREFIX}/lib" isSystemDir)
  #if("${isSystemDir}" STREQUAL "-1")
  #  set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
  #endif( )
else()
  cm_msg_notice( "Setting RPATH for DEFAULT" )
endif( )

#dnl =====================================================================
#dnl  Check for compilers and language features
#dnl =====================================================================
#
#AX_LDAS_PROG_CC
#
#AX_LDAS_PROG_CXX
#AX_CXX_COMPILE_STDCXX([11],[noext],[mandatory])
#AX_LDAS_CXX_CONSTEXPR
#AX_LDAS_CXX_EXCEPTIONS_RETHROW
#AX_LDAS_CXX_TEMPLATE_ALIASES
#AX_LDAS_CXX_TEMPLATE_ALIASES_VIA_USING
#AX_LDAS_CXX_TEMPLATE_AS_TEMPLATE_PARAMETER
#
#AX_LDAS_ARG_WITH_OPTIMIZATION
#
#AC_MSG_CHECKING([if C preprocessor supports _Pragma])
#AC_COMPILE_IFELSE(
#  [ AC_LANG_PROGRAM([[#include <stdio.h>]
#		     [#define DO_PRAGMA(x) _Pragma(#x)]
#		     [#define WORKING() DO_PRAGMA(message("WORKING _Pragma"))]
#		     [WORKING()]
#                     [const char hw[] = "Hello, World\n";]],
#                     [[fputs (hw, stdout );]]) ],
#  [ AC_DEFINE([HAVE__PRAGMA],[1],[1 if the preprocessor support _Pragma()])
#    AC_MSG_RESULT([yes])
#  ],
#  [ AC_MSG_RESULT([no]) ] )
#
#AC_MSG_CHECKING([if C++ compiler supports pragma: -Wimlicit_fallthrough])
#AC_LANG_PUSH([C++])
#ldas_saved_cxxflags=${CXXFLAGS}
#CXXFLAGS="${CXXFLAGS} -Werror -Wimplicit-fallthrough"
#AC_COMPILE_IFELSE(
#  [ AC_LANG_PROGRAM([[#include <stdio.h>]
#                     [const char hw[] = "Hello, World\n";]],
#                     [#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"]
#                     [[fputs (hw, stdout );]]) ],
#  [ AC_DEFINE([HAVE_PRAGMA_WIMPLICIT_FALLTHROUGH],[1],[1 if the compiler support the -Wimplicit-fallthrough pragma])
#    AC_MSG_RESULT([yes])
#  ],
#  [ AC_MSG_RESULT([no]) ] )
#CXXFLAGS=${ldas_saved_cxxflags}
#AC_LANG_POP()
#
#dnl =====================================================================
#dnl  Checking if the OS is 64 bit
#dnl =====================================================================
#
#AX_LDAS_SYS_64BIT_OS
#
#dnl =====================================================================
#dnl  Check for key programs
#dnl =====================================================================
#
#AX_LDAS_PROG_LIBTOOL
#AX_LDAS_PROG_PKGBUILDER
#AX_LDAS_PROG_DOXYGEN
#AX_LDAS_PROG_VALGRIND
#AX_LDAS_PROG_ABI_DUMPER
#AX_LDAS_PROG_ABI_COMPLIANCE_CHECKER
#AX_LDAS_PROG_PKG_CONFIG
#
#AC_CHECK_PROGS([EXPR],[expr])
#
#dnl ---------------------------------------------------------------------
#dnl  Checks for packaging
#dnl ---------------------------------------------------------------------
#
#AX_LDAS_PACKAGING_DEB
#AX_LDAS_PACKAGING_MACPORTS
#AX_LDAS_PACKAGING_RPM
#
#dnl =====================================================================
#dnl  Check for header files
#dnl =====================================================================
#
#AC_HEADER_STDC
#AC_CHECK_HEADERS([mntent.h])
#AC_CHECK_HEADERS([sys/mnttab.h])
#AC_CHECK_HEADERS([sys/proc.h])
#AC_CHECK_HEADERS([sys/stat.h])
#AC_CHECK_HEADERS([sys/sysctl.h])
#AC_CHECK_HEADERS([sys/sysinfo.h])
#AC_CHECK_HEADERS([sys/types.h])
#AC_CHECK_HEADERS([dirent.h])
#AC_CHECK_HEADERS([procfs.h])
#AC_CHECK_HEADERS([stdlib.h])
#
#AX_LDAS_CHECK_HEADER_FOR_SUBST([unordered_map],[HAVE_UNORDERED_MAP])
#AX_LDAS_CHECK_HEADER_FOR_SUBST([map],[HAVE_MAP])
#AX_LDAS_CHECK_HEADER_FOR_SUBST([memory],[HAVE_MEMORY])

# =======================================================================
#  Check for key programs
# =======================================================================

find_package(
  Boost ${BOOST_MINIMUM_VERSION} REQUIRED
  COMPONENTS
  program_options
  )

#AC_CHECK_HEADERS([boost/test/unit_test.hpp],[],[AC_MSG_ERROR(Could not find a version of the boost Unit Test Framework)])
#CPPFLAGS="${saved_CPPFLAGS}"
#LDFLAGS="${saved_LDFLAGS}"
#AX_BOOST_PROGRAM_OPTIONS
#
#dnl =====================================================================
#dnl  Check for structures/sizes
#dnl =====================================================================

cm_c_bigendian( )
cx_msg_debug_variable( WORDS_BIGENDIAN )
if ( WORDS_BIGENDIAN )
  cm_subst( LDAS_WORDS_BIGENDIAN 1 )
else( )
  set( LDAS_WORDS_BIGENDIAN 0 )
  cm_subst( LDAS_WORDS_BIGENDIAN 0 )
endif( )
cx_msg_debug_variable( LDAS_WORDS_BIGENDIAN )
#AX_LDAS_CHECK_SIZES
#AX_LDAS_STRUCT_PROCFS_PSINFO
#AX_LDAS_STRUCT_PROCFS_STAT
#
# =====================================================================
#  Check for individual libraries
# =====================================================================

#AX_LIB_SOCKET_NSL
#AC_CHECK_LIB([rt],[clock_gettime])
#AC_CHECK_LIB([pthread],[pthread_cancel])
#AX_LDAS_CHECK_LIB_UMEM
cx_check_lib_z( )
cx_check_lib_framel( )
cx_check_lib_md5sum( )

#dnl =====================================================================
#dnl  Check for individual functions
#dnl =====================================================================
#
#AC_CHECK_FUNCS([clock_gettime])
#AC_FUNC_STRERROR_R
#AC_CHECK_FUNCS([fpathconf])
#AC_CHECK_FUNCS([dirfd])
#AC_CHECK_FUNCS([fdopendir])
#AC_EGREP_CPP([dirent_header_defines_dirfd],[
#  #include <sys/types.h>
#  #include <dirent.h>
#  #ifdef dirfd
#   dirent_header_defines_dirfd
#  #endif],
#  [AC_DEFINE([HAVE_DIRFD_MACRO],[1],[Defined if dirfd() is a macro in dirent.h])])
#
#AX_LDAS_CHECK_FUNC_SWAP_ALL
#AX_LDAS_CHECK_PTHREAD_READ_WRITE_LOCK
#
#dnl =====================================================================
#dnl  Check for supported classes
#dnl =====================================================================
#
#AX_LDAS_CXX_TEMPLATE_MOVE
#AX_LDAS_CXX_UNIQUE_PTR
#
#dnl =====================================================================
#dnl Look for additional ldas-tools pieces
#dnl =====================================================================
#
#AS_IF([test "x$enable_documentation_only" != "xyes"], [
#  AX_LDAS_PKG_CHECK([ldastoolsal])
#])
#
#dnl =====================================================================
#dnl
#dnl =====================================================================
#
#FRAMECPP_VERSION_NUMBER="`echo ${PACKAGE_VERSION} | sed -e 's/\./00000 + /' -e 's/\./000 + /' -e 's/\..*$//'`"
#FRAMECPP_VERSION_NUMBER="`${EXPR} $FRAMECPP_VERSION_NUMBER`"
#AC_DEFINE_UNQUOTED([FRAMECPP_VERSION_NUMBER],[${FRAMECPP_VERSION_NUMBER}],[Version of the library expressed as single number])
#AC_SUBST([FRAMECPP_VERSION_NUMBER])

string( TIMESTAMP FRAMECPP_BUILD_DATE )
cm_subst( FRAMECPP_BUILD_DATE ${FRAMECPP_BUILD_DATE} )

#AC_DEFINE([LIBEXECDIR],
#	  ["@libexecdir@"],
#	  [Directory for installing executables that other other programs run])
#AX_LDAS_DEF_VARIABLE([PACKAGE_VERSION_NUMERIC],
#		     ["`echo ${PACKAGE_VERSION} |  ${SED} -e 's/[[~]][(]alpha|beta[)].*$//'`"])
#
# =====================================================================
# Look for additional ldas-tools pieces
# =====================================================================

if( NOT ENABLE_DOCUMENTATION_ONLY )
  cm_pkg_check_modules(
    PREFIX LDASTOOLS
    REQUIRED
    MODULES
      ldastoolsal>=${LDAS_TOOLS_AL_VERSION}
    )
endif( )
#
#ax_ldas_libdirs=""
#ax_ldas_pythonpath=""
#for file in \
#	Version8 \
#	Version7 \
#	Version6 \
#	Version4 \
#	Version3 \
#	Common
#do
#	ax_ldas_libdirs="${ax_ldas_libdirs}:"'${abs_top_builddir}/src/'"${file}"'/.libs'
#	ax_ldas_pythonpath="${ax_ldas_pythonpath}:"'${abs_top_builddir}/src/'"${file}"
#done
#AC_SUBST([ax_ldas_libdirs])
#AC_SUBST([ax_ldas_pythonpath])

#========================================================================
# Traverse the tree
#========================================================================

add_subdirectory(src)
add_subdirectory(test)
add_subdirectory(doc)

#========================================================================
# Commit configuration information to the file
#========================================================================

cm_config_headers( ${PROJECT_CONFIG_FILENAME} )

#========================================================================
# Prepare the distribution
#========================================================================

cx_cpack(
  MACPORTS_CATEGORY science
  MAINTAINER "Edward Maros <ed.maros@ligo.org>"
  )

#========================================================================
# Report configuration information
#  CMAKE_MAKE_PROGRAM:FILEPATH
#========================================================================
cm_msg_notice( "=========================================================")
cm_msg_notice( " Project ${PROJECT_NAME} has now ben successfully configured:" )
cm_msg_notice( "      VERSION: ${PROJECT_VERSION}" )
cm_msg_notice( " " )
cm_msg_notice( " installation directories:" )
cm_msg_notice( "      CMAKE_INSTALL_BINDIR           ${CMAKE_INSTALL_BINDIR}" )
cm_msg_notice( "      CMAKE_INSTALL_DATADIR          ${CMAKE_INSTALL_DATADIR}" )
cm_msg_notice( " package information:" )
cm_msg_notice( "      LDASTOOLS_FOUND:               ${LDASTOOLS_FOUND}" )
cm_msg_notice( "      LDASTOOLS_LIBRARIES:           ${LDASTOOLS_LIBRARIES}" )
cm_msg_notice( "      LDASTOOLS_LIBRARIES_FULL_PATH: ${LDASTOOLS_LIBRARIES_FULL_PATH}" )
cm_msg_notice( " " )
cm_msg_notice( "  Now run 'cmake --build .' to build ${PROJECT_NAME}" )
cm_msg_notice( "  and run 'cpack' to package ${PROJECT_NAME}" )
cm_msg_notice( "=========================================================" )
