include(vcpkg_common_functions)
vcpkg_download_distfile(ARCHIVE_FILE
    URLS "http://www.zlib.net/zlib-1.2.11.tar.gz" "https://downloads.sourceforge.net/project/libpng/zlib/1.2.11/zlib-1.2.11.tar.gz"
    FILENAME "zlib1211.tar.gz"
    SHA512 73fd3fff4adeccd4894084c15ddac89890cd10ef105dd5e1835e1e9bbb6a49ff229713bd197d203edfa17c2727700fce65a2a235f07568212d820dca88b528ae
)
vcpkg_extract_source_archive_ex(
    ARCHIVE ${ARCHIVE_FILE}
    OUT_SOURCE_PATH SOURCE_PATH
    PATCHES
        no-examples.patch
        cmake_dont_build_more_than_needed.patch
)

# This is generated during the cmake build
file(REMOVE ${SOURCE_PATH}/zconf.h)

vcpkg_configure_cmake(
    SOURCE_PATH ${SOURCE_PATH}
    PREFER_NINJA
    OPTIONS
        -DSKIP_BUILD_EXAMPLES=ON
    OPTIONS_RELEASE
        -DINSTALL_PKGCONFIG_DIR=${CURRENT_PACKAGES_DIR}/lib/pkgconfig
        -DSKIP_INSTALL_FILES=OFF
    OPTIONS_DEBUG
        -DSKIP_INSTALL_FILES=ON
        -DSKIP_INSTALL_HEADERS=ON
)

vcpkg_install_cmake()
vcpkg_fixup_pkgconfig_file()

# Both dynamic and static are built, so keep only the one needed
if(VCPKG_LIBRARY_LINKAGE STREQUAL static)
    if(EXISTS ${CURRENT_PACKAGES_DIR}/lib/zlibstatic.lib)
        file(RENAME ${CURRENT_PACKAGES_DIR}/lib/zlibstatic.lib ${CURRENT_PACKAGES_DIR}/lib/zlib.lib)
    endif()
    if(EXISTS ${CURRENT_PACKAGES_DIR}/debug/lib/zlibstaticd.lib)
        file(RENAME ${CURRENT_PACKAGES_DIR}/debug/lib/zlibstaticd.lib ${CURRENT_PACKAGES_DIR}/debug/lib/zlibd.lib)
    endif()

    if(EXISTS ${CURRENT_PACKAGES_DIR}/lib/libzlibstatic.a)
        file(RENAME ${CURRENT_PACKAGES_DIR}/lib/libzlibstatic.a ${CURRENT_PACKAGES_DIR}/lib/libz.a)
    endif()
    if(EXISTS ${CURRENT_PACKAGES_DIR}/debug/lib/libzlibstaticd.a)
        file(RENAME ${CURRENT_PACKAGES_DIR}/debug/lib/libzlibstaticd.a ${CURRENT_PACKAGES_DIR}/debug/lib/libzd.a)
    endif()

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

file(INSTALL ${CMAKE_CURRENT_LIST_DIR}/LICENSE DESTINATION ${CURRENT_PACKAGES_DIR}/share/zlib RENAME copyright)

vcpkg_copy_pdbs()

file(COPY ${CMAKE_CURRENT_LIST_DIR}/usage DESTINATION ${CURRENT_PACKAGES_DIR}/share/${PORT})
