#
# LDASTools frameCPP - A library implementing the LIGO/Virgo frame specification
#
# Copyright (C) 2018 California Institute of Technology
#
# LDASTools frameCPP 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 frameCPP 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
#

function( framecpp_target_gwf )
  set(options
    TEST_FRAME_MODE
    )
  set(oneValueArgs
    BUILDER
    TARGET
    DESCRIPTION
    GPS_START
    DT
    )
  set(multiValueArgs
    VERSIONS
    )

  cmake_parse_arguments( ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} )

  if ( NOT ARG_BUILDER )
    set( ARG_BUILDER framecpp_sample )
  endif( )
  if ( NOT ARG_GPS_START )
    set( ARG_GPS_START 600000000 )
  endif( )
  if ( NOT ARG_DT )
    set( ARG_DT 1 )
  endif( )
  foreach ( ver ${ARG_VERSIONS} )
    set( filename Z-${ARG_DESCRIPTION}${ver}-${ARG_GPS_START}-${ARG_DT}.gwf )
    list( APPEND
      filenames
      "${CMAKE_CURRENT_BINARY_DIR}/${filename}"
      )
    if ( ARG_TEST_FRAME_MODE )
      set( cmd $<TARGET_FILE:${ARG_BUILDER}> --test-frame-mode --version ${ver} )
    elseif( ARG_BUILDER STREQUAL "framecpp_sample_c" )
      set( cmd $<TARGET_FILE:${ARG_BUILDER}> )
    else( )
      set( cmd $<TARGET_FILE:${ARG_BUILDER}>
        --version ${ver}
		    --description ${ARG_DESCRIPTION}${ver}
		    --start-time ${ARG_GPS_START}
		    --duration ${ARG_DT}
        )
    endif( )
    add_custom_command(
      OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${filename}
      DEPENDS ${ARG_BUILDER}
      COMMAND	${cmd}
      )
  endforeach( )
  add_custom_target(
    ${ARG_TARGET}
    ALL
    DEPENDS ${filenames}
    )
endfunction( )
set(LDAS_DIRECTORY_BUILD_LIB_FRAMECPP ${CMAKE_BINARY_DIR} )

set( INCLUDES
  ${CMAKE_CURRENT_SOURCE_DIR}
  ${CMAKE_BINARY_DIR}/include
  ${CMAKE_BINARY_DIR}
  ${LDASTOOLS_INCLUDE_DIRS}
  )

set( LDADD
  framecpp
  framecpp8
  framecpp7
  framecpp6
  framecpp4
  framecpp3
  framecppcmn
  )
#if HAVE_LIBZ
#LDADD += -lz
#endif

#bin_PROGRAMS =
#noinst_PROGRAMS =
#TESTS =

#========================================================================
# Programs based on the latest supported frame spec
#========================================================================

#bin_PROGRAMS += framecpp_compare
set( framecpp_compare_SOURCES  framecmp.cc )
#framecpp_compare_LDFLAGS = $(BOOST_LDFLAGS) 

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

#bin_PROGRAMS += framecpp_sample
set( framecpp_sample_SOURCES FrSample.cc )
set( framecpp_sample_CPPFLAGS
  # -UFR_SAMPLE_VERSION
  )

list( APPEND noinst_HEADERS
  FrSample.hh
  FrSample3.tcc
  FrSample4.tcc
  FrSample6.tcc
  FrSample7.tcc
  FrSample8.tcc
  )
cx_target_executable( framecpp_sample
  SOURCES ${framecpp_sample_SOURCES}
  DEFINES ${framecpp_sample_CPPFLAGS}
  LDADD ${LDADD} ${Boost_PROGRAM_OPTIONS_LIBRARY}
  INCLUDE_DIRECTORIES BEFORE PRIVATE
    ${INCLUDES} ${Boost_INCLUDE_DIRS}
  )

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

set( framecpp_sample_c_SOURCES framecpp_sample_c.c )
set( framecpp_sample_c_CFLAGS ${CFLAGS_NON_FATAL_WARNINGS} )
set( framecpp_sample_c_LDADD  framecppc )
cx_target_executable( framecpp_sample_c
  NOINST
  SOURCES ${framecpp_sample_c_SOURCES}
  DEFINES ${framecpp_sample_c_CPPFLAGS}
  LDADD ${framecpp_sample_c_LDADD}
  INCLUDE_DIRECTORIES BEFORE PRIVATE
    ${INCLUDES}
  )

##=========================================================================
## Test frames
##=========================================================================

framecpp_target_gwf(
  TARGET test_frame_set_1
  TEST_FRAME_MODE
  DESCRIPTION R_std_test_frame_ver
  VERSIONS 3 4 6 7 8
  )
framecpp_target_gwf(
  TARGET test_frame_set_2
  DESCRIPTION std_test_frame_ver
  GPS_START 700000000
  VERSIONS 6 7 8
  )
framecpp_target_gwf(
  TARGET test_frame_set_3
  BUILDER framecpp_sample_c
  DESCRIPTION Sample_C_ver
  VERSIONS 8
  )

##========================================================================
## Generate text
##========================================================================
#
#doxygen_INPUT = \
#	$(srcdir)/framecpp_sample.txt
#
#doxygen_OUTPUT = $(addprefix $(builddir)/,$(notdir $(doxygen_INPUT)))
#doxygen_DIFFS = $(addprefix $(builddir)/,$(addsuffix .diff,$(notdir $(basename $(doxygen_INPUT)))))
#
#$(builddir)/%.txt: $(builddir)/%
#	$< --doxygen-help > $@
#
#$(builddir)/%.diff: $(builddir)/%.txt $(srcdir)/%.txt
#	diff $^ > $@ 2>&1
#
#.PHONEY: doxygen-diffs doxygen-update
#
#doxygen-diffs: $(doxygen_DIFFS)
#
#doxygen-update: $(doxygen_OUTPUT)
#	cp $(doxygen_OUTPUT) $(srcdir)
#
