# -*- 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.113
categories          www
# See: https://trac.macports.org/ticket/67097
platforms           {darwin >= 20}
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/

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}
    }
}

# Ensure port can still be parsed on unsupported archs
set chrome_arch invalid
platform darwin arm {
    set chrome_arch mac-arm64
}
platform darwin i386 {
    set chrome_arch mac-x64
}

# https://googlechromelabs.github.io/chrome-for-testing/
master_sites        https://storage.googleapis.com/chrome-for-testing-public/${version}/${chrome_arch}

use_zip             yes

distname            ${name}-${chrome_arch}
distfiles           ${distname}${extract.suffix}

variant chrome_app \
    description {Also install chrome-for-testing app with fixed version.} {
    distfiles-append \
                    chrome-${chrome_arch}${extract.suffix}
    notes-append \
        "Use ${name} with Google\\ Chrome\\ for\\ Testing.app, e.g.
    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/${name}',
        browser_executable_path='${applications_dir}/Google Chrome for Testing.app/Contents/MacOS/Google Chrome for Testing',
    )"
}

default_variants-append +chrome_app

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

if {${configure.build_arch} eq {arm64}} {
    checksums       ${name}-${chrome_arch}${extract.suffix} \
                    rmd160  3b4e8bf5c14b957dd3a205491d67c1b85b0eefe4 \
                    sha256  8c8a6956b93dc2ccbbc2d1146fa99da0af507c291567d421570e9912e91d2189 \
                    size    8419908
    if { [variant_isset "chrome_app"] } {
        checksums-append \
                    chrome-${chrome_arch}${extract.suffix} \
                    rmd160  2b8df3fd6705af1f3d7bd021d4db54da36a4585f \
                    sha256  8a0a23242157daa323127353e2c95c5d1c21c79d21c2d776b42b572399e087a0 \
                    size    159581929
    }
} elseif {${configure.build_arch} eq {x86_64}} {
    checksums       ${name}-${chrome_arch}${extract.suffix} \
                    rmd160  b8547761e97c1743e209e5d8f1f3227bdc7938b5 \
                    sha256  3c8ef934f9a37f23081f24fc4354ea37ad08202cc3cca157380bf6524c5bc6ec \
                    size    9207465
    if { [variant_isset "chrome_app"] } {
        checksums-append \
                    chrome-${chrome_arch}${extract.suffix} \
                    rmd160  ac4d696c48992a5908dadeac05f54268b2f538ae \
                    sha256  4c7f9dc303fb16e028ed5430406ec2ff10664da15fedc5757c5672fbea58dd22 \
                    size    167728647
    }
}

# 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/${name}
    if { [variant_isset "chrome_app"] } {
        copy \
            "${workpath}/chrome-${chrome_arch}/Google Chrome for Testing.app" \
            ${destroot}${applications_dir}
    }
}

# 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 \
                    port:py${python_version}-websockets

    post-patch {
        copy \
            ${worksrcpath}/${name} \
            ${worksrcpath}/${name}-${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}/${name}-undetected
        ln -s \
            ${prefix}/bin/${name}-undetected \
            ${worksrcpath}/${name}
    }

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

    notes-append \
        "The ${name} binary has been patched to mitigate detection.\
The original ${name} binary has been moved to ${name}-original.\
Use ${name} 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/${name}-undetected')
"
}

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