set(MAJOR 3)
set(MINOR 0)
set(REVISION 23)
set(VERSION v${MAJOR}.${MINOR}.${REVISION})
set(PACKAGE_NAME ${PORT}-${VERSION})
set(PACKAGE ${PACKAGE_NAME}.tar.bz2)

vcpkg_fail_port_install(ON_TARGET "Windows" MESSAGE "Mapnik is not supported on windows")
vcpkg_find_acquire_program(PYTHON3)

set(SOURCE_PATH_DEBUG   ${CURRENT_BUILDTREES_DIR}/src-${TARGET_TRIPLET}-debug/${PORT}-${VERSION})
set(SOURCE_PATH_RELEASE ${CURRENT_BUILDTREES_DIR}/src-${TARGET_TRIPLET}-release/${PORT}-${VERSION})

vcpkg_download_distfile(ARCHIVE
    URLS "https://github.com/${PORT}/${PORT}/releases/download/${VERSION}/${PACKAGE}"
    FILENAME "${PACKAGE}"
    SHA512 d901e092c8d876bc21f0e8cb713a6d102308983652f21f4fb5f120c50616724b692bcf0abe66ffecf1f0aa8a60e0f9b803be85eb0eee83c477895082559b38c9
)

vcpkg_extract_source_archive_ex(
    OUT_SOURCE_PATH SOURCE_PATH
    ARCHIVE ${ARCHIVE}
    PATCHES
        gdal-framework.patch
        mapnik-render-link.patch
        tiff-link.patch
        icu-link.patch
        config-path.patch
)

set(SOURCE_PATH_DEBUG ${CURRENT_BUILDTREES_DIR}/src/mapnik-dbg)
set(SOURCE_PATH_RELEASE ${CURRENT_BUILDTREES_DIR}/src/mapnik-rel)

file(REMOVE ${SOURCE_PATH_DEBUG})
file(REMOVE ${SOURCE_PATH_RELEASE})
file(COPY ${SOURCE_PATH}/ DESTINATION ${SOURCE_PATH_DEBUG})
file(COPY ${SOURCE_PATH}/ DESTINATION ${SOURCE_PATH_RELEASE})

set(MAPNIK_INPUT_PLUGINS "raster,shape,csv,geojson")

set(SCONS_OPTIONS
    CC=${CMAKE_C_COMPILER}
    CXX=${CMAKE_CXX_COMPILER}
    LD=${LINKER_EXECUTABLE}
    AR=${CMAKE_AR}
    RANLIB=${CMAKE_RANLIB}
    PATH=${CURRENT_INSTALLED_DIR}/tools
    CUSTOM_LDFLAGS=${CMAKE_EXE_LINKER_FLAGS}
    BOOST_INCLUDES=${CURRENT_INSTALLED_DIR}/include
    ICU_INCLUDES=${CURRENT_INSTALLED_DIR}/include
    FREETYPE_INCLUDES=${CURRENT_INSTALLED_DIR}/include/freetype2
    HB_INCLUDES=${CURRENT_INSTALLED_DIR}/include
    PROJ_INCLUDES=${CURRENT_INSTALLED_DIR}/include
    PNG_INCLUDES=${CURRENT_INSTALLED_DIR}/include
    TIFF_INCLUDES=${CURRENT_INSTALLED_DIR}/include
    CAIRO=no
    JPEG=no
    WEBP=no
    DEMO=no
    CPP_TESTS=no
    BINDINGS=none
    SHAPEINDEX=no
    MAPNIK_INDEX=no
    PATH_REMOVE=/usr
)

if (${VCPKG_LIBRARY_LINKAGE} STREQUAL "static")
    list(APPEND SCONS_OPTIONS LINKING=static)
    list(APPEND SCONS_OPTIONS PLUGIN_LINKING=static)
else ()
    list(APPEND SCONS_OPTIONS LINKING=shared)
    list(APPEND SCONS_OPTIONS PLUGIN_LINKING=shared)
endif ()

if (${VCPKG_CRT_LINKAGE} STREQUAL "static")
    list(APPEND SCONS_OPTIONS RUNTIME_LINK=static)
else ()
    list(APPEND SCONS_OPTIONS RUNTIME_LINK=shared)
endif ()

if ("gdal" IN_LIST FEATURES)
    list(APPEND SCONS_OPTIONS GDAL_CONFIG=${CURRENT_INSTALLED_DIR}/tools/gdal/gdal-config)
    set(MAPNIK_INPUT_PLUGINS "${MAPNIK_INPUT_PLUGINS},gdal")
else ()
    list(APPEND SCONS_OPTIONS GDAL_CONFIG=${CURRENT_INSTALLED_DIR}/tools/invalid-config)
endif ()

list(APPEND SCONS_OPTIONS INPUT_PLUGINS=${MAPNIK_INPUT_PLUGINS})
vcpkg_assemble_compiler_cflags(DEBUG MAPNIK_C_FLAGS_DEBUG RELEASE MAPNIK_C_FLAGS_RELEASE)
vcpkg_assemble_compiler_cxxflags(DEBUG MAPNIK_CXX_FLAGS_DEBUG RELEASE MAPNIK_CXX_FLAGS_RELEASE)

