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

PortSystem          1.0

name                chromedriver
version             136.0.7103.92
categories          www
platforms           darwin
maintainers         nomaintainer
license             Apache-2
supported_archs     arm64 x86_64

description         ChromeDriver - WebDriver for Chrome

long_description    WebDriver is an open source tool for automated testing of \
                    webapps across many browsers. It provides capabilities for \
                    navigating to web pages, user input, JavaScript execution, \
                    and more.

homepage            https://chromedriver.chromium.org/

# https://googlechromelabs.github.io/chrome-for-testing/

platform arm {
    master_sites    https://storage.googleapis.com/chrome-for-testing-public/${version}/mac-arm64
    distname        ${name}-mac-arm64
}
platform i386 {
    master_sites    https://storage.googleapis.com/chrome-for-testing-public/${version}/mac-x64
    distname        ${name}-mac-x64
}

use_zip             yes
distfiles           ${distname}${extract.suffix}

if {${os.arch} ni [list arm i386]} {
    known_fail      yes
    pre-fetch {
        ui_error "chromedriver is only available on \
            [join ${supported_archs} " or "] architectures."
        error {unsupported platform}
    }
}

# See: https://trac.macports.org/ticket/67097
if {${os.platform} eq "darwin" && ${os.major} < 20} {
    known_fail      yes
    pre-fetch {
        ui_error "${name} @${version} requires macOS 11 or later."
        return -code error "incompatible macOS version"
    }
}

# set build_arch by hand on arm64/x86_64 systems to get x86_64/arm64 checksums
# sudo port -v checksum chromedriver os.arch=arm build_arch=arm64
# run `port clean --all chromedriver` afterwards

if {${configure.build_arch} eq {arm64}} {
    checksums       rmd160  c4e6735210c5ef2523725aa6c3812e735d3a6765 \
                    sha256  00c0e1a7ceaf22ae95a5f6ebcdda5e11519f17082d77db8b9622bdbd80259324 \
                    size    8419833
} elseif {${configure.build_arch} eq {x86_64}} {
    checksums       rmd160  b8547761e97c1743e209e5d8f1f3227bdc7938b5 \
                    sha256  3c8ef934f9a37f23081f24fc4354ea37ad08202cc3cca157380bf6524c5bc6ec \
                    size    9207465
}

# most recent versions via
# https://sites.google.com/chromium.org/driver/downloads

set major_version   [lindex [split ${version} .] 0]

use_configure       no

build {}

destroot {
    copy \
        ${worksrcpath}/${name} \
        ${destroot}${prefix}/bin/${subport}
}

# use these to specify python versions, python3 required
# use ${prefix}/var/macports/sources/rsync.macports.org/macports/release/tarballs/ports/_resources/port1.0/group/python-1.0.tcl
set python_version  312
set python_branch   [string index ${python_version} 0].[string range ${python_version} 1 end]
set python_prefix   ${frameworks_dir}/Python.framework/Versions/${python_branch}
set python_bin      ${python_prefix}/bin/python${python_branch}

variant undetected \
    description {Undetected chromedriver.} {

    depends_lib-append \
                    port:python${python_version} \
                    port:py${python_version}-undetected-chromedriver

    post-patch {
        copy \
            ${worksrcpath}/${name} \
            ${worksrcpath}/${subport}-${major_version}

        # patch the chromedriver binary through undetected_chromedriver.Patcher
        system -W ${worksrcpath} \
            "${python_bin} <<PATCH_CHROMEDRIVER_PY
from undetected_chromedriver import Patcher

patcher = Patcher(executable_path='${worksrcpath}/${name}')
assert patcher.patch(), \\
    \"Binary '${worksrcpath}/${name}' is not patched.\"
PATCH_CHROMEDRIVER_PY
"
        if {${configure.build_arch} eq {arm64}} {
            system "codesign -f -s - ${worksrcpath}/${name}"
        }
        move \
            ${worksrcpath}/${name} \
            ${worksrcpath}/${subport}-undetected
        ln -s \
            ${prefix}/bin/${subport}-undetected \
            ${worksrcpath}/${name}
    }

    post-destroot {
        copy \
            ${worksrcpath}/${subport}-${major_version} \
            ${destroot}${prefix}/bin
        ln -s \
            ${prefix}/bin/${subport}-${major_version} \
            ${destroot}${prefix}/bin/${subport}-original
        copy \
            ${worksrcpath}/${subport}-undetected \
            ${destroot}${prefix}/bin
    }

    notes-append \
        "The ${subport} binary has been patched to mitigate detection.\
The original ${name} binary has been moved to ${subport}-original.\
Use ${subport} with the port

    py${python_version}-undetected-chromedriver

(with your preferred Python version) and the Python code:

    import undetected_chromedriver as uc
    driver = uc.Chrome(driver_executable_path='${prefix}/bin/${subport}-undetected')
"
}

livecheck.type  regex
livecheck.url   https://googlechromelabs.github.io/chrome-for-testing/
livecheck.regex {>Stable<[^\d]+>((?:\d+\.?)+)<}
