# -*- coding: utf-8; mode: tcl; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- vim:fenc=utf-8:et:sw=4:ts=4:sts=4

PortSystem      1.0
PortGroup       cmake 1.1
PortGroup       github 1.0
PortGroup       legacysupport 1.1

legacysupport.newest_darwin_requires_legacy 16
# Pre-10.12 Apple libc++ does not support all needed features.
# This does not affect macOS versions using libsdtc++.
legacysupport.use_mp_libcxx yes

github.setup    lexiforest curl-impersonate 2.0.0 v
revision        0
categories      net www
maintainers     {@barracuda156 macos-powerpc.org:barracuda} openmaintainer
license         MIT

description     active fork of curl-impersonate with more versions and build targets
long_description \
                An {*}${description}.

distname        ${github.project}-${version}
github.tarball_from archive

# curl-impersonate uses specific versions of dependencies and links them statically.
# See: https://github.com/lexiforest/curl-impersonate/blob/ec41b71ce888806bfec56ada7a7258d333eb3d19/CMakeLists.txt
set boringssl_hash      156c7b75ae9b8c3b3f847acf264f17594c3859fb
set brotli_version      1.2.0
set curl_version        8.21.0
set libidn_version      2.3.7
set nghttp2_version     1.63.0
set nghttp3_version     1.15.0
set ngtcp2_version      1.20.0
set zlib_version        1.3.1
set zstd_version        1.5.7

set curl_tag            [string map {. _} ${curl_version}]

distfiles-append \
                ${boringssl_hash}${extract.suffix}:boringssl \
                v${brotli_version}${extract.suffix}:brotli \
                curl-${curl_tag}${extract.suffix}:curl \
                libidn2-${libidn_version}${extract.suffix}:libidn \
                v${nghttp2_version}${extract.suffix}:nghttp2 \
                nghttp3-${nghttp3_version}${extract.suffix}:nghttp3 \
                ngtcp2-${ngtcp2_version}${extract.suffix}:ngtcp2 \
                zlib-${zlib_version}${extract.suffix}:zlib \
                zstd-${zstd_version}${extract.suffix}:zstd

master_sites-append \
                https://github.com/google/boringssl/archive/:boringssl \
                https://github.com/google/brotli/archive/refs/tags/:brotli \
                https://github.com/curl/curl/archive/refs/tags/:curl \
                https://ftp.gnu.org/gnu/libidn/:libidn \
                https://github.com/nghttp2/nghttp2/archive/refs/tags/:nghttp2 \
                https://github.com/ngtcp2/nghttp3/releases/download/v${nghttp3_version}/:nghttp3 \
                https://github.com/ngtcp2/ngtcp2/releases/download/v${ngtcp2_version}/:ngtcp2 \
                https://github.com/madler/zlib/releases/download/v${zlib_version}/:zlib \
                https://github.com/facebook/zstd/releases/download/v${zstd_version}/:zstd

checksums       ${distname}${extract.suffix} \
                rmd160  7b86ca2cb6132bedd2d91fc44ff7c875d3d160cb \
                sha256  a9827cfce8246e78b86e26f012ae9fcb9bc05822009cd7ce4c9e130af6bdfb6f \
                size    207780 \
                ${boringssl_hash}${extract.suffix} \
                rmd160  2606447aeebd42ef2ee5fc948713f0aa3666450b \
                sha256  4cbd70611e6400616fd5a58f42e19e7bafd568c674ecb3a6418c43ae1e3ac93b \
                size    74515087 \
                v${brotli_version}${extract.suffix} \
                rmd160  949e0c56045b760b854714f45b0912936a9be53a \
                sha256  816c96e8e8f193b40151dad7e8ff37b1221d019dbcb9c35cd3fadbfe6477dfec \
                size    646315 \
                curl-${curl_tag}${extract.suffix} \
                rmd160  bb9ba1116f1ce6e91fa609d6a5d6803f614c9aae \
                sha256  ec753aa6f408a3ca9f0d6d5f7a77417aecd1544db13c03ae5d443612bf367364 \
                size    3592963 \
                libidn2-${libidn_version}${extract.suffix} \
                rmd160  511eb10830342dc546824c5e48bb6ef0022382ab \
                sha256  4c21a791b610b9519b9d0e12b8097bf2f359b12f8dd92647611a929e6bfd7d64 \
                size    2155214 \
                v${nghttp2_version}${extract.suffix} \
                rmd160  fa3d04976a104cf66338997f6fa55d1c465623d6 \
                sha256  f3da0627bee7a6a60f5a4eb6de8d17d25e99f50f87b0fc0c20676c682bf31098 \
                size    1070155 \
                nghttp3-${nghttp3_version}${extract.suffix} \
                rmd160  37d60d4b7c56836ec03cb94bd2a528781b40c6ab \
                sha256  0e431c81eb2d3df5ced048d6e942925ff922ad053e76a0274eea7b164c9b776e \
                size    655937 \
                ngtcp2-${ngtcp2_version}${extract.suffix} \
                rmd160  839f587fb815485782b20d8891bf718a5db1af56 \
                sha256  daa3398640a1fe1823ad8e2b1b88562b8da7888a47edd8c8ba03c75cfd9c8bb6 \
                size    1204305 \
                zlib-${zlib_version}${extract.suffix} \
                rmd160  52945a1a5a1bd51ac676a0b9197bdaacbc537fd6 \
                sha256  9a93b2b7dfdac77ceba5a558a580e74667dd6fede4585b91eefb60f03b72df23 \
                size    1512791 \
                zstd-${zstd_version}${extract.suffix} \
                rmd160  9a07d2d7b2cc9c6acd48dbf593ae1bace4590c94 \
                sha256  eb33e51f49a15e023950cd7825ca74a4a2b43db8354825ac24fc1b7ee09e6fa3 \
                size    2434947