set(SCONS_OPTIONS_REL
    CUSTOM_CFLAGS=${MAPNIK_C_FLAGS_RELEASE}
    CUSTOM_CXXFLAGS=${MAPNIK_CXX_FLAGS_RELEASE}
    ${SCONS_OPTIONS}
    DEBUG=no
    PREFIX=${CURRENT_PACKAGES_DIR}
    BOOST_LIBS=${CURRENT_INSTALLED_DIR}/lib
    ICU_LIBS=${CURRENT_INSTALLED_DIR}/lib
    FREETYPE_LIBS=${CURRENT_INSTALLED_DIR}/lib
    HB_LIBS=${CURRENT_INSTALLED_DIR}/lib
    PROJ_LIBS=${CURRENT_INSTALLED_DIR}/lib
    PNG_LIBS=${CURRENT_INSTALLED_DIR}/lib
    TIFF_LIBS=${CURRENT_INSTALLED_DIR}/lib
)

set(SCONS_OPTIONS_DBG
    ${SCONS_OPTIONS}
    DEBUG=yes
    CUSTOM_CFLAGS=${MAPNIK_C_FLAGS_DEBUG}
    CUSTOM_CXXFLAGS=${MAPNIK_CXX_FLAGS_DEBUG}
    PREFIX=${CURRENT_PACKAGES_DIR}/debug
    BOOST_LIBS=${CURRENT_INSTALLED_DIR}/debug/lib
    ICU_LIBS=${CURRENT_INSTALLED_DIR}/debug/lib
    FREETYPE_LIBS=${CURRENT_INSTALLED_DIR}/debug/lib
    HB_LIBS=${CURRENT_INSTALLED_DIR}/debug/lib
    PROJ_LIBS=${CURRENT_INSTALLED_DIR}/lib
    PNG_LIBS=${CURRENT_INSTALLED_DIR}/debug/lib
    TIFF_LIBS=${CURRENT_INSTALLED_DIR}/debug/lib
)

message(STATUS "Configuring ${TARGET_TRIPLET}-rel")
message(STATUS "${SCONS_OPTIONS_REL}")
vcpkg_execute_required_process(
    COMMAND ${PYTHON3} scons/scons.py configure
    "${SCONS_OPTIONS_REL}"
    WORKING_DIRECTORY ${SOURCE_PATH_RELEASE}
    LOGNAME scons-configure-${TARGET_TRIPLET}-release
)

message(STATUS "Configuring ${TARGET_TRIPLET}-dbg")
vcpkg_execute_required_process(
    COMMAND ${PYTHON3} scons/scons.py configure
    "${SCONS_OPTIONS_DBG}"
    WORKING_DIRECTORY ${SOURCE_PATH_DEBUG}
    LOGNAME scons-configure-${TARGET_TRIPLET}-debug
)

message(STATUS "Building ${TARGET_TRIPLET}-rel")
vcpkg_execute_required_process(
    COMMAND ${PYTHON3} scons/scons.py --jobs=${VCPKG_CONCURRENCY}
    WORKING_DIRECTORY ${SOURCE_PATH_RELEASE}
    LOGNAME scons-build-${TARGET_TRIPLET}-release
)

message(STATUS "Building ${TARGET_TRIPLET}-dbg")
vcpkg_execute_required_process(
    COMMAND ${PYTHON3} scons/scons.py --jobs=${VCPKG_CONCURRENCY}
    WORKING_DIRECTORY ${SOURCE_PATH_DEBUG}
    LOGNAME scons-build-${TARGET_TRIPLET}-debug
)

message(STATUS "Installing ${TARGET_TRIPLET}-rel")
vcpkg_execute_required_process(
    COMMAND ${PYTHON3} scons/scons.py install
    WORKING_DIRECTORY ${SOURCE_PATH_RELEASE}
    LOGNAME scons-install-${TARGET_TRIPLET}-release
)

message(STATUS "Installing ${TARGET_TRIPLET}-dbg")
vcpkg_execute_required_process(
    COMMAND ${PYTHON3} scons/scons.py install
    WORKING_DIRECTORY ${SOURCE_PATH_DEBUG}
    LOGNAME scons-install-${TARGET_TRIPLET}-debug
)

file(REMOVE_RECURSE ${CURRENT_PACKAGES_DIR}/debug/include)
file(REMOVE_RECURSE ${CURRENT_PACKAGES_DIR}/debug/bin)

file(GLOB BIN_FILES ${CURRENT_PACKAGES_DIR}/bin/*)
file(COPY ${BIN_FILES} DESTINATION ${CURRENT_PACKAGES_DIR}/tools)
file(REMOVE_RECURSE ${CURRENT_PACKAGES_DIR}/bin)
file(REMOVE_RECURSE ${CURRENT_PACKAGES_DIR}/lib/mapnik)
file(REMOVE_RECURSE ${CURRENT_PACKAGES_DIR}/debug/lib/mapnik)

# Handle copyright
file(INSTALL ${SOURCE_PATH_RELEASE}/COPYING DESTINATION ${CURRENT_PACKAGES_DIR}/share/${PORT} RENAME copyright)
