cmake_minimum_required(VERSION 3.14)
project(tcl-static-build)
include(ExternalProject)
include(FetchContent)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/modules")
include(Utils)

if(NOT (MSVC OR UNIX))
  message(FATAL_ERROR "This build currenly works only with Microsoft Visual Studio and Linux. MacOS is planned.")
endif()

if(NOT DEFINED TCL_STATIC_BUILD_BUILD_AND_INSTALL_TCL_TK)
  set(TCL_STATIC_BUILD_BUILD_AND_INSTALL_TCL_TK On)
endif()

if(APPLE OR UNIX)
  find_program(TCL_STATIC_BUILD_ZIP_EXECUTABLE NAMES zip)
  if(NOT TCL_STATIC_BUILD_ZIP_EXECUTABLE)
    message(FATAL_ERROR "Could not find 'zip' which is necessary for building with ZipFs support.")
  endif()
endif()

if(APPLE OR UNIX)
  find_program(TCL_STATIC_BUILD_LIBTOOL libtool)
  if(NOT TCL_STATIC_BUILD_LIBTOOL)
    message(FATAL_ERROR "Could not find 'libtool' which is necessary to build static archives")
  endif()
endif()

set(TCL_STATIC_BUILD_VERSION "9.1")
set(TCL_STATIC_ZIP_VERSION 91a1)

set(TCL_STATIC_BUILD_CRITCL_VERSION "3.3.1")
set(TCL_STATIC_BUILD_TCLLIB_VERSION "2.0")

if(TCL_STATIC_BUILD_TCL_LOCAL_SOURCES)
  to_absolute_path(${TCL_STATIC_BUILD_TCL_LOCAL_SOURCES} TCL_STATIC_BUILD_URL)