set dep_src_dir     ${worksrcpath}/deps/src
set dep_build_dir   ${worksrcpath}/deps/build
set dep_inst_dir    ${worksrcpath}/deps/install

post-extract {
    xinstall -d ${dep_src_dir}

    move ${workpath}/boringssl-${boringssl_hash} ${dep_src_dir}/boringssl
    move ${workpath}/brotli-${brotli_version} ${dep_src_dir}/brotli
    move ${workpath}/curl-curl-${curl_tag} ${dep_src_dir}/curl
    move ${workpath}/libidn2-${libidn_version} ${dep_src_dir}/libidn2
    move ${workpath}/nghttp2-${nghttp2_version} ${dep_src_dir}/nghttp2
    move ${workpath}/nghttp3-${nghttp3_version} ${dep_src_dir}/nghttp3
    move ${workpath}/ngtcp2-${ngtcp2_version} ${dep_src_dir}/ngtcp2
    move ${workpath}/zlib-${zlib_version} ${dep_src_dir}/zlib
    move ${workpath}/zstd-${zstd_version} ${dep_src_dir}/zstd
}

cmake.out_of_source no
cmake.generator     Ninja

set py_ver          3.14
set py_ver_nodot    [string map {. {}} ${py_ver}]
configure.python    ${prefix}/bin/python${py_ver}

# Prefer system libiconv, it works fine back to powerpc:
set iconv_prefix        /usr
# macOS 11+ do not have libiconv in filesystem however, so there we need a port:
if {${os.platform} eq "darwin" && ${os.major} > 19} {
    set iconv_prefix    ${prefix}
    depends_lib-append  port:libiconv
}

depends_build-append \
                    port:autoconf \
                    port:automake \
                    port:libtool \
                    path:bin/perl:perl5 \
                    path:bin/pkg-config:pkgconfig

depends_run-append  path:share/curl/curl-ca-bundle.crt:curl-ca-bundle

compiler.c_standard     2011
compiler.cxx_standard   2020

patch.pre_args-replace  -p0 -p1

patchfiles-append       0001-CMakeLists.patch \
                        0002-libiconv.patch \
                        0003-boringssl.patch

if {${os.major} < 11 && [string match *gcc* ${configure.compiler}]} {
    patchfiles-append   0004-zstd.patch
}

post-patch {
    reinplace "s,@PREFIX@,${prefix},g" ${worksrcpath}/CMakeLists.txt
    reinplace "s,@ICONV_PREFIX@,${iconv_prefix}," ${worksrcpath}/CMakeLists.txt
}

configure.pre_args-replace \
                -DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=ON \
                -DCMAKE_BUILD_WITH_INSTALL_RPATH:BOOL=OFF

# Defaults which normal curl uses as well:
configure.args-append \
                -DCURL_CA_BUNDLE=${prefix}/share/curl/curl-ca-bundle.crt \
                -DCURL_SUBPROJECT_FORCE_OPENSSL_PATHS=ON \
                -DUSE_APPLE_SECTRUST=OFF \
                -DUSE_LIBIDN2=ON

# asm does not exist for powerpc in boringssl and may not exist/work on other exotic archs.
if {${configure.build_arch} ni [list arm64 x86_64]} {
    configure.args-append \
                -DDISABLE_BORINGSSL_ASM=ON
}

if {${cxx_stdlib} eq "libc++"} {
    configure.args-append \
                -DCURL_IMPERSONATE_CXX_RUNTIME_LIBRARY:STRING=c++
} else {
    configure.args-append \
                -DCURL_IMPERSONATE_CXX_RUNTIME_LIBRARY:STRING=stdc++
}

# USE_LIBIDN2 requires prebuilt libidn2, which upstream does with a standalone script.
pre-configure {
    xinstall -d ${dep_build_dir}/libidn2
    system -W ${dep_build_dir}/libidn2 "PKG_CONFIG_PATH=\"${dep_inst_dir}/lib/pkgconfig\" \
        CPPFLAGS= LDFLAGS= LIBRARY_PATH= \
        ${dep_src_dir}/libidn2/configure --prefix=${worksrcpath}/deps/install --disable-nls \
        --disable-shared --enable-static --with-pic --with-included-libunistring \
        --with-libiconv-prefix=${iconv_prefix} --without-libintl-prefix && make V=1 MAKEFLAGS=-j${build.jobs} && make install MAKEFLAGS="
}

universal_variant   no

post-destroot {
    move ${destroot}${prefix}/include/curl ${destroot}${prefix}/include/${name}
    platform darwin {
        set dlib ${prefix}/lib/lib${name}.dylib
        system "install_name_tool -id ${dlib} ${destroot}${dlib}"
        system "install_name_tool -change @rpath/libcurl-impersonate.4.dylib ${dlib} ${destroot}${prefix}/bin/${name}"
    }
}
