# This file is part of OpenOrienteering.

# Copyright 2016-2020 Kai Pastor
#
# Redistribution and use is allowed according to the terms of the BSD license:
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 
# 1. Redistributions of source code must retain the copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products 
#    derived from this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

set(version        7.1.1)
set(download_hash  SHA256=324e7abb5569fb5f787dadf1d4474766915c485a188cf48cf07153b99156b5f9)
set(patch_version  ${version}-1)
set(patch_hash     SHA256=2e37382de22a36fcb9e859aaa0d9eb5979fcd45eb55773c6602793144be9578a)
set(base_url       https://snapshot.debian.org/archive/debian/20200901T150453Z/pool/main/p/proj/)

option(USE_SYSTEM_PROJ "Use the system PROJ4 if possible" ON)

set(test_system_proj [[
	if(${USE_SYSTEM_PROJ})
		enable_language(C)
		find_library(PROJ4_LIBRARY NAMES proj QUIET)
		find_path(PROJ4_INCLUDE_DIR NAMES proj_api.h QUIET)
		string(FIND "${PROJ4_LIBRARY}" "${CMAKE_STAGING_PREFIX}/" staging_prefix_start)
		if(PROJ4_LIBRARY AND PROJ4_INCLUDE_DIR AND NOT staging_prefix_start EQUAL 0)
			message(STATUS "Found ${SYSTEM_NAME} PROJ4: ${PROJ4_LIBRARY}")
			set(BUILD_CONDITION 0)
		endif()
	endif()
]])

string(CONCAT portability_sed [[
# Generated by ]] "${CMAKE_CURRENT_LIST_FILE}\n" [[
# On macOS, the temporary files are not put into the subdir.
s/^\(begin [^ ]*\) [^ ]*xzi/\1 tmp.xzi/
s/^\(xz -dc\) [^ ]*xzi/\1 tmp.xzi/
$ i \
rm -f tmp.xzi
]])

superbuild_package(
  NAME           proj-patches
  VERSION        ${patch_version}
  DEPENDS
    host:xz-utils
  
  SOURCE_WRITE
    portability.sed  portability_sed
  SOURCE
    URL            ${base_url}proj_${patch_version}.debian.tar.xz
    URL_HASH       ${patch_hash}
    PATCH_COMMAND
      sed -f "<SOURCE_DIR>/portability.sed" -i -- "<SOURCE_DIR>/datumgrids.shar"
    COMMAND
      sed -f "<SOURCE_DIR>/portability.sed" -i -- "<SOURCE_DIR>/datumgrids-ch.shar"
  
  # Only in the build phase, we can rely on our `xz` from package xz-utils.
  BUILD [[
    CONFIGURE_COMMAND ""
    BUILD_COMMAND
      "${CMAKE_COMMAND}" -E make_directory "<SOURCE_DIR>/data"
    COMMAND
      "${CMAKE_COMMAND}" -E chdir "<SOURCE_DIR>/data"
        sh "<SOURCE_DIR>/datumgrids.shar" -c
    COMMAND
      # Verify existence of a file from datumgrids.shar
      "${CMAKE_COMMAND}" -E md5sum "<SOURCE_DIR>/data/BETA2007.gsb"
    #COMMAND
    #  "${CMAKE_COMMAND}" -E chdir "<SOURCE_DIR>/data"
    #    sh "<SOURCE_DIR>/datumgrids-ch.shar" -c
    INSTALL_COMMAND ""
  ]]
)

superbuild_package(
  NAME           proj
  VERSION        ${patch_version}
  DEPENDS
    source:proj-patches-${patch_version}  # source-time: patches
    host:proj-patches-${patch_version}    # install-time: data
    host:sqlite3
    curl
    googletest
    pkg-config
    sqlite3
    tiff
  
  SOURCE
    URL            ${base_url}proj_${version}.orig.tar.gz
    URL_HASH       ${download_hash}
    PATCH_COMMAND
      "${CMAKE_COMMAND}"
        -Dpackage=proj-patches-${patch_version}
        -P "${APPLY_PATCHES_SERIES}"
  
  USING            USE_SYSTEM_PROJ patch_version
  BUILD_CONDITION  ${test_system_proj}
  BUILD [[
    CMAKE_ARGS
      "-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}"
      "-DCMAKE_BUILD_TYPE:STRING=$<CONFIG>"
      -DBUILD_SHARED_LIBS=ON
      -DUSE_THREAD=ON
      -DPROJ_BIN_SUBDIR=bin
      -DPROJ_LIB_SUBDIR=lib
      -DPROJ_DATA_SUBDIR=share/proj
      -DPROJ_DOC_SUBDIR=share/doc/proj
      -DPROJ_INCLUDE_SUBDIR=include
      -DPROJ_CMAKE_SUBDIR=lib/cmake
    $<$<NOT:$<BOOL:@CMAKE_CROSSCOMPILING@>>:
      -DBUILD_TESTING=ON
      -DUSE_EXTERNAL_GTEST=ON
    >
    $<$<BOOL:@CMAKE_CROSSCOMPILING@>:
      -DCMAKE_PROGRAM_PATH=${HOST_DIR}/bin # for sqlite3
      -DBUILD_TESTING=OFF
    >
    $<$<NOT:$<OR:$<BOOL:@CMAKE_CROSSCOMPILING@>,$<BOOL:@MSYS@>>>:
    TEST_COMMAND
      "${CMAKE_COMMAND}" -E env
        "PROJ_LIB=${DESTDIR}${CMAKE_STAGING_PREFIX}/share/proj"
        "${CMAKE_COMMAND}" --build . --target test
    TEST_AFTER_INSTALL
    >
    INSTALL_COMMAND
      "${CMAKE_COMMAND}" --build . --target install/strip/fast
    COMMAND
      "${CMAKE_COMMAND}" -E copy_directory
        "<SOURCE_DIR>/../proj-patches-${patch_version}/data"
        "${DESTDIR}${CMAKE_STAGING_PREFIX}/share/proj"
    COMMAND
      "${CMAKE_COMMAND}" -E copy
        "<SOURCE_DIR>/../proj-patches-${patch_version}/copyright"
        "${DESTDIR}${CMAKE_STAGING_PREFIX}/share/doc/copyright/proj-${patch_version}.txt"
  ]]
)
