# This file is part of OpenOrienteering.

# Copyright 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        2.10.4+dfsg)
set(download_hash  SHA256=db0c0938b3b75cf314775baa75198098e41583b3aaa4804b454f183ce45120a9)
set(patch_version  ${version}-1)
set(patch_hash     SHA256=96276d66eb56247545cd9e60ffae0bd5b5aee0490e4e7171337a6666bc51b125)
set(base_url       https://snapshot.debian.org/archive/debian/20201208T212129Z/pool/main/f/freetype/)

option(USE_SYSTEM_FREETYPE "Use the system Freetype if possible" ON)

set(test_system_freetype [[
	if(${USE_SYSTEM_FREETYPE})
		enable_language(C)
		find_package(Freetype)
		string(FIND "${FREETYPE_LIBRARIES}" "${CMAKE_STAGING_PREFIX}/" staging_prefix_start)
		if(FREETYPE_FOUND AND FREETYPE_INCLUDE_DIRS AND NOT staging_prefix_start EQUAL 0)
			message(STATUS "Found ${SYSTEM_NAME} FreeType: ${FREETYPE_LIBRARY} (${FREETYPE_VERSION_STRING})")
			set(BUILD_CONDITION 0)
		endif()
	endif()
]])

string(CONCAT series_sed [[
# Generated by ]] "${CMAKE_CURRENT_LIST_FILE}\n" [[
/^fix-js-doc/d
/^ft2demos/d
/^hide-donations/d
/^no-web-fonts/d
]])

superbuild_package(
  NAME           freetype-patches
  VERSION        ${patch_version}
  
  SOURCE
    URL            ${base_url}freetype_${patch_version}.debian.tar.xz
    URL_HASH       ${patch_hash}
)
  
superbuild_package(
  NAME           freetype
  VERSION        ${patch_version}
  DEPENDS
    source:freetype-patches-${patch_version}
    libpng
    zlib
  
  SOURCE_WRITE
    series.sed     series_sed
  SOURCE
    URL            ${base_url}freetype_${version}.orig.tar.xz
    URL_HASH       ${download_hash}
    PATCH_COMMAND
      sed -f "<SOURCE_DIR>/series.sed" -i --
        "<SOURCE_DIR>/../freetype-patches-${patch_version}/patches/series"
    COMMAND
      "${CMAKE_COMMAND}"
        -Dpackage=freetype-patches-${patch_version}
        -P "${APPLY_PATCHES_SERIES}"
  
  USING            USE_SYSTEM_FREETYPE patch_version
  BUILD_CONDITION  ${test_system_freetype}
  BUILD [[
    CMAKE_ARGS
      "-DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE}"
      "-DCMAKE_BUILD_TYPE:STRING=$<CONFIG>"
      -DBUILD_SHARED_LIBS=ON
      -DFT_WITH_PNG=ON
      -DFT_WITH_ZLIB=ON
      -DCMAKE_DISABLE_FIND_PACKAGE_BrotliDec=ON
      -DCMAKE_DISABLE_FIND_PACKAGE_BZip2=ON
      -DCMAKE_DISABLE_FIND_PACKAGE_HarfBuzz=ON
    INSTALL_COMMAND
      "${CMAKE_COMMAND}" --build . --target install/strip/fast
    COMMAND
      "${CMAKE_COMMAND}" -E copy
        "<SOURCE_DIR>/../freetype-patches-${patch_version}/copyright"
        "${DESTDIR}${CMAKE_STAGING_PREFIX}/share/doc/copyright/freetype-${patch_version}.txt"
  ]]
)