else()
  set(TCL_STATIC_BUILD_URL http://prdownloads.sourceforge.net/tcl/tcl${TCL_STATIC_ZIP_VERSION}-src.zip)
endif()
if(TCL_STATIC_BUILD_TK_LOCAL_SOURCES)
  to_absolute_path(${TCL_STATIC_BUILD_TK_LOCAL_SOURCES} TK_STATIC_BUILD_URL)
else()
  set(TK_STATIC_BUILD_URL http://prdownloads.sourceforge.net/tcl/tk${TCL_STATIC_ZIP_VERSION}-src.zip)
endif()

if(TCL_STATIC_BUILD_CRITCL_LOCAL_SOURCES)
  to_absolute_path(${TCL_STATIC_BUILD_CRITCL_LOCAL_SOURCES} CRITCL_STATIC_BUILD_URL)
else()
  set(CRITCL_STATIC_BUILD_URL http://github.com/andreas-kupries/critcl/zipball/${TCL_STATIC_BUILD_CRITCL_VERSION})
endif()

if(TCL_STATIC_BUILD_TCLLIB_LOCAL_SOURCES)
  to_absolute_path(${TCL_STATIC_BUILD_TCLLIB_LOCAL_SOURCES} CRITCL_STATIC_BUILD_URL)
else()
  set(TCLLIB_STATIC_BUILD_URL https://core.tcl-lang.org/tcllib/uv/tcllib-${TCL_STATIC_BUILD_TCLLIB_VERSION}.zip)
endif()

if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
  if(APPLE OR UNIX)
    set(EXAMPLE_INSTALL_PREFIX "/home/user/MyWorkspace")
  else()
    set(EXAMPLE_INSTALL_PREFIX "C:\User\user\MyWorkspace")
  endif()
  message(WARNING "CMAKE_INSTALL_PREFIX is the default ${CMAKE_INSTALL_PREFIX}. It is recommended to override it to your local workspace, eg. 'cmake .. -DCMAKE_INSTALL_PREFIX=${EXAMPLE_INSTALL_PREFIX}'")
endif()

set(TCL_SOURCES tcl${TCL_STATIC_BUILD_VERSION})
FetchContent_Declare(
  ${TCL_SOURCES}
  URL ${TCL_STATIC_BUILD_URL}
  DOWNLOAD_EXTRACT_TIMESTAMP FALSE
)

set(CRITCL_SOURCES critcl${TCL_STATIC_BUILD_CRITCL_VERSION})
FetchContent_Declare(
  ${CRITCL_SOURCES}
  URL ${CRITCL_STATIC_BUILD_URL}
  DOWNLOAD_EXTRACT_TIMESTAMP FALSE
)

set(TCLLIB_SOURCES tcllib${TCL_STATIC_BUILD_TCLLIB_VERSION})
FetchContent_Declare(
  ${TCLLIB_SOURCES}
  URL ${TCLLIB_STATIC_BUILD_URL}
  DOWNLOAD_EXTRACT_TIMESTAMP FALSE
)

if(TCL_STATIC_BUILD_TK)
  set(TK_SOURCES tk${TCL_STATIC_BUILD_VERSION})
  FetchContent_Declare(
    ${TK_SOURCES}
    URL ${TK_STATIC_BUILD_URL}
    DOWNLOAD_EXTRACT_TIMESTAMP FALSE
  )
endif()

if(NOT ${TCL_SOURCES}_POPULATED)
  if(NOT TCL_STATIC_BUILD_TCL_LOCAL_SOURCES)
    message(STATUS "Downloading Tcl sources: ${TCL_STATIC_BUILD_URL} ...")
  else()
    message(STATUS "Using local Tcl sources: ${TCL_STATIC_BUILD_URL}")
  endif()
  FetchContent_MakeAvailable(${TCL_SOURCES})
  if(NOT TCL_STATIC_BUILD_TCL_LOCAL_SOURCES)
    message(STATUS "Completed download: ${${TCL_SOURCES}_SOURCE_DIR}")
  endif()
endif()

if(NOT ${CRITCL_SOURCES}_POPULATED)
  if(NOT TCL_STATIC_BUILD_CRITCL_LOCAL_SOURCES)
    message(STATUS "Downloading Critcl sources: ${CRITCL_STATIC_BUILD_URL} ...")
  else()
    message(STATUS "Using local Critcl sources: ${CRITCL_STATIC_BUILD_URL}")
  endif()
  FetchContent_MakeAvailable(${CRITCL_SOURCES})
  if(NOT TCL_STATIC_BUILD_TCL_LOCAL_SOURCES)
    message(STATUS "Completed download: ${${CRITCL_SOURCES}_SOURCE_DIR}")
  endif()
endif()

if(NOT ${TCLLIB_SOURCES}_POPULATED)
  if(NOT TCL_STATIC_BUILD_TCLLIB_LOCAL_SOURCES)
    message(STATUS "Downloading Tcllib sources: ${TCLLIB_STATIC_BUILD_URL} ...")
  else()
    message(STATUS "Using local Tcllib sources: ${TCLLIB_STATIC_BUILD_URL}")
  endif()
  FetchContent_MakeAvailable(${TCLLIB_SOURCES})
  if(NOT TCL_STATIC_BUILD_TCL_LOCAL_SOURCES)
    message(STATUS "Completed download: ${${TCLLIB_SOURCES}_SOURCE_DIR}")
  endif()
endif()

if(TCL_STATIC_BUILD_TK)
  if(NOT ${TK_SOURCES}_POPULATED)
    if(NOT TCL_STATIC_BUILD_TK_LOCAL_SOURCES)
      message(STATUS "Downloading Tk sources: ${TK_STATIC_BUILD_URL} ...")
    else()
      message(STATUS "Using local Tk sources: ${TK_STATIC_BUILD_URL}")
    endif()
    FetchContent_MakeAvailable(${TK_SOURCES})
    if(NOT TCL_STATIC_BUILD_TK_LOCAL_SOURCES)
      message(STATUS "Completed download: ${${TK_SOURCES}_SOURCE_DIR}")
    endif()
  endif()
endif()

if(APPLE OR UNIX)
 include(LinuxMacosBuild)
else()
  include(Windows)
endif()
