# tk/CMakeLists.txt for installed PLplot examples
### Process this file with cmake to produce Makefile
###
# Copyright (C) 2009 Alan W. Irwin
#
# This file is part of PLplot.
#
# PLplot 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.
#
# PLplot 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 PLplot; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA

set(tk_FILES)

set(tk_SRC_FILES
  README.tkdemos
  runAllDemos.tcl
  runExtendedDemos.tcl
  tkdemos.tcl
  tk01
  tk02
  tk03
  tk04
  )

set(tk_STRING_INDICES
  "01"
  "02"
  "03"
  "04"
  "05"
  "06"
  "07"
  "08"
  "09"
  "10"
  "11"
  "12"
  "13"
  "14"
  "15"
  "16"
  "17"
  "18"
  "19"
  "20"
  "21"
  "22"
  "23"
  "24"
  "25"
  "26"
  "27"
  "28"
  "29"
  "30"
  )

# Copy files to the binary directory (if different) for generating tclIndex
# This ensures no files are created in the source tree.
if(NOT CMAKE_CURRENT_BINARY_DIR STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
  foreach(_file ${tk_SRC_FILES})
    add_custom_command(
      OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}
      COMMAND ${CMAKE_COMMAND}
      -E copy
      ${CMAKE_CURRENT_SOURCE_DIR}/${_file}
      ${CMAKE_CURRENT_BINARY_DIR}/${_file}
      DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${_file}
      )
    set(tk_FILES ${tk_FILES} ${CMAKE_CURRENT_BINARY_DIR}/${_file})
  endforeach(_file ${tk_SRC_FILES})
endif(NOT CMAKE_CURRENT_BINARY_DIR STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")  

foreach(STRING_INDEX ${tk_STRING_INDICES})
  set(_file x${STRING_INDEX}.tcl)
  add_custom_command(
    OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${_file}
    COMMAND ${CMAKE_COMMAND}
    -E copy
    ${CMAKE_SOURCE_DIR}/tcl/${_file}
    ${CMAKE_CURRENT_BINARY_DIR}/${_file}
    DEPENDS ${CMAKE_SOURCE_DIR}/tcl/${_file}
    )
  set(tk_FILES ${tk_FILES} ${CMAKE_CURRENT_BINARY_DIR}/${_file})
endforeach(STRING_INDEX ${tk_STRING_INDICES})

add_custom_command(
  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/plgrid.tcl
  COMMAND ${CMAKE_COMMAND}
  -E copy
  ${CMAKE_SOURCE_DIR}/tcl/plgrid.tcl
  ${CMAKE_CURRENT_BINARY_DIR}/plgrid.tcl
  DEPENDS ${CMAKE_SOURCE_DIR}/tcl/plgrid.tcl
  )
set(tk_FILES ${tk_FILES} ${CMAKE_CURRENT_BINARY_DIR}/plgrid.tcl)

add_custom_command(
  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/tclIndex
  COMMAND ${TCL_TCLSH} ${MKTCLINDEX} ${MKTCLINDEX_ARGS}
  DEPENDS ${tk_FILES}
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
  )

# The second and fourth Tk examples depend on Itk to work.
set(tk_SCRIPTS tk01 tk03)
set(tk_SRC xtk01.c)
  
if(ENABLE_itkX)
  list(APPEND tk_SCRIPTS tk02 tk04)
  list(APPEND tk_SRC xtk02.c xtk04.c)
endif(ENABLE_itkX)

include_directories(
  ${TCL_INCLUDE_PATH}
  ${TK_INCLUDE_PATH}
  ${INCLUDE_DIR}
  )
foreach(TK_SRC_FILE ${tk_SRC})
  string(REGEX REPLACE ".c$" "" TK_EXE ${TK_SRC_FILE})
  add_executable(${TK_EXE} ${TK_SRC_FILE})
  if(BUILD_SHARED_LIBS)
    SET_SOURCE_FILES_PROPERTIES(${TK_SRC_FILE}
      PROPERTIES COMPILE_FLAGS "-DUSINGDLL" )
  endif(BUILD_SHARED_LIBS)
  target_link_libraries(${TK_EXE} plplottcltk${LIB_TAG} tclmatrix${LIB_TAG} plplot${LIB_TAG} ${MATH_LIB})
  set_property(GLOBAL APPEND PROPERTY TARGETS_examples_tk ${TK_EXE})
endforeach(TK_SRC_FILE ${tk_SRC})

add_custom_target(tclIndex_examples_tk ALL
  DEPENDS ${tk_FILES} ${CMAKE_CURRENT_BINARY_DIR}/tclIndex
  )
set_property(GLOBAL APPEND PROPERTY TARGETS_examples_tk tclIndex_examples_tk)
